結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー 沙耶花沙耶花
提出日時 2022-07-29 23:49:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,077 bytes
コンパイル時間 5,118 ms
コンパイル使用メモリ 272,028 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2023-09-26 23:45:27
合計ジャッジ時間 23,353 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 17 ms
4,376 KB
testcase_04 AC 11 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 20 ms
4,380 KB
testcase_07 AC 12 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 26 ms
4,380 KB
testcase_10 AC 13 ms
4,376 KB
testcase_11 AC 24 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 25 ms
4,376 KB
testcase_14 AC 17 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 17 ms
4,380 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 12 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 9 ms
4,380 KB
testcase_22 AC 7 ms
4,380 KB
testcase_23 AC 16 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 20 ms
4,380 KB
testcase_27 AC 6 ms
4,380 KB
testcase_28 AC 560 ms
6,724 KB
testcase_29 AC 610 ms
7,052 KB
testcase_30 AC 615 ms
7,104 KB
testcase_31 AC 591 ms
6,904 KB
testcase_32 AC 495 ms
6,252 KB
testcase_33 AC 683 ms
7,424 KB
testcase_34 AC 605 ms
7,196 KB
testcase_35 AC 433 ms
6,088 KB
testcase_36 AC 545 ms
6,696 KB
testcase_37 AC 559 ms
6,788 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000

vector<vector<long long>> get(vector<int> x,vector<int> c,vector<int> v,int C,int W){
	
	vector dp(C+1,vector<long long>(W+1,-Inf));
	dp[0][0] = 0;
	
	rep(i,x.size()){
		//cout<<i<<endl;
		vector ndp(C+1,vector<long long>(W+1,-Inf));
		vector f(C+1,vector<bool>(W+1,false));
		
		rep(j,C+1){
			rep(k,W+1){
				
				if(f[j][k])continue;
				deque<pair<long long,int>> D;
				int jj = j,kk = k;
				while(true){
					if(jj>C||kk>W)break;
					f[jj][kk] = true;
					while(D.size()>0&&jj - D.front().second > c[i])D.pop_front();
					if(dp[jj][kk]!=-Inf){
						while(D.size()>0 && D.back().first<=dp[jj][kk] - (long long)v[i] * jj)D.pop_back();
						D.emplace_back(dp[jj][kk] - (long long)v[i] * jj, jj);
					}
					
					if(D.size()>0)ndp[jj][kk] = max(ndp[jj][kk],(long long)v[i]*jj + D.front().first);
					
					jj++;
					kk+=x[i];
				}
				
			}
		}
		swap(dp,ndp);
	}
	return dp;
	
}

int main() {
    
	int N;
	cin>>N;
	
	vector<int> c(N+1),v(N+1);
	rep(i,N){
		cin>>c[i+1]>>v[i+1];
		v[i+1] += 1000000000;
	}
	
	vector<int> xx,cc,vv;
	for(int i=1;i<=min(N,50);i++){
		xx.push_back(i);
		cc.push_back(c[i]);
		vv.push_back(v[i]);
	}
	//cout<<'a'<<endl;
	auto dpx = get(xx,cc,vv,N+1,N+1);
	
	xx.resize(0),cc.resize(0),vv.resize(0);
	for(int i=51;i<=N;i++){
		xx.push_back(i);
		cc.push_back(c[i]);
		vv.push_back(v[i]);
	}
	
	auto dpy = get(xx,cc,vv,51,N+1);
	
	/*rep(i,dpx.size()){
		rep(j,N+1){
			cout<<dpx[i][j]<<',';
		}
		cout<<endl;
	}*/
	rep(i,dpx.size()){
		rep(j,N)dpx[i][j+1] = max(dpx[i][j+1],dpx[i][j]);
	}
	rep(i,dpy.size()){
		rep(j,N)dpy[i][j+1] = max(dpy[i][j+1],dpy[i][j]);
	}
	vector<long long> ans(N+1,-Inf);
	rep(i,dpx.size()){
		rep(j,dpy.size()){
			if(i+j>N)break;
			rep(k,N+1){
				ans[i+j] = max(ans[i+j],dpx[i][k] + dpy[j][N-k]);
			}
		}
	}
	rep(i,N){
		ans[i+1] -= 1000000000LL * (i+1);
		cout<<ans[i+1]<<endl;
	}
				
	
    return 0;
}
0