[递推dp] zoj 3747 Attack on Titans

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170

Attack on Titans


Time Limit: 2 Seconds      Memory Limit: 65536 KB



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

Hint

Denote the Garrison, the Recon Corp and the Military Police as G, R and P. Reasonable arrangements are: GGG, GGR, GGP, RGG, PGG.

题目意思:

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

解题思路:

dp递推。

先把问题都转化成至多连续的情况:至多k个连续R,至多n个连续G情况 【减去】至多k个连续R,至多(m-1)个连续G情况。

至多的情况比较好考虑,至少的情况比较复杂,比赛的时候一直落在至少的圈子里,没想到用递推。

//dp[i][0]表示第i个为G,至多有u个连续G,至多有v个连续R的个数  //这里的u和v固定

//dp[i][1]表示第i个为R,....

 //d[i][2]表示第i个为P,....

当第i个为P的情况很好考虑不会对连续的R和G产生影响,dp[i][2]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

当第i个为G时

如果i<=u 时 无论怎么放都不会超过u个连续的G这个限制条件 所以dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

如果i=u+1时,要排除前u个都放了G的情况,dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-1;

如果i>u+1时,要排除从i-1到i-u位置都放了G的情况,dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-dp[i-u-1][1]-dp[i-u-1][2];

当第i个为R时

如果i<=v 时 无论怎么放都不会超过u个连续的G这个限制条件 所以dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

如果i=v+1时,要排除前v个都放了G的情况,dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-1;

如果i>v+1时,要排除从i-1到i-v位置都放了G的情况,dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-dp[i-v-1][0]-dp[i-v-1][2];

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 1100000

LL dp[Maxn][5]; //dp[i][0]表示第i个为G,至多有u个连续G,至多有v个连续R的个数
                //dp[i][1]表示第i个为R,....
                //dp[i][2]表示第i个为P,....
LL n,m,k,u,v;

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])%M;
       dp[i][2]=sum;

       if(i<=u)
            dp[i][0]=sum;
       else if(i==u+1)
            dp[i][0]=(sum-1)%M;
       else
            dp[i][0]=(sum-dp[i-u-1][1]-dp[i-u-1][2])%M;

       if(i<=v)
            dp[i][1]=sum;
       else if(i==v+1)
            dp[i][1]=(sum-1)%M;
       else
            dp[i][1]=(sum-dp[i-v-1][0]-dp[i-v-1][2])%M;

       //printf("u:%lld v:%lld i:%d %lld %lld %lld\n",u,v,i,dp[i][0],dp[i][1],dp[i][2]);
       //system("pause");

    }
    return (dp[n][0]+dp[n][1]+dp[n][2])%M;
}

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%lld%lld%lld",&n,&m,&k))
   {
       LL ans;
       u=n,v=k;
       ans=Cal();

       //printf(":%lld\n",ans);
       //system("pause");

       u=m-1,v=k;
       //printf(":%lld\n",Cal());
       //system("pause");
       ans=((ans-Cal())%M+M)%M;
       printf("%lld\n",ans);

   }
   return 0;
}

[递推dp] zoj 3747 Attack on Titans,码迷,mamicode.com

时间: 2024-10-08 01:47:02

[递推dp] zoj 3747 Attack on Titans的相关文章

ZOJ 3747 Attack on Titans

Attack on Titans Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 374764-bit integer IO format: %lld      Java class name: Main Over centuries ago, mankind faced a new enemy, the Titans. The difference of powe

hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5069    Accepted Submission(s): 2868 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Outpu

D. Caesar&#39;s Legions 背包Dp 递推DP

http://codeforces.com/problemset/problem/118/D 设dp[i][j][k1][k2] 表示,放了i个1,放了j个2,而且1的连续个数是k1,2的连续个数是k2 如果这样写,用dfs写是很简单的.但是超时,我记忆化不到 如果用递推写,对于每一个状态,更新到下一个状态. 如果放的是1,那么新的状态是dp[i + 1][j][k1 + 1][0]也就是,用多了一个1,而且连续的个数也增加了.同时,2的连续个数就打破了,变成了0 这种枚举旧状态,更新下一个状态

递推DP URAL 1031 Railway Tickets

题目传送门 1 /* 2 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 3 注意:s1与s2大小不一定,坑! 4 详细解释:http://blog.csdn.net/kk303/article/details/6847948 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <s

递推DP URAL 1119 Metro

题目传送门 1 /* 2 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 3 递推DP:仿照JayYe,处理的很巧妙,学习:) 4 好像还要滚动数组,不会,以后再补 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cmath> 10 #include <cs

Code Force 429B Working out【递推dp】

Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and mcolumns. Let number a[i][j] represents the calories burned by performing workout at the

递推DP UVA 607 Scheduling Lectures

题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按照时间多少,学生会有不满意度.问最少要几节课讲完主题,如果多种方案输出不满意度最小的 分析:dp[i]表示前i个主题最少要多少节课讲完,那么这个主题可能和上个主题在同一节课讲或者多开新的一节课讲,状态转移方程:看代码:优先满足节数少的情况 收获:普通的递推DP 代码: /**************

CodeForces Round#229 DIV2 C 递推DP

对这道题目也只好说呵呵了,没注意k的范围最大才10,所以昨晚纠结了很久,没什么好的方法来处理,后来无奈想去翻翻题解,发现人家开头就来了句,因为k的范围比较小 所以.........我只好暂停马上回头看看题目,是的,k比较小所以完全可以先在询问前预处理DP一遍, DP就比较清晰了,dp[i][j]  (i>=0 && i<k,,,,j>=i && j <=n)代表意义呢 以i为开头的  区间[1,j]注意 是 1~j的 所需要的操作数,题目问的是最小操

递推DP URAL 1353 Milliard Vasya&#39;s Function

题目传送门 1 /* 2 题意:1~1e9的数字里,各个位数数字相加和为s的个数 3 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 4 状态转移方程:dp[i][j] += dp[i-1][j-k],为了不出现负数 5 改为:dp[i][j+k] += dp[i-1][j] 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #include <cmath> 10 #include <algorithm