当前位置:网站首页>Explanation report of the explosion
Explanation report of the explosion
2022-07-05 15:25:00 【wch(】
The explosion's runes and cards are full of explanation reports
label : Dynamic programming
source : Cattle from 
Their thinking :
The obvious knapsack problem
But observe again a,b Data range of , I can't drive that big dp Array
This inspires us to optimize , Because the title requires magic, the total cost is k Multiple
We can spend magic power on every data k modulus
Use dp[][j] Save directly in After taking the mold, the sum of magic is j The greatest power of time
Then the dynamic transfer equation :
dp[i][j] = max(dp[i-1][j],dp[i-1][((j-a)%k+k)%k]+b);
After doing this, I found that it is the same as the problem of nine people and nine doors in nine hours , It's a pity that I didn't do it during the game
Code implementation :
#include <bits/stdc++.h>
using namespace std;
long long dp[1050][1050];
int main(){
int n,k;
cin>>n>>k;
int a,b;
memset(dp,-0x3f,sizeof(dp));
dp[0][0]=0;
for(int i=1;i<=n;i++){
cin>>a>>b;
for(int j=0;j<k;j++){
dp[i][j] = max(dp[i-1][j],dp[i-1][((j-a)%k+k)%k]+b);
}
}
long long ans = dp[n][0];
if(ans<=0) ans=-1;
cout<<ans<<endl;
return 0;
}
边栏推荐
猜你喜欢
![[JVM] operation instruction](/img/f5/85580495474ef58eafbb421338e93f.png)
[JVM] operation instruction

Ionic Cordova project modification plug-in

I include of spring and Autumn

Talk about your understanding of microservices (PHP interview theory question)

你童年的快乐,都是被它承包了

亿咖通科技通过ISO27001与ISO21434安全管理体系认证

Bugku's Ah Da
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps

Common MySQL interview questions

sql server学习笔记
随机推荐
Bugku easy_ nbt
mapper.xml文件中的注释
机器学习笔记 - 灰狼优化
Interpretation of Apache linkage parameters in computing middleware
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
R 熵权法计算权重及综合得分
Your childhood happiness was contracted by it
CODING DevSecOps 助力金融企业跑出数字加速度
MySQL表字段调整
Common interview questions about swoole
[JVM] operation instruction
Database learning - Database Security
keep-alive
SQL Server learning notes
queryRunner. Query method
sql server学习笔记
Huawei Hubble incarnation hard technology IPO harvester
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
Cartoon: programmers don't repair computers!
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟