結果

問題 No.2617 容量3のナップザック
ユーザー ゆにぽけゆにぽけ
提出日時 2024-01-26 23:34:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 410 ms / 2,000 ms
コード長 1,815 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 138,160 KB
実行使用メモリ 90,820 KB
最終ジャッジ日時 2024-01-26 23:34:20
合計ジャッジ時間 7,579 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 184 ms
42,220 KB
testcase_13 AC 136 ms
30,616 KB
testcase_14 AC 166 ms
37,136 KB
testcase_15 AC 91 ms
20,296 KB
testcase_16 AC 199 ms
48,748 KB
testcase_17 AC 136 ms
29,956 KB
testcase_18 AC 123 ms
26,288 KB
testcase_19 AC 23 ms
7,900 KB
testcase_20 AC 124 ms
29,664 KB
testcase_21 AC 87 ms
20,888 KB
testcase_22 AC 141 ms
30,264 KB
testcase_23 AC 168 ms
36,988 KB
testcase_24 AC 8 ms
6,548 KB
testcase_25 AC 112 ms
26,012 KB
testcase_26 AC 217 ms
56,760 KB
testcase_27 AC 71 ms
18,204 KB
testcase_28 AC 6 ms
6,548 KB
testcase_29 AC 60 ms
16,140 KB
testcase_30 AC 133 ms
32,304 KB
testcase_31 AC 147 ms
33,100 KB
testcase_32 AC 157 ms
36,748 KB
testcase_33 AC 183 ms
40,632 KB
testcase_34 AC 200 ms
42,732 KB
testcase_35 AC 169 ms
36,936 KB
testcase_36 AC 410 ms
90,820 KB
testcase_37 AC 201 ms
44,964 KB
testcase_38 AC 153 ms
36,468 KB
testcase_39 AC 209 ms
43,776 KB
testcase_40 AC 178 ms
40,224 KB
testcase_41 AC 94 ms
36,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
void Main()
{
	int N,K;
	long long s,a,b,m;
	cin >> N >> K >> s >> a >> b >> m;
	vector<long long> f(2 * N);
	f[0] = s;
	for(int i = 0;i + 1 < 2 * N;i++)
	{
		f[i + 1] = (a * f[i] + b) % m;
	}
	vector<vector<long long>> G(4);
	for(int i = 0;i < N;i++)
	{
		int w = f[i] % 3 + 1;
		long long v = w * f[N + i];
		G[w].push_back(v);
	}
	for(int i = 1;i <= 3;i++)
	{
		if(i == 1)
		{
			while(G[i].size() < 3 * K)
			{
				G[i].push_back(0ll);
			}
		}
		else
		{
			while(G[i].size() < K)
			{
				G[i].push_back(0ll);
			}
		}
		sort(G[i].rbegin(),G[i].rend());
	}
	long long ans = 0;
	long long S3 = 0;
	vector<vector<long long>> S(3);
	for(int i = 1;i <= 2;i++)
	{
		S[i].resize(G[i].size() + 1);
		for(int j = 0;j < (int)G[i].size();j++)
		{
			S[i][j + 1] = S[i][j] + G[i][j];
		}
	}
	for(int i = 0;i <= K;i++)
	{
		long long cur = S3;
		int ng = -1,ok = K - i;
		while(ok - ng > 1)
		{
			int mid = (ok + ng) / 2;
			long long df = G[2][mid] - (G[1][3 * (K - i) - 2 * mid - 2] + G[1][3 * (K - i) - 2 * mid - 1]);
			if(df <= 0)
			{
				ok = mid;
			}
			else
			{
				ng = mid;
			}
		}
		cur += S[2][ok] + S[1][3 * (K - i) - 2 * ok];
		ans = max(ans,cur);
		if(i < K)
		{
			S3 += G[3][i];
		}
	}
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0