POJ 1276 Cash Machine
生活随笔
收集整理的這篇文章主要介紹了
POJ 1276 Cash Machine
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很容易看出來是一個背包問題,開始把每一張鈔票都跑了一遍01背包,直接TLE了。
其實就是多重背包模板題。
1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <utility> 4 #include <vector> 5 #include <cstring> 6 using namespace std; 7 #define fst first 8 #define scd second 9 #define pb(x) push_back((x)) 10 #define mkp(x,y) make_pair((x),(y)) 11 #define ist(x) insert((x)) 12 typedef long long ll; 13 typedef pair<int ,int > pii; 14 typedef pair<ll ,ll > pll; 15 typedef vector< int > vi; 16 ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);} 17 ll qPow(ll a,ll b,ll mod){ ll ret=1ll;while(b){ if(b&1) ret=ret*a%mod;a=a*a%mod;b>>=1;} return ret; } 18 19 pii nD[13]; 20 int dp[100000+5]; 21 int nValue; 22 23 void ZeroOnePack(int cost , int weight){ 24 for(int i=nValue;i>=cost;i--) 25 dp[i]=max(dp[i],dp[i-cost]+weight); 26 } 27 28 void CompletePack(int cost , int weight){ 29 for(int i=cost;i<=nValue;++i) 30 dp[i]=max(dp[i],dp[i-cost]+weight); 31 } 32 33 void MultiplePack(int cost ,int weight,int amount){ 34 if(cost*amount>=nValue) CompletePack(cost , weight); 35 else{ 36 int k=1; 37 while(k<amount){ 38 ZeroOnePack(k*cost,k*weight); 39 amount-=k; 40 k<<=1; 41 } 42 ZeroOnePack(amount*cost,amount*weight); 43 } 44 } 45 46 int main(){ 47 int cash,N; 48 while(cin>>cash>>N){ 49 nValue=cash; 50 memset(dp,0,sizeof(dp)); 51 for(int i=0;i<N;++i) 52 cin>>nD[i].first>>nD[i].second; 53 for(int i=0;i<N;++i){ 54 MultiplePack(nD[i].second,nD[i].second,nD[i].first); 55 } 56 cout<<dp[cash]<<endl; 57 } 58 return 0; 59 } View Code?
轉載于:https://www.cnblogs.com/Kiritsugu/p/9474752.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的POJ 1276 Cash Machine的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看环境变量有无配置成功等命令操作
- 下一篇: 模块(sys/os/序列化模块)