Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1629 Accepted Submission(s): 618
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.
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).
題意:有 n 個人,可任意分成若干組,然後每個人各提供一個信息,表示他們組前面有多少個人,後面有多少個人。問最多有多少個信息是不衝突的。
題解:每個人都提供了一個區間[x+1,n-y],dp[i]表示前面i個人不衝突的最大人數。
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<vector>
#define ll long long
using namespace std;
int n;
int G[555][555],dp[555];
struct Jd {
int l,r;
} ;
Jd a[555];
bool cmp(Jd a,Jd b) {
return a.l<b.l||(a.l==b.r&&a.r<b.r);
}
int main() {
// freopen("test.in","r",stdin);
while(~scanf("%d",&n)) {
memset(G,0,sizeof G);
memset(dp,0,sizeof dp);
int x,y,m=0;
for(int i=0; i<n; i++) {
scanf("%d%d",&x,&y);
if(x+y>=n)continue;
if(G[x+1][n-y]==0) {
a[m].l=x+1;
a[m++].r=n-y;
}
if(n-y-x>G[x+1][n-y]) {
G[x+1][n-y]++;
}
}
int k=0;
sort(a,a+m,cmp);
for(int i = 0; i < m; ++i) {
dp[i]=G[a[i].l][a[i].r];
for(int j = 0; j < i; ++j) {
if(a[i].l>a[j].r) {
dp[i]=max(dp[i],dp[j]+G[a[i].l][a[i].r]);
}
}
}
int ans=0;
for(int i=0; i<m; i++)ans=max(ans,dp[i]);
printf("%d\n",ans);
}
return 0;
}