結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー 沙耶花沙耶花
提出日時 2022-07-30 00:04:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,298 bytes
コンパイル時間 5,081 ms
コンパイル使用メモリ 269,792 KB
実行使用メモリ 104,804 KB
最終ジャッジ日時 2023-09-27 00:18:09
合計ジャッジ時間 99,066 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 165 ms
101,820 KB
testcase_01 AC 209 ms
101,560 KB
testcase_02 AC 165 ms
101,532 KB
testcase_03 AC 966 ms
101,792 KB
testcase_04 AC 804 ms
101,812 KB
testcase_05 AC 318 ms
101,604 KB
testcase_06 AC 1,061 ms
101,856 KB
testcase_07 AC 828 ms
101,752 KB
testcase_08 AC 427 ms
101,632 KB
testcase_09 AC 1,171 ms
102,140 KB
testcase_10 AC 863 ms
101,812 KB
testcase_11 AC 1,137 ms
101,864 KB
testcase_12 AC 211 ms
101,604 KB
testcase_13 AC 1,162 ms
101,912 KB
testcase_14 AC 998 ms
101,904 KB
testcase_15 AC 175 ms
101,520 KB
testcase_16 AC 950 ms
101,848 KB
testcase_17 AC 769 ms
101,724 KB
testcase_18 AC 185 ms
101,552 KB
testcase_19 AC 820 ms
101,820 KB
testcase_20 AC 120 ms
101,680 KB
testcase_21 AC 701 ms
101,700 KB
testcase_22 AC 611 ms
101,664 KB
testcase_23 AC 942 ms
101,800 KB
testcase_24 AC 184 ms
101,592 KB
testcase_25 AC 274 ms
101,604 KB
testcase_26 AC 1,037 ms
101,836 KB
testcase_27 AC 582 ms
101,712 KB
testcase_28 AC 6,178 ms
104,372 KB
testcase_29 AC 6,433 ms
104,736 KB
testcase_30 AC 6,485 ms
104,652 KB
testcase_31 AC 6,290 ms
104,496 KB
testcase_32 AC 5,566 ms
104,300 KB
testcase_33 AC 6,754 ms
104,804 KB
testcase_34 AC 6,375 ms
104,660 KB
testcase_35 AC 5,350 ms
104,136 KB
testcase_36 AC 6,105 ms
104,352 KB
testcase_37 AC 6,124 ms
104,420 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,101,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