結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー 沙耶花沙耶花
提出日時 2022-07-30 00:15:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,457 bytes
コンパイル時間 5,255 ms
コンパイル使用メモリ 269,096 KB
実行使用メモリ 258,000 KB
最終ジャッジ日時 2023-09-27 00:36:46
合計ジャッジ時間 89,739 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 166 ms
258,000 KB
testcase_01 AC 209 ms
101,592 KB
testcase_02 AC 166 ms
101,644 KB
testcase_03 AC 947 ms
101,752 KB
testcase_04 AC 778 ms
101,780 KB
testcase_05 AC 314 ms
101,548 KB
testcase_06 AC 1,030 ms
101,776 KB
testcase_07 AC 820 ms
101,848 KB
testcase_08 AC 423 ms
101,584 KB
testcase_09 AC 1,149 ms
101,876 KB
testcase_10 AC 839 ms
101,876 KB
testcase_11 AC 1,089 ms
101,804 KB
testcase_12 AC 205 ms
101,592 KB
testcase_13 AC 1,097 ms
101,824 KB
testcase_14 AC 931 ms
101,844 KB
testcase_15 AC 175 ms
101,604 KB
testcase_16 AC 934 ms
101,804 KB
testcase_17 AC 736 ms
101,752 KB
testcase_18 AC 183 ms
101,556 KB
testcase_19 AC 791 ms
101,708 KB
testcase_20 AC 122 ms
101,700 KB
testcase_21 AC 685 ms
101,880 KB
testcase_22 AC 598 ms
101,628 KB
testcase_23 AC 918 ms
101,744 KB
testcase_24 AC 183 ms
101,720 KB
testcase_25 AC 268 ms
101,668 KB
testcase_26 AC 1,012 ms
101,928 KB
testcase_27 AC 569 ms
101,620 KB
testcase_28 AC 5,452 ms
104,496 KB
testcase_29 AC 5,693 ms
104,684 KB
testcase_30 AC 5,706 ms
104,608 KB
testcase_31 AC 5,575 ms
104,544 KB
testcase_32 AC 4,935 ms
104,040 KB
testcase_33 AC 5,921 ms
104,800 KB
testcase_34 AC 5,691 ms
104,548 KB
testcase_35 AC 4,742 ms
103,904 KB
testcase_36 AC 5,393 ms
104,440 KB
testcase_37 AC 5,461 ms
104,504 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<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;
				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] - (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,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