hdu4293

D - Groups
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4293

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个人,可任意分成若干组,然后每个人个各提供一个信息,表示他们组前面有多少人,后面有多少人。问最多有多少个信息是不冲突的。
解:给n个人编号1到n,一个组报出前后有多少人时可以算出这组的人的区间,这样就可以把本题转化成给定一些带权值的区间,求不相交区间的最大权值。这样就可以先按区间排个序,然后依次更新就行了。
ac 代码:有一些解释
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=600;
struct node
{
    int a,b;
}d[maxn];
int dp[maxn],mp[maxn][maxn];
bool cmp(node x,node y)
{
    if(x.b!=y.b)
        return x.b<y.b;
    return x.a<y.a;
}
int main()
{
    int n;
    int a,b;
    while(scanf("%d",&n)!=-1)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&d[i].a,&d[i].b);
            d[i].a++;
            d[i].b=n-d[i].b;
        }
        sort(d,d+n,cmp);
        memset(mp,0,sizeof(mp));///mp[a][b]表示区间a到b有几个人
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            a=d[i].a,b=d[i].b;
            if(a>b||mp[a][b]>=b-a+1)///a>b肯定不行,这个然肯定撒谎了,mp[a][b]之间最多有b-a+1个人,
            continue;               ///人满了就不需要再操作了
            mp[a][b]++;
            dp[b]=max(dp[b],dp[a-1]+mp[a][b]);///dp[b]表示b之间有多少人没有撒谎
            for(int j=b+1;j<=n;j++)
                dp[j]=dp[b];
        }
        printf("%d\n",dp[n]);

    }
    return 0;
}


你可能感兴趣的:(hdu4293)