您的位置:首页 > 其它

hdu 4293 Groups

2012-10-06 18:55 423 查看

Groups

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 733 Accepted Submission(s): 288

[align=left]Problem Description[/align]
  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 Ai players in front of our group, as well as Bi players are following us.” from the
ith 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.

[align=left]Input[/align]
  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 Ai and Bi (0 <= Ai,Bi
< N) separated by single spaces.

  Please process until EOF (End Of File).

[align=left]Output[/align]
  For each test case your program should output a single integer M, the maximum number of players providing correct information.

[align=left]Sample Input[/align]

3
2 0
0 2
2 2
3
2 0
0 2
2 2


[align=left]Sample Output[/align]

2
2
Hint
The third player must be making a mistake, since only 3 plays exist.


[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Chengdu Online

分析:
简单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
,s

;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: