hdu4293 dp

Groups

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

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
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.
 
Source
转化为区间,求不相交的区间的num的和最大,有点像是2037的今年暑假不AC的那个区间
例如:有15个人,信息分别是
 0  14
 1  13
 1  13
 4   8
 6   7
 6   7
 6   7
10  2
10  2
10  4
11  3
12  2
12  6
13  7
13  7
其中最后3个是不合法的,总人数大于n了,所以直接淘汰(说谎的孩子果断不是好孩子,而且还说话说的这么没技术含量),信息已经按照从小大到的循序排好了,然后已知前面的人,和后面的人,所以他们的group所占的区间就确定下来了,这个区间的长度就是最多这个group有多少人,那么画出所有合法的情况,就是下图的第一个图片。
可能说话人说的一样,每个区间对应一个left值,对应一个right值,对应一个num值,是看看有多少个人说这个答案,所以图二就是把蓝色的去掉,去重,改变num值。
图三中就是去重之后的数轴区间,选择不相交的区间的num的和最大,如果相交,两个人说的话就矛盾了……利用dp找不相交的区间的num的和最大,所以红色的就是答案了,最多有7个人说实话。

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 #define LL long long
 7 using namespace std;
 8 const int N=505;
 9 struct node
10 {
11     int l,r;
12     int num;
13 }mem[N];
14 int ans[N];
15 bool cmp(node x,node y)
16 {
17     if(x.l==y.l)
18     return x.r<y.r;
19     return x.l<y.l;
20 }
21 int main()   
22 {
23     int n;
24     while(scanf("%d",&n)!=EOF)
25     {
26         int I=0;
27         for(int i=1;i<=n;++i)
28         {
29             int k1,k2;
30             scanf("%d %d",&k1,&k2);
31             if(k1+k2>=n)
32             continue;
33             mem[I].l=k1+1;
34             mem[I].r=n-k2;
35             mem[I].num=1;
36             ++I;
37         }
38         if(I==0)
39         {printf("0\n");continue;}
40         sort(mem,mem+I,cmp);
41         int l=0;
42         for(int i=1;i<I;++i)
43         {
44             if(mem[i].l==mem[l].l&&mem[i].r==mem[l].r)
45             {
46                 mem[l].num++;
47                 mem[l].num=min(mem[l].num,mem[l].r-mem[l].l+1);
48             }
49             else
50             {
51                 ++l;
52                 mem[l].l=mem[i].l;
53                 mem[l].r=mem[i].r;
54                 mem[l].num=mem[i].num;
55             }
56         }
57         int ans=mem[0].num;
58         for(int i=1;i<=l;++i)
59         {
60             int k=0;
61             for(int j=0;j<i;++j)
62             {
63                 if(mem[j].r<mem[i].l&&mem[j].num>k)
64                 {
65                     k=mem[j].num;
66                 }
67             }
68             mem[i].num+=k;
69             ans=max(ans,mem[i].num);
70         }
71         printf("%d\n",ans);
72     }
73 }

 

posted @ 2012-09-16 21:01  _sunshine  阅读(538)  评论(0编辑  收藏  举报