算法:DP
题目来源:2012年9月16日成都赛区网络赛 1006 Groups
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4293
Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 511 Accepted Submission(s): 203
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 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.
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.
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 Ai and Bi (0 <= Ai,Bi < 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 Ai and Bi (0 <= Ai,Bi < 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
Recommend
liuyiding
/************************************************************* 题意:有n个人在小道上走, 每个人都报一遍自己前面有几个人,后面有几个人;(有人说谎) 样例:第一行,输入人数n; 剩下n行,依次表示那个人前面有几人,后面有几人; 算法:DP; 注意:dp[i][j]表示第 i+1 到第 j 个人为一组时,前j个人中最多的说真话的人数 思路:1、先依次遍历确立任意一组的人数; 2、然后再往前找,找出这一组前面说真话的最多人数+这一组的人数; 参考的两个不错的blog: http://blog.csdn.net/taozifish/article/details/7985044 http://www.cnblogs.com/Griselda/archive/2012/09/18/2692393.html /************************************************************/ //2012-09-19 16:51:33 Accepted 4293 46MS 2280K 700 B C++ #include<cstdio> #include<cstring> int dp[510][510];//dp[i][j]表示第 i+1 到第 j 个人为一组时,前j个人中最多的说真话的人数 int truth[510]; //truth[i]表示前 i 个人中最多的说真话的人数 int people[510][510];//people[i][j]表示说前面有i个人,后面有j个人的人数 int main() { int n; int front,behind; int i,j; while(scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp)); memset(truth,0,sizeof(truth)); memset(people,0,sizeof(people)); for(i=0;i<n;i++) { scanf("%d%d",&front,&behind); if(front+behind<n) //判断可能为真 people[front][behind]++; if(people[front][behind]>n-front-behind)//不可能超过,若超过则取最大的可能的 people[front][behind]=n-front-behind; } for(i=1;i<=n;i++) { for(j=0;j<i;j++) {//第j+1到第i个人为一组时,前j个人说真话的人数=前j个人说真话的最多人数+前i个人中从第j+1个人到第i个人为一组的人数 dp[j][i]=truth[j]+people[j][n-i]; if(dp[j][i]>truth[i])//如果从第j+1到第i个人为一组时,前i个人中说真话的人比原来的前i个人中说真话的人多 truth[i]=dp[j][i]; } } printf("%d\n",truth[n]); } return 0; }