标签:des style blog http io ar color os sp
Description
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<iostream> using namespace std; int main() { int N,V,T; int w[1005]; int c[1005]; int i,j,k; scanf("%d",&T); while(T--) { int f[1005]={0}; scanf("%d%d",&N,&V); for(i=0;i<N;i++) { scanf("%d",&w[i]); } for(i=0;i<N;i++) { scanf("%d",&c[i]); } for(i=0;i<N;i++) { // printf("\nc[%d]=%d\tw[%d]=%d\n",i,c[i],i,w[i]); for(int v=V;v>=c[i];v--)// 剩下的体积要大于想放入的体积 { f[v]=max(f[v],f[v-c[i]]+w[i]); // printf("v=%d\tf[%d]=%d\n",v,v,f[v]); } } } return 0; } /* 1 5 10 2 3 4 5 1 4 3 2 1 5*/
Description
Bessie has gone to the mall‘s jewelry store and spies a charm bracelet. Of course, she‘d like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a ‘desirability‘ factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
4 6 1 4 2 6 3 12 2 7
Sample Output
23
#include<cstdio> #include<iostream> using namespace std; int main() { int N,M; int W[3500],D[3500]; int i,j,k; while(scanf("%d%d",&N,&M)!=EOF) { int f[12885]={0}; for(i=0;i<N;i++) { scanf("%d%d",&W[i],&D[i]); } for(i=0;i<N;i++) { for(j=M;j>=W[i];j--) { // if(f[j]<f[j-W[i]]+D[i]) // f[j]=f[j-W[i]]+D[i]; f[j]=max(f[j],f[j-W[i]]+D[i]);// 注意这里是加上第i的价值哦,不是j; // printf("j=%d\tf[%d]=%d\n",j,j,f[j]); } } printf("%d\n",f[M]); } return 0; } /* 4 6 1 4 2 6 3 12 2 7 */
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12642 Accepted Submission(s): 4383
#include<cstdio> #include<iostream> using namespace std; int main() { int n,i,j,m; int c[1005]; int sum,mmax; while(scanf("%d",&n),n) { sum=0; mmax=0; int f[1005]= {0}; for(i=0; i<n; i++) { scanf("%d",&c[i]); sum+=c[i]; mmax=max(mmax,c[i]); } scanf("%d",&m); if(m<5) { printf("%d\n",m); } else if(sum<=m) printf("%d\n",m-sum); else { int t=0; for(i=0; i<n; i++) { if(c[i]==mmax) { c[i]=0; t=1; } if(t) break; } for(i=0; i<n; i++) { for(j=m-5; j>=c[i]; j--) { f[j]=max(f[j],f[j-c[i]]+c[i]); } } printf("%d\n",m-mmax-f[m-5]); } } return 0; }
Description
CD |
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most out of tape space and have as short unused space as possible.
Assumptions:
Program should find the set of tracks which fills the tape best and print it in the same sequence as the tracks are stored on the CD
5 3 1 3 4 10 4 9 8 4 2 20 4 10 5 7 4 90 8 10 23 1 2 3 4 5 7 45 8 4 10 44 43 12 9 8 2
1 4 sum:5 8 2 sum:10 10 5 4 sum:19 10 23 1 2 3 4 5 7 sum:55 4 10 12 9 8 2 sum:45
#include<cstdio> #include<iostream> using namespace std; int main() { int N,tracks,i,j; int time[25]; while(scanf("%d%d",&N,&tracks)!=EOF) { int dp[1005]= {0}; int vis[25][1005]={0}; for(i=0; i<tracks; i++) { scanf("%d",&time[i]); } for(i=tracks-1; i>=0; i--) { for(j=N; j>=time[i]; j--) { if(dp[j]<dp[j-time[i]]+time[i]) { dp[j]=dp[j-time[i]]+time[i]; vis[i][j]=1; // printf("vis[%d][%d]\tdp[%d]=%d\n",i,j,j,dp[j]); } } } for(i=0,j=dp[N];i<tracks&&j>0;i++) { if(vis[i][j]) { // printf("vis[%d][%d]=%d\tdp[%d]=%d\t time[%d]=%d\n",i,j,vis[i][j],j,dp[j],i,time[i]); printf("%d ",time[i]); j-=time[i]; } } printf("sum:%d\n",dp[N]); } return 0; }
#include<cstdio> #include<iostream> using namespace std; int mmax; int main() { int N,tracks,i,j; int time[25]; while(scanf("%d%d",&N,&tracks)!=EOF) { for(i=0; i<tracks; i++) { scanf("%d",&time[i]); // printf("%d \t",time[i]); } // printf("\n"); mmax=(1<<tracks)-1; // printf("mmax=%d\n",mmax); int p=0,sum,t,temp=0; for(i=mmax; i>0; i--) { t=0; sum=0; // printf("i= %d\n",i); for(j=i; j>0;) { if(j%2) sum+=time[t]; // printf("sum= %d \t",sum); j/=2; t++; } if(sum<=N&&temp<=sum) { temp=sum; p=i; } } t=0; for(j=p; j>0;) { if(j%2) printf("%d ",time[t]); j/=2; t++; } printf("sum:%d\n",temp); } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/yuyixingkong/p/4166087.html