HDU 4293 Groups(dp)

来源:互联网 发布:潘石屹三段婚姻知乎 编辑:程序博客网 时间:2024/05/21 21:40

Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1629    Accepted Submission(s): 618


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.
 

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).
 

Output
  For each test case your program should output a single integer M, the maximum number of players providing correct information.
 

Sample Input
32 00 22 232 00 22 2
 

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

Source
2012 ACM/ICPC Asia Regional Chengdu Online
 


题意:有 n 个人,可任意分成若干组,然后每个人各提供一个信息,表示他们组前面有多少个人,后面有多少个人。问最多有多少个信息是不冲突的。

题解:每个人都提供了一个区间[x+1,n-y],dp[i]表示前面i个人不冲突的最大人数。

#include<cstring>#include<algorithm>#include<cstdio>#include<iostream>#include<vector>#define ll long longusing 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;}


0 0
原创粉丝点击