# [2016/7/28][dp]动态规划入门练习题

ROBBER:http://acm.hdu.edu.cn/showproblem.php?pid=2955

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
typedef struct{
int money;
double p;
}Node;
Node r[10003];
int T;
double P;
int N;
int Mj;
double dp[10003];
int main(){
cin>>T;
while(T--){
cin>>P>>N;
int sum = 0;
for(int i = 1;i<=N;i++){
cin>>r[i].money>>r[i].p;
sum += r[i].money;
}
dp[0] = 1;//不被抓概率

for(int i = 1;i<=N;i++)
for(int j = sum;j>=r[i].money;j--)
dp[j] = max(dp[j-r[i].money]*(1-r[i].p),dp[j]);

for(int i = sum;i>=0;i--){
if((1-dp[i])<=P){
printf("%d\n",i);
break;
}
}
memset(dp,0,sizeof(dp));
}
return 0;
}

1.输入格式问题。scanf中%*+数据类型，意为输入的时候跳过此数据类型。还有规定格式输入。

2.因为都是两位double，所以要×100转换为int，才可转换为背包问题。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
typedef struct{
int a;
int b;
int c;
int total;
}Node;
Node shu[33];
int dp[3000003];
int main(){
double Q;
int N;
cin>>Q>>N;
while(N!=0){
//	cout<<"2333"<<endl;
Q *= 100;
char r;
double money;
int ci;
Node A;
int index = 0;
for(ci = 1;ci<=N;ci++){
A.a = 0;
A.b = 0;
A.c = 0;
A.total = 0;
int i;
int num;
scanf("%d",&num);
bool flag = true;
for(i = 1;i<=num;i++){
scanf("%*c%c:%lf",&r,&money);
//printf("%c %lf\n",r,money);
money*=100;
if(r=='A')
A.a += (int)money;
else if(r=='B')
A.b += (int)money;
else if(r=='C')
A.c += (int)money;
else
flag = false;
}
if(A.a>60000||A.b>60000||A.c>60000)
flag = false;
A.total = A.a+A.b+A.c;
if(A.total>100000)
flag = false;
if(flag){
shu[++index] = A;
//	cout<<index<<endl;
}
// 	printf("%d\n",flag);
}
for(int i = 1;i<=index;i++)
for(int j = (int)Q;j>=shu[i].total;j--)
dp[j] = max(dp[j-shu[i].total]+shu[i].total,dp[j]);

printf("%.2lf\n",(double)dp[(int)Q]/100.00);
memset(dp,0,sizeof(dp));
memset(shu,0,sizeof(shu));
cin>>Q>>N;
}
return 0;

}

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
int N;
int num[10004];
int dp[10004];
int len[10004];
int main(){
scanf("%d",&N);
while(N!=0){
bool flag = false;
for(int i = 1;i<=N;i++){
scanf("%d",&num[i]);
if(num[i]>=0)
flag = true;
}
int ma = -99999999;
if(!flag){
cout<<0<<" "<<num[1]<<" "<<num[N]<<endl;
cin>>N;
continue;
}
else{
dp[0] = -99999999;
int sum = 0;
for(int i = 1;i<=N;i++){
if(dp[i-1]<=0){
dp[i] = num[i];
len[i] = 0;
}
else{
dp[i] = num[i] + dp[i-1];
len[i] = len[i-1]+1;
}
ma = max(dp[i],ma);
}
}
//	printf("2333\n");
int end = 99;
for(int i = 1;i<=N;i++){
if(dp[i]==ma){
end = i;
break;
}
}
//	cout<<end<<endl;
//	printf("23333\n");
while(num[end]==0&&len[end]!=0)
end--;

//	printf("2333");
cout<<dp[end]<<" "<<num[end-len[end]]<<" "<<num[end]<<endl;
//	cout<<233;
memset(len,0,sizeof(len));
memset(dp,0,sizeof(dp));
cin>>N;
}

return 0;
}
max sum http://acm.hdu.edu.cn/showproblem.php?pid=1003

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
int N;
int num[100004];
int dp[100004];
int len[100004];
int main(){
scanf("%d",&N);
int m = N;
int mar = 0;
while(N--){
int ma = -99999999;
dp[0] = -99999999;
int n;
scanf("%d",&n);
for(int i = 1;i<=n;i++)
scanf("%d",&num[i]);
for(int i = 1;i<=n;i++){
if(dp[i-1]<0){
dp[i] = num[i];
len[i] = 0;
}
else{
dp[i] = num[i] + dp[i-1];
len[i] = len[i-1]+1;
}
ma = max(dp[i],ma);
}
int end = 99;
for(int i = 1;i<=n;i++){
if(dp[i]==ma){
end = i;
break;
}
}
//	while(num[end]==0&&len[end]!=0)
//		end--;
mar++;
cout<<"Case "<<mar<<":"<<endl;
cout<<dp[end]<<" "<<end-len[end]<<" "<<end<<endl;
memset(len,0,sizeof(len));
memset(dp,0,sizeof(dp));
if(mar!=m)
printf("\n");
}
return 0;

}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120