Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1691 Accepted Submission(s): 650
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.
题意:有n个人走路,有一些人组队在一起走,每个人说出自己队伍的前面有多少人,自己队伍后面有多少人,然后求这些人最多有多少人说的是正确的。
思路:首先求出每个人所在的区间,然后思考这些区间,如果有相同区间的就把这个区间的权值加一,不过最大的上限是不能超过这个区间的长度,然后就是求整个无覆盖的区间权值的最大和了,因为如果两个区间有了区间覆盖,这就显然是矛盾的了,一个区间就表示了一个队伍,有区间覆盖了就代表队伍描述有错,所以区间有覆盖的直接忽略就好。最终求最大值的思路其实是dp,只是没用数组表示。。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
struct data1
{
int a,b;
} a[510];
struct data
{
int a,b,num;
} b[510];
bool cmp(data1 a,data1 b)
{
if(a.b!=b.b)
return a.b<b.b;
return a.a<b.a;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int bj=0;
for(int i=0; i<n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(x+y<n)
a[bj].a=x+1,a[bj++].b=n-y;
}
sort(a,a+bj,cmp);
int bjj=0;
b[0].a=a[0].a,b[0].b=a[0].b,b[0].num=1;
for(int i=1; i<bj; i++)
{
if(a[i].a==a[i-1].a&&a[i].b==a[i-1].b)
b[bjj].num=min(a[i].b-a[i].a+1,b[bjj].num+1);
else
{
bjj++;
b[bjj].a=a[i].a;
b[bjj].b=a[i].b;
b[bjj].num=1;
}
}
int ans=b[0].num;
for(int i=1; i<=bjj; i++)
{
int max0=0;
for(int j=i-1; j>=0; j--)
if(b[j].b<b[i].a&&b[j].num>max0)
max0=b[j].num;
b[i].num+=max0;
ans=max(ans,b[i].num);
}
printf("%d\n",ans);
}
return 0;
}