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.
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).
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
2
Hint
The third player must be making a mistake, since only 3 plays exist.
/**
hdu4293 dp
题目大意:n个人在一条路上走路,多个或一个人为一组,每个人说一句话“我们组前面有多少人,我们组后面有多少人”
求最多有多少人说真话
解题思路:mp记录区间(i,j)为一组时有多少人说相同的话dp[i]表示前i个人最多有多少人说真话。
转移方程为:dp[i]=max(dp[j-1]+mp[i][j]),j<=i
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int n,mp[506][505],dp[505];
int main()
{
while(~scanf("%d",&n))
{
memset(mp,0,sizeof(mp));
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(x+y>=n)continue;
if(n-y-x>mp[x+1][n-y])
{
mp[x+1][n-y]++;
}
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
dp[i]=max(dp[i],dp[j-1]+mp[j][i]);
}
}
printf("%d\n",dp[n]);
}
return 0;
}