您的位置:首页 > 其它

SDAU训练日志第17篇----------动态规划(13)(2018年2月23日)

2018-02-23 18:36 288 查看
DP这是最后一篇了吧,今明两天开始弄二分。把剩下的小尾巴放上来。
HDU2955
Problem Description
The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.

For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.

His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
 
Input
The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj . 
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
 
Output
For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.

Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
 
Sample Input
3
0.04 3
1 0.02
2 0.03
3 0.05
0.06 3
2 0.03
2 0.03
3 0.05
0.10 3
1 0.03
2 0.02
3 0.05

Sample Output
2
4
6


关键思路

转换思维,如果按照常规思路,将被抓概率作为容量,金币作为总价值,这样是不可行的,因为被抓概率不离散,用数组没法表示,变一下思路,问题转换成同样的金币求最大生还概率,然后在被抓的临界值输出金币数就行了。
最后输出时按照生还概率降序,一旦生还概率符合条件就输出金币数。
状态转移方程就是普通的0-1背包的写法
dp[j] = max(dp[j],dp[j-Bag[i].v]*(1-Bag[i].p));

HDU OJ好像根本不能把自己的AC代码调出来,这个就很坑了,只能把别人网上的AC代码发出来了#include<stdio.h>
#include<string.h>
using namespace std;

struct bag
{
int v;
double p;
}Bag[10010];
double dp[10010];

int main()
{
int T,N;
double p;
scanf("%d",&T);
while(T--)
{
scanf("%lf %d",&p,&N);
int sum = 0;
for(int i = 0; i < N; i++)
{
scanf("%d%lf",&Bag[i].v,&Bag[i].p);
sum += Bag[i].v;
}
memset(dp,0,sizeof(dp));
dp[0] = 1;
for(int i = 0; i < N; i++)
{
for(int j = sum; j >= Bag[i].v; j--)
{
dp[j] = max(dp[j],dp[j-Bag[i].v]*(1-Bag[i].p));
}
}

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

第二个题



关键点:
1.题目给的数据是精确到小数点后两位的,要乘上100,再进行计算,结果除100进行输出
2.并不是所有的发票都能报销,进行筛选存储

1. 发票中只能有A或B或C 这3类物品,比如样例里有类型为X的发票
2.一张发票的额度<=1000.
3. A BC三类物品的额度<=600.
3.问题核心还是0-1背包问题
上代码:(参考了focus_best的博客题解代码)#include<iostream>
using namespace std;
int dp[30*1000*100+5];
int val[35];
int max_val;
int main()
{
int n=0,cnt=0;
double v=0;
while(cin>>v>>n&&n)
{
max_val=int(100*v);
cnt=0;
for(int i=1;i<=n;i++)
{
bool flag=true;
int m=0;
cin>>m;
char type;
double val_A=0,val_B=0,val_C=0,v=0;
while(m--)
{

cin>>type;
cin.get();
cin>>v;
if(type=='A')
val_A+=v;
else if(type=='B') val_B+=v;
else if(type=='C') val_C+=v;
else flag=false;
}
if(flag&&(val_A<=600)&&(val_B<=600)&&(val_C<=600)&&(val_A+val_B+val_C<=1000))
val[++cnt]=int((val_A+val_B+val_C)*100);
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=cnt;i++)
for(int j=max_val;j>=val[i];j--)
dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
printf("%.2lf\n",(dp[max_val])/100.0 );
}
}
DP就暂时到此为止了,还有很多高深的DP没学,以后有空再自学吧,
这两天搞二分算法。
今天看了一些题,二分好像和搜索还有不少关系,搞不好还得扩展学习一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: