当前位置:网站首页>CF1703G Good Key, Bad Key
CF1703G Good Key, Bad Key
2022-08-01 22:42:00 【秦小咩】
G. Good Key, Bad Key
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains two integers nn and kk (1≤n≤1051≤n≤105; 0≤k≤1090≤k≤109) — the number of chests and the cost of a good key respectively.
The second line of each test case contains nn integers aiai (0≤ai≤1090≤ai≤109) — the amount of coins in each chest.
The sum of nn over all test cases does not exceed 105105.
Output
For each test case output a single integer — the maximum number of coins you can obtain after opening the chests in order from chest 11 to chest nn.
Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
Example
input
Copy
5 4 5 10 10 3 1 1 2 1 3 12 10 10 29 12 51 5 74 89 45 18 69 67 67 11 96 23 59 2 57 85 60
output
Copy
11 0 13 60 58
Note
In the first test case, one possible strategy is as follows:
- Buy a good key for 55 coins, and open chest 11, receiving 1010 coins. Your current balance is 0+10−5=50+10−5=5 coins.
- Buy a good key for 55 coins, and open chest 22, receiving 1010 coins. Your current balance is 5+10−5=105+10−5=10 coins.
- Use a bad key and open chest 33. As a result of using a bad key, the number of coins in chest 33 becomes ⌊32⌋=1⌊32⌋=1, and the number of coins in chest 44 becomes ⌊12⌋=0⌊12⌋=0. Your current balance is 10+1=1110+1=11.
- Use a bad key and open chest 44. As a result of using a bad key, the number of coins in chest 44 becomes ⌊02⌋=0⌊02⌋=0. Your current balance is 11+0=1111+0=11.
At the end of the process, you have 1111 coins, which can be proven to be maximal.
=========================================================================
DP问题,常规的想即可,唯一需要注意的是,当坏钥匙用超过30次的时候,虽然再次使用坏钥匙无法降低物品价值,但是却可以比用好钥匙更优,这时候一定不能用好钥匙,否则就是花费k获得0的价值,dp[i][x]=......=..dp[i][32]=dp[i][31]=dp[i][30]统一把x设成35即可,代表超过30次坏钥匙的情况。
# include<iostream>
# include<deque>
using namespace std;
typedef long long int ll;
ll dp[100000+10][50];
ll b[100000+10];
ll a[100000+10][50];
int main ()
{
int t;
cin>>t;
while(t--)
{
ll n,k;
scanf("%lld%lld",&n,&k);
for(int i=1; i<=n; i++)
{
scanf("%lld",&b[i]);
}
for(int i=1; i<=n; i++)
{
a[i][0]=b[i];
for(int j=1; j<=35; j++)
{
a[i][j]=a[i][j-1]/2;
}
}
for(int i=1; i<=n; i++)
{
for(int j=0; j<=35; j++)
{
dp[i][j]=-1e18;
}
}
ll ans=-1e18;
for(int i=1; i<=n; i++)
{
for(int j=0; j<=35; j++)
{
if(j>=1)
dp[i][j]=max(dp[i-1][j]+a[i][j]-k,dp[i-1][j-1]+a[i][j]);
else
dp[i][j]=dp[i-1][j]+a[i][j]-k;
if(j==35)
dp[i][j]=max(dp[i][j],dp[i-1][35]);
if(i==n)
ans=max(ans,dp[n][j]);
}
}
cout<<ans<<endl;
}
return 0;
}
边栏推荐
- [Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
- vscode hide menu bar
- AQS
- 从0到100:招生报名小程序开发笔记
- Getting Started Database Days4
- 美赞臣EDI 940仓库装运订单详解
- 将vim与系统剪贴板的交互使用
- 罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
- y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
- img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
猜你喜欢

Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity

y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)

From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs

xctf attack and defense world web master advanced area web2

数据增强--学习笔记(图像类,cnn)

Today's sleep quality record 74 points

美赞臣EDI 940仓库装运订单详解

leetcode刷题

Still struggling with reporting tool selection?To take a look at this
![[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道](/img/6b/d4ff120493e878fcf5c9aa728eced7.png)
[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道
随机推荐
解决 win10 下 ISE14.7的 iMPACT 崩溃问题 - FPGA 笔记
别看了,这就是你的题呀
PHP算法之有效的括号
Today's sleep quality record 74 points
SQL Server(设计数据库--存储过程--触发器)
工程建筑行业数据中台指标分析
Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
小程序容器+自定义插件,可实现混合App快速开发
NgRx Store createSelector 的单步调试和源代码分析
Prufer sequence
From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
blender3.2.1 unit setting
[Recommended books] The first self-driving technology book
excel cell contian two words, seperated by a slash
联邦学习在金融领域的发展和应用
xctf attack and defense world web master advanced area webshell
Safe fifth after-school exercise
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
使用 Zokrates 在 BSV 上创建您的第一个 zkSNARK 证明
JS 数组去重(含简单数组去重、对象数组去重)