結果

問題 No.626 Randomized 01 Knapsack
ユーザー ats5515ats5515
提出日時 2017-12-16 20:45:01
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,165 bytes
コンパイル時間 878 ms
コンパイル使用メモリ 86,904 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 04:15:57
合計ジャッジ時間 9,424 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
struct T {
	int v;
	int w;
	bool operator<(const T& right) const {
		return  v * right.w > right.v * w;
	}
};
struct State {
	int v;
	int w;
	//vector<int> goods;
	bool operator<(const State& right) const {
		return  v * right.w < right.v * w;
	}
};
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, W;
	cin >> N >> W;
	vector<T> A(N);
	
	for (int i = 0; i < N; i++) {
		cin >> A[i].v >> A[i].w;
	}
	sort(A.begin(), A.end());
	int w = 0;
	int v = 0;
	priority_queue<State> pq;
	priority_queue<State> npq;
	State st;
	st.v = 0;
	st.w = 0;
	pq.push(st);
	int res = -1;
	for (int i = 0; i < N; i++) {
		while ((int)npq.size() > 0)npq.pop();
		for (int j = 0; j < 1000; j++) {
			if (pq.size() == 0) break;
			st = pq.top(); pq.pop();
			npq.push(st);
			st.v += A[i].v;
			st.w += A[i].w;
			if (st.w <= W) {
				res = max(res, st.v);
				npq.push(st);
			}
		}
		swap(pq, npq);
	}
	
	cout << res << endl;
}
0