結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー 沙耶花沙耶花
提出日時 2022-07-30 00:06:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,297 bytes
コンパイル時間 4,871 ms
コンパイル使用メモリ 270,092 KB
実行使用メモリ 208,896 KB
最終ジャッジ日時 2023-09-27 00:21:34
合計ジャッジ時間 91,208 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 163 ms
208,896 KB
testcase_01 AC 204 ms
101,744 KB
testcase_02 AC 160 ms
101,572 KB
testcase_03 AC 932 ms
101,772 KB
testcase_04 AC 768 ms
101,812 KB
testcase_05 AC 305 ms
101,572 KB
testcase_06 AC 1,019 ms
101,860 KB
testcase_07 AC 804 ms
101,728 KB
testcase_08 AC 410 ms
101,548 KB
testcase_09 AC 1,129 ms
101,964 KB
testcase_10 AC 837 ms
101,712 KB
testcase_11 AC 1,077 ms
102,032 KB
testcase_12 AC 202 ms
101,576 KB
testcase_13 AC 1,136 ms
101,796 KB
testcase_14 AC 929 ms
102,020 KB
testcase_15 AC 171 ms
101,556 KB
testcase_16 AC 936 ms
101,836 KB
testcase_17 AC 746 ms
101,700 KB
testcase_18 AC 185 ms
101,480 KB
testcase_19 AC 810 ms
101,784 KB
testcase_20 AC 120 ms
101,604 KB
testcase_21 AC 679 ms
101,700 KB
testcase_22 AC 587 ms
101,656 KB
testcase_23 AC 924 ms
101,768 KB
testcase_24 AC 179 ms
101,556 KB
testcase_25 AC 265 ms
101,516 KB
testcase_26 AC 1,009 ms
102,004 KB
testcase_27 AC 563 ms
101,736 KB
testcase_28 AC 5,583 ms
104,364 KB
testcase_29 AC 5,897 ms
104,588 KB
testcase_30 AC 5,810 ms
104,564 KB
testcase_31 AC 5,509 ms
104,700 KB
testcase_32 AC 4,876 ms
104,096 KB
testcase_33 AC 5,996 ms
104,912 KB
testcase_34 AC 5,840 ms
104,804 KB
testcase_35 AC 4,765 ms
103,864 KB
testcase_36 AC 5,396 ms
104,348 KB
testcase_37 AC 5,494 ms
104,428 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
long long dp[2505][2505],ndp[2505][2505];
bool f[2505][2505];
vector<vector<long long>> get(vector<int> x,vector<int> c,vector<int> v,int C,int W){
	rep(i,C+1){
		rep(j,W+1)dp[i][j] = -Inf;
	}
	dp[0][0] = 0;
	
	rep(i,x.size()){
		//cout<<i<<endl;
		rep(j,C+1){
			rep(k,W+1){
				ndp[j][k] = -Inf;
				f[j][k] = 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);
	}
	vector<vector<long long>> ret(C+1,vector<long long>(W+1));
	rep(i,ret.size()){
		rep(j,ret[i].size())ret[i][j] = dp[i][j];
	}
	return ret;
	
}

int main() {
    
	int N;
	cin>>N;
	
	vector<int> c(N+1),v(N+1);
	rep(i,N){
		cin>>c[i+1]>>v[i+1];
		c[i+1] = min(c[i+1],N/(i+1));
		v[i+1] += 1000000000;
	}
	
	vector<int> xx,cc,vv;
	for(int i=1;i<=min(N,100);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=101;i<=N;i++){
		xx.push_back(i);
		cc.push_back(c[i]);
		vv.push_back(v[i]);
	}
	
	auto dpy = get(xx,cc,vv,26,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