随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

题目

There are nn chests. The ii-th chest contains aiai coins. You need to open all nn chests in order from chest 11 to chest nn.

There are two types of keys you can use to open a chest:

  • a good key, which costs kk coins to use;
  • a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest ii will do ai=⌊ai2⌋ai=⌊ai2⌋, ai+1=⌊ai+12⌋,…,an=⌊an2⌋ai+1=⌊ai+12⌋,…,an=⌊an2⌋;
  • any key (both good and bad) breaks after a usage, that is, it is a one-time use.

You need to use in total nn keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.

During the process, you are allowed to go into debt; for example, if you have 11 coin, you are allowed to buy a good key worth k=3k=3 coins, and your balance will become −2−2 coins.

Find the maximum number of coins you can have after opening all nn chests in order from chest 11 to chest nn.

解答

注意初始化

#include <iostream>
using namespace std;
 const int N=2e5+4;
 #define int long long
 int inf=1e18;
 int a[N],n,K,w[34];
 int f[N][34];
 
 void solve(){
 	int i,j;
 	f[0][0]=0;
 	for(i=0;i<=n;i++)
 	 for(j=0;j<=32;j++) f[i][j]=-inf;
 	 f[0][0]=0;
 	 
 	for(i=1;i<=n;i++){
 		for(j=0;j<=32&&j<=i;j++){
 			f[i][j]=max(f[i][j],f[i-1][j]+a[i]/w[j]-K);
 			if(j>=1) f[i][j]=max(f[i][j],f[i-1][j-1]+a[i]/w[j]);
 			if(j>=31) f[i][j]=max(f[i][j],f[i-1][j]);
		 }
	 }
	 int ans=0;
	 for(j=0;j<=32;j++){
	 	ans=max(ans,f[n][j]);
	 }
	 cout<<ans<<endl;
 }
 signed main(){
 	w[0]=1;
 	int i,cas;
 	for(i=1;i<=32;i++) w[i]=w[i-1]*2;
 	cin>>cas;
 	
 	while(cas--){
 	cin>>n>>K;
 		for(i=1;i<=n;i++) cin>>a[i];
 		solve();
 	}
 }
 
 
 

posted on   towboat  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示