Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 733 Accepted Submission(s): 288
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.
Source
分析:
简单dp,假设给所有人编号为0~n-1。
对于某个人给出的信息,假设它是合法的(a+b<n),那么这个人所属区间为[a,n-1-b]。
注意:区间总人数不能超过区间总长度,即若设s[i][j]为区间[i,j]内没有说谎人数,则s[i][j]<=j-i+1;
dp[i]:到第i号人为止,最多有dp[i]个人没有说谎。
转移方程:dp[i]=max(dp[i],dp[j-1]+s[j][i]);
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=500;
int dp[N],s[N][N];
int main()
{
int n,i,j,ans,a,b;
while(~scanf("%d",&n))
{
memset(s,0,sizeof(s));
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
if(s[a][n-1-b]<n-a-b) s[a][n-1-b]++;
}
ans=0;
for(i=0;i<n;i++)
{
dp[i]=0;
for(j=i;j>=0;j--)dp[i]=max(dp[i],dp[j-1]+s[j][i]);
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return 0;
}