UESTC 1218 Pick The Sticks (dp )
2015-11-25 11:41
495 查看
http://acm.uestc.edu.cn/#/problem/show/1218
题目: 一根长为m的长木板和一些小木棒,每一根小木棒有它的长度和价值,这些小木棒要放在长木板上并且每一根小木棒的重心要在长木板上(可以露出一半的长),最大价值是多少。
让人联想到01背包,但是又有些许的不同。
注意: 那个双重循环里的第二层循环如果写成 for(int j=a[i]/2;j<=len;j++) //这种遍历会造成数据污染(滚动数组),除非再开一维。另外,因为输出格式不对,%lld--写成-->%I64d 居然出现了(Restricted Function on test 1)瞬间惊呆了我。
题目: 一根长为m的长木板和一些小木棒,每一根小木棒有它的长度和价值,这些小木棒要放在长木板上并且每一根小木棒的重心要在长木板上(可以露出一半的长),最大价值是多少。
让人联想到01背包,但是又有些许的不同。
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=4e3+10; typedef long long LL; LL dp [5]; // 0,1,2分别表示露在外面的木棍数量 LL a[1010],v[1010]; int main() { //freopen("cin.txt","r",stdin); LL n,t=0,m,len; cin>>n; while(n--){ scanf("%lld%lld",&m,&len); len=len*2; LL ans=0; for(int i=0;i<m;i++){ scanf("%lld%lld",&a[i],&v[i]); a[i]=a[i]*2; ans=max(ans,v[i]); } memset(dp,0,sizeof(dp)); for(int i=0;i<m;i++){ for(int j=len;j>=a[i]/2;j--){ dp[j][1]=max(dp[j-a[i]/2][0]+v[i],dp[j][1]); // outside dp[j][2]=max(dp[j-a[i]/2][1]+v[i],dp[j][2]); // 不放就仍然使用原值 if(j>=a[i]){ dp[j][0]=max(dp[j-a[i]][0]+v[i],dp[j][0]); //inside dp[j][1]=max(dp[j-a[i]][1]+v[i],dp[j][1]); dp[j][2]=max(dp[j-a[i]][2]+v[i],dp[j][2]); } } } for(int i=0;i<3;i++){ ans=max(ans,dp[len][i]); } printf("Case #%lld: %lld\n",++t,ans); } return 0; }
注意: 那个双重循环里的第二层循环如果写成 for(int j=a[i]/2;j<=len;j++) //这种遍历会造成数据污染(滚动数组),除非再开一维。另外,因为输出格式不对,%lld--写成-->%I64d 居然出现了(Restricted Function on test 1)瞬间惊呆了我。
相关文章推荐
- UIView 翻转动画
- bluetooth handfree client test on console
- UIButton 点击后变灰
- TinyOS论文03:Bugs or Anomalies? Sequence Mining based Debugging in Wireless Sensor Networks
- Fluent API 配置
- UITextView - 3
- [转]Ionic – Mobile UI Framework for PhoneGap/Cordova Developers
- AS2.0--加速你的开发build速度
- libubox - uloop runqueue ustream
- 用Pymel里的with来些UI
- ElasticsSearch——Query String
- UIBarButtonSystemItem样式
- 更换免开发证书账号出现的Your build settings specify a provisioning profile with the UUID"",however,no such provisioning profile was found
- vue.js学习笔记
- 2.AsyncQueryHandler、内容提供者
- R12.2.0 buildStage 运行结果
- Bringing up interface eth0: Error: No suitable device found: no device found for connection 'System
- UCGUI移植
- 这三种特殊权限(suid、sgid、sticky)
- UIBarButtonItem (系统 Item 样式)