結果

問題 No.626 Randomized 01 Knapsack
ユーザー startcppstartcpp
提出日時 2017-12-29 18:03:12
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 782 bytes
コンパイル時間 553 ms
コンパイル使用メモリ 65,820 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-23 09:01:23
合計ジャッジ時間 2,692 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 5 ms
4,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 6 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//v / wが大きいほうから詰めていく嘘貪欲(もちろんWA)
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

struct Baggage {
	double key;
	long long v, w;
	Baggage() {}
	Baggage(long long v, long long w) {
		this->v = v;
		this->w = w;
		key = (double)v / w;
	}
	bool operator>(const Baggage &r) const {
		return key > r.key;
	}
};

int n;
long long w;
Baggage bag[5000];

int main() {
	int i;
	
	cin >> n >> w;
	for (i = 0; i < n; i++) {
		long long v, w;
		cin >> v >> w;
		bag[i] = Baggage(v, w);
	}
	sort(bag, bag + n, greater<Baggage>());
	
	long long remW = w;
	long long sumV = 0;
	for (i = 0; i < n; i++) {
		if (remW >= bag[i].w) {
			remW -= bag[i].w;
			sumV += bag[i].v;
		}
	}
	cout << sumV << endl;
	return 0;
}
0