基本都是背包问题,不熟悉的同学可以找《背包九讲》
3.3.1 Big Event in HDU
#include<stdio.h>
#include<memory.h>
using namespace std;
int dp[100000];
int main()
{
int n;
scanf("%d",&n);
while (n>=0)
{
int v[100],m[100],max=0;
for (int i=1;i<=n;i++)
{
scanf("%d%d",&v[i],&m[i]);
max+=v[i]*m[i];
}
memset(dp,0,sizeof(dp));
for (int i=1;i<=n;i++)
for (int j=max/2;j>=0;j--)
for (int k=0;k<=m[i];k++)
{
if (j<k*v[i]) break;
if (dp[j-k*v[i]]+k*v[i]>dp[j])
dp[j]=dp[j-k*v[i]]+k*v[i];
}
int a,b=0;
for (int j=0;j<=max/2;j++)
if (dp[j]>b) b=dp[j];
a=max-b;
printf("%d %d\n",a,b);
scanf("%d",&n);
}
return 0;
}
3.3.2 I love sneakers!
这题代码虽然AC了,不过好像有点问题,总感觉不太对,又检验不出来
#include<iostream>
#include<stdio.h>
using namespace std;
int ff[20][20000];
int main()
{
int ww[20][150],cc[20][150];
int v,n,i,j,k,q;
int t;
while(scanf("%d%d%d",&n,&v,&q)!=EOF)
{
for(i=1;i<=q;i++)
{
ww[i][0]=cc[i][0]=0;
}
for(i=0;i<n;i++)
{
scanf("%d",&t);
cc[t][0]++;
ww[t][0]++;
scanf("%d%d",&cc[t][cc[t][0]],&ww[t][ww[t][0]]);
}
for(i=0;i<=q;i++)
{
for(j=0;j<=v;j++)
ff[i][j]=-1;
}
for(i=0;i<=v;i++)
ff[0][i]=0;
for(k=1;k<=q;k++)
{
for(i=1;i<=cc[k][0];i++)
{
for(j=v;j>=cc[k][i];j--)
{
if(ff[k][j]<ff[k][j-cc[k][i]]+ww[k][i] && ff[k][j-cc[k][i]]!=-1)
ff[k][j]=ff[k][j-cc[k][i]]+ww[k][i];
if(ff[k][j]<ff[k-1][j-cc[k][i]]+ww[k][i] && ff[k-1][j-cc[k][i]]!=-1)
ff[k][j]=ff[k-1][j-cc[k][i]]+ww[k][i];
}
}
}
if(ff[q][v]==-1)
printf("Impossible\n");
else printf("%d\n",ff[q][v]);
}
return 0;
}
3.3.3 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
#include <iostream>
using namespace std;
int a[105][3],w[105][105];
int max(int x,int y)
{
if(x<y)
return y;
else
return x;
}
int main()
{
int c,n,m,i,j,temp;
while(cin>>c && c)
{
while(c--)
{
cin>>n>>m;
for(i=1;i<=m;i++)
cin>>a[i][0]>>a[i][1]>>a[i][2];
for(i=0;i<=m;i++)
w[i][0]=0;
for(i=0;i<=n;i++)
w[0][i]=0;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
w[i][j]=w[i-1][j];
temp=j/a[i][0];
if(temp>=1)
{
if(temp<=a[i][2])
{
w[i][j]=max(w[i][j],w[i-1][j-a[i][0]*temp]+a[i][1]*temp);
temp--;
while(temp>=1)
{
w[i][j]=max(w[i][j],w[i-1][j-a[i][0]*temp]+a[i][1]*temp);
temp--;
}
}
else
{
temp=a[i][2];
w[i][j]=max(w[i][j],w[i-1][j-a[i][0]*temp]+a[i][1]*temp);
temp--;
while(temp>=1)
{
w[i][j]=max(w[i][j],w[i-1][j-a[i][0]*temp]+a[i][1]*temp);
temp--;
}
}
}
}
}
cout<<w[m][n]<<endl;
}
}
return 0;
}
3.3.4 Ahui Writes Word
#include<stdio.h>
#include<string.h>
#include<memory.h>
using namespace std;
int f[20][20];
int dp[20000];
int main()
{
int n,c,a,b;
char *s;
while (scanf("%d%d",&n,&c)!=EOF)
{
memset(f,0,sizeof(f));
memset(dp,0,sizeof(dp));
for (int i=0;i<n;i++)
{
scanf("%s%d%d",&s,&a,&b);
f[a][b]++;
}
int i,ii,j,k;
for (i=0;i<=10;i++)
for (ii=0;ii<=10;ii++)
{
for (k=1;k<f[i][ii];f[i][ii]-=k,k*=2)
for (j=c;j>=0;j--)
if (k*ii>j) break;
else
if (dp[j-k*ii]+k*i>dp[j]) dp[j]=dp[j-k*ii]+k*i;
k=f[i][ii];
for (j=c;j>=0;j--)
if (k*ii>j) break;
else
if (dp[j-k*ii]+k*i>dp[j]) dp[j]=dp[j-k*ii]+k*i;
}
int ans=0;
for (j=0;j<=c;j++)
if (dp[j]>ans) ans=dp[j];
printf("%d\n",ans);
}
return 0;
}
3.3.5 Watch The Movie
#include<stdio.h>
#include<string.h>
int value[110] , time[110] , fine[101][10001] ;
int main ()
{
int cas , n , m , total_tim ;
scanf ( "%d" , &cas ) ;
while ( cas -- )
{
scanf ( "%d%d%d" , &n , &m , &total_tim ) ;
for ( int i = 0 ; i < n ; ++ i )
{
scanf ( "%d%d" , time+i , value+i ) ;
}
for ( int i = 0 ; i <= m ; i ++ )
for ( int j = 0 ; j <= total_tim ; j ++ )
{
fine[i][j] = -1 ;
if ( i == 0 ) fine[i][j] = 0 ;
}
for ( int i = 0 ; i < n ; i ++ )
for ( int k = m ; k >= 1 ; -- k )
for ( int j = total_tim ; j >= time[i] ; -- j )
{
if ( fine[k - 1][j-time[i]] != -1 )
if ( fine[k][j] < fine[k-1][j-time[i]] + value[i] )
fine[k][j] = fine[k-1][j-time[i]] + value[i] ;
}
if ( fine[m][total_tim] == -1 ) fine[m][total_tim] = 0 ;
printf ( "%d\n" , fine[m][total_tim] ) ;
}
return 0 ;
}
3.3.6饭卡
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int n,k[1001],f[1001],m;
while(scanf("%d",&n),n)
{
for(int i = 0; i < n; i++)
{
scanf("%d",&k[i]);
}
sort(k,k+n,cmp);
scanf("%d",&m);
if(m>=5)
{
memset(f,0,sizeof(f));
for(int i = 0; i < n-1; i++)
for(int j = m-5; j>=k[i];j--)
{
if(f[j-k[i]]+k[i]>f[j]&&f[j-k[i]]+k[i]<=m-5)
f[j] = f[j-k[i]]+k[i];
}
printf("%d\n",m-f[m-5]-k[n-1]);
}
else printf("%d\n",m);
}
return 0;
}
3.3.7和3.3.6一样的题目,hdu这个弄水了啊
3.3.8 Proud Merchants
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int f[5001];
typedef struct
{
int p,q,v;
}item;
bool cmp(item a,item b)
{
return a.q-a.p<b.q-b.p;
}
int main()
{
int n,m;
item it[501];
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d%d%d",&it[i].p,&it[i].q,&it[i].v);
}
sort(it,it+n,cmp);
memset(f,0,sizeof(f));
for(int i = 0; i < n; i++)
for(int j = m; j>=it[i].q;j--)
{
if(f[j-it[i].p]+it[i].v>f[j])
f[j] = f[j-it[i].p]+it[i].v;
}
printf("%d\n",f[m]);
}
return 0;
}