結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー 沙耶花沙耶花
提出日時 2022-07-30 00:17:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,386 bytes
コンパイル時間 5,398 ms
コンパイル使用メモリ 271,204 KB
実行使用メモリ 104,904 KB
最終ジャッジ日時 2023-09-27 00:39:30
合計ジャッジ時間 86,026 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 157 ms
101,636 KB
testcase_01 AC 195 ms
101,744 KB
testcase_02 AC 155 ms
101,736 KB
testcase_03 AC 859 ms
101,916 KB
testcase_04 AC 713 ms
101,668 KB
testcase_05 AC 289 ms
101,632 KB
testcase_06 AC 938 ms
101,804 KB
testcase_07 AC 744 ms
101,932 KB
testcase_08 AC 391 ms
101,724 KB
testcase_09 AC 1,053 ms
101,948 KB
testcase_10 AC 786 ms
101,700 KB
testcase_11 AC 1,021 ms
101,836 KB
testcase_12 AC 196 ms
101,464 KB
testcase_13 AC 1,037 ms
101,852 KB
testcase_14 AC 852 ms
101,980 KB
testcase_15 AC 164 ms
101,528 KB
testcase_16 AC 836 ms
101,816 KB
testcase_17 AC 685 ms
101,776 KB
testcase_18 AC 176 ms
101,464 KB
testcase_19 AC 734 ms
101,660 KB
testcase_20 AC 118 ms
101,488 KB
testcase_21 AC 633 ms
101,892 KB
testcase_22 AC 549 ms
101,608 KB
testcase_23 AC 852 ms
101,776 KB
testcase_24 AC 174 ms
101,476 KB
testcase_25 AC 256 ms
101,488 KB
testcase_26 AC 926 ms
101,808 KB
testcase_27 AC 533 ms
101,580 KB
testcase_28 AC 4,959 ms
104,368 KB
testcase_29 AC 5,275 ms
104,700 KB
testcase_30 AC 5,256 ms
104,820 KB
testcase_31 AC 5,170 ms
104,484 KB
testcase_32 AC 4,616 ms
104,076 KB
testcase_33 AC 5,493 ms
104,904 KB
testcase_34 AC 5,258 ms
104,596 KB
testcase_35 AC 4,404 ms
103,992 KB
testcase_36 AC 4,873 ms
104,428 KB
testcase_37 AC 5,420 ms
104,372 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

#pragma target("avx2")
#pragma optimize("O3")
#pragma optimize("unroll-loops")

long long dp[2505][2505],ndp[2505][2505];
bool f[2505][2505];
vector<vector<long long>> get(vector<int> x,vector<int> c,vector<long long> 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;
				if(k+x[i]>W){
					ndp[j][k] = max(ndp[j][k],dp[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] - v[i] * jj)D.pop_back();
						D.emplace_back(dp[jj][kk] - v[i] * jj, jj);
					}
					
					if(D.size()>0)ndp[jj][kk] = max(ndp[jj][kk],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);
	vector<long long> 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;
	vector<long long> 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)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