第十二天 动态规划

第十二天 动态规划

1

Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newfound enemy was overwhelming. Soon, mankind was driven to the brink of extinction. Luckily, the surviving humans managed to build three walls: Wall Maria, Wall Rose and Wall Sina. Owing to the protection of the walls, they lived in peace for more than one hundred years.

But not for long, a colossal Titan appeared out of nowhere. Instantly, the walls were shattered, along with the illusory peace of everyday life. Wall Maria was abandoned and human activity was pushed back to Wall Rose. Then mankind began to realize, hiding behind the walls equaled to death and they should manage an attack on the Titans.

So, Captain Levi, the strongest ever human being, was ordered to set up a special operation squad of N people, numbered from 1 to N. Each number should be assigned to a soldier. There are three corps that the soldiers come from: the Garrison, the Recon Corp and the Military Police. While members of the Garrison are stationed at the walls and defend the cities, the Recon Corps put their lives on the line and fight the Titans in their own territory. And Military Police serve the King by controlling the crowds and protecting order. In order to make the team more powerful, Levi will take advantage of the differences between the corps and some conditions must be met.

The Garrisons are good at team work, so Levi wants there to be at least M Garrison members assigned with continuous numbers. On the other hand, members of the Recon Corp are all elite forces of mankind. There should be no more than K Recon Corp members assigned with continuous numbers, which is redundant. Assume there is unlimited amount of members in each corp, Levi wants to know how many ways there are to arrange the special operation squad.

Input
There are multiple test cases. For each case, there is a line containing 3 integers N (0 < N < 1000000), M (0 < M < 10000) and K (0 < K < 10000), separated by spaces.

Output
One line for each case, you should output the number of ways mod 1000000007.

Sample Input
3 2 2
Sample Output
5

题意:给n个士兵排队,每个士兵三种G、R、P可选,求至少有m个连续G士兵,最多有k个连续R士兵的排列的种数。

题解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,m,k,u,v;
int dp[101][3];
ll cal(){
	dp[0][0] = 1;
	dp[0][1] = 0;
	dp[0][2] = 0;
	for(int i = 1;i<=n;i++){
		ll sum = (dp[i-1][0]+dp[i-1][1]+dp[i-1][2]) % 1000;
		dp[i][2] = sum;
		if(i<=u){
			dp[i][0] = sum;
		}else if(i == u+1){
			dp[i][0] = (sum-1) % 1000;
		}else{
			dp[i][0] = (sum - dp[i-u-1][1]-dp[i-u-1][2]) % 1000;
		}
		if(i<=v){
			dp[i][1] = sum;
		}else if(i == u+1){
			dp[i][1] = (sum-1) % 1000;
		}else{
			dp[i][1] = (sum - dp[i-v-1][0]-dp[i-v-1][2]) % 1000;
		}
	}	
	return (dp[n][0]+dp[n][1]+dp[n][2]) % 1000;
}
int main(){
	cin >>n>>m>>k;
	u = n;
	v = k;
	int ans = cal();
//	cout <<cal()<<endl;
	u = m-1;
	v = k;
	cout <<(ans - cal())<<endl;
} 

2

问题描述
有抱负的罗伊·罗伯(Roy the Robber)看过很多美国电影,并且知道坏蛋通常最终会被抓到,通常是因为他们变得太贪心了。他决定只在短期内从事赚钱的银行抢劫案工作,然后退休,然后再回到大学从事舒适的工作。
在这里插入图片描述

几个月来,罗伊(Roy)一直在评估各种银行的安全性和所持有的现金量。他想冒险,并想尽可能多地赚钱。
他的母亲奥拉(Ola)决定了可以忍受的可能性。她认为,如果他一起抢劫的银行给出的概率小于这个概率,他就足够安全了。

输入项
输入的第一行给出T,即案例数。对于每种情况,输入的第一行给出一个浮点数P(Roy的概率必须低于此值),以及一个整数N(他打算为其规划的银行数量)。然后跟随N行,其中行j给出整数Mj和浮点数Pj。
银行j包含Mj百万,而抢劫它的概率为Pj。

输出量
对于每个测试用例,输出一条行,他可以预期获得的最大数目为百万,而被捕获的可能性小于设置的限制。

注意事项和约束条件
0 <T <= 100
0.0 <= P <= 1.0
0 <N <= 100
0 <Mj <= 100
0.0 <= Pj <= 1.0
如果银行被抢劫,它可能会破产,您可以假定概率是独立的,因为警察资金很少。

样本输入
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

样本输出
2
4
6

题解

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
double p0;
int v[105];
double p[105];
double dp[10005];
int main(){
	int t;
	cin >>t;
	while(t--){
		cin >>p0>>n;
		int sum = 0;
		for(int i = 1;i<=n;i++){
			cin >>v[i]>>p[i];
			sum += v[i];
		}
		memset(dp,0,sizeof(dp));
		dp[0] = 1;
		for(int i = 1;i<=n;i++){
			for(int j = sum;j>=v[i];j--){
				dp[j] = max(dp[j],dp[j-v[i]]*(1-p[i]));
			}
		}
		for(int i = sum;i>=0;i--){
			if(dp[i] > (1-p0)){
				cout <<i<<endl;
				break;
			}
		}
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值