http://acm.hdu.edu.cn/showproblem.php?pid=4293
Problem Description
After the regional contest, all the ACMers are walking alone a very long avenue to the dining hall in groups. Groups can vary in size for kinds of reasons, which means, several players could walk together, forming a group.
As the leader of the volunteers, you want to know where each player is. So you call every player on the road, and get the reply like “Well, there are A i players in front of our group, as well as B i players are following us.” from the i th player.
You may assume that only N players walk in their way, and you get N information, one from each player.
When you collected all the information, you found that you’re provided with wrong information. You would like to figure out, in the best situation, the number of people who provide correct information. By saying “the best situation” we mean as many people as possible are providing correct information.
As the leader of the volunteers, you want to know where each player is. So you call every player on the road, and get the reply like “Well, there are A i players in front of our group, as well as B i players are following us.” from the i th player.
You may assume that only N players walk in their way, and you get N information, one from each player.
When you collected all the information, you found that you’re provided with wrong information. You would like to figure out, in the best situation, the number of people who provide correct information. By saying “the best situation” we mean as many people as possible are providing correct information.
Input
There’re several test cases.
In each test case, the first line contains a single integer N (1 <= N <= 500) denoting the number of players along the avenue. The following N lines specify the players. Each of them contains two integers A i and B i (0 <= A i,B i < N) separated by single spaces.
Please process until EOF (End Of File).
In each test case, the first line contains a single integer N (1 <= N <= 500) denoting the number of players along the avenue. The following N lines specify the players. Each of them contains two integers A i and B i (0 <= A i,B i < N) separated by single spaces.
Please process until EOF (End Of File).
Output
For each test case your program should output a single integer M, the maximum number of players providing correct information.
Sample Input
3 2 0 0 2 2 2 3 2 0 0 2 2 2
Sample Output
2 2HintThe third player must be making a mistake, since only 3 plays exist.
简单思路:dp[i]表示前n个人中说真话的人最多的个数,g[i][j]表示区间[i,j]中的人是一个小组,他们说的话全是真话。状态转移方程为:
dp[j]=max(dp[i]+g[i][j],dp[i][j]) 其中g[i][j]不为零时才可以这么干,否则,dp[i]=mamx(dp[i-1],dp[i]);
ps: 写了这么多关于区间dp的题目,现在写起来还是那么的无从下手,不知道要到啥时候呢,唉,ac成长路漫漫啊~~~
ac代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int N=505;
int g[N][N];
int dp[N];
int main()
{
int n,a,b;
while(~scanf("%d",&n))
{
memset(g,0,sizeof(g));
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
if(a+b<n&&g[a][n-b]+a+b<n)
{
g[a][n-b]++;
}
}
for(int i=0;i<=n;i++)
{
if(i!=0)
dp[i]=max(dp[i-1],dp[i]);
for(int j=i+1;j<=n;j++)
{
if(g[i][j])
dp[j]=max(dp[j],dp[i]+g[i][j]);
}
}
printf("%d\n",dp[n]);
}
return 0;
}