ZOJ 3197-阿里云开发者社区

开发者社区> 大数据> 正文
登录阅读全文

ZOJ 3197

简介: Google Book Time Limit: 1 Second      Memory Limit: 32768 KB You, the best hacker in the world, want to download the books published on Google Book.
Google Book
Time Limit: 1 Second      Memory Limit: 32768 KB

You, the best hacker in the world, want to download the books published on Google Book. After some investigation, you found that the address of each page consists of two parts. The first part is the page number, the second part is the signature which is unique for each page. To get the signature, you can send the query to the server. The query has one parameter, which indicates the page number. The server will return the signature of the required page, and it may also return the signature of some adjacent pages.

To minimize the bytes downloaded from the internet, and also make the server adminstrator hard to notice your "hack", you'd like to minimize the number of queries

Input

The input has multiple cases. The first line of the input is a single integer T which is the number of test cases. Then T consecutive test cases follow. In each test case, the first line is a number N (1<=N<=5000), indicating the number of pages of the book. Then n lines follows. On the i-th line, there will be two integers ai and bi (ai<=i<=bi). They indicate that the query for the i-th page will return the signatures from page ai to page bi (inclusive)

Output

Results should be directed to standard output. The output of each test case should be a single integer, which is the minimum number of queries to get all the signatures.

Sample Input

 

2
3
1 1
2 2
3 3
3
1 1
1 3
3 3

 

Sample Output

 

3
1

 


Author: FAN, Xiang
Source: ZOJ Monthly, May 2009

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdlib>
 4 using namespace std;
 5 
 6 const int N = 5010;
 7 typedef struct Node 
 8 {
 9     int start,end;
10 };
11 Node node[N];
12 
13 int cmp(const void *a, const void *b)
14 {
15     Node *c = (Node *)a;
16     Node *d = (Node *)b;
17     if(c->start != d->start)
18         return c->start > d->start;
19     return c->end > d->end;
20 }
21 bool comp(Node s1, Node s2)
22 {
23     if(s1.start != s2.start)
24         return s1.start < s2.start;
25     return s1.end < s2.end;
26 }
27 
28 int main()
29 {
30     int i,j,k;
31     int T;
32     int n;
33     cin>>T;
34     int from, to, cnt;
35     while(T--)
36     {
37         cin>>n;
38        // memset(node,0,sizeof(node));
39         for(i=0; i<n; i++)
40         {
41             cin>>node[i].start>>node[i].end;
42             if(node[i].end>n)
43                 node[i].end = n;
44         }
45         //qsort排序失败了不知道为啥子, 
46         //qsort(node,n,sizeof(Node[0]),cmp);
47         sort(node, node +n,comp);
48         /*
49         for(i=0; i<n; i++)
50         {
51             cout<<node[i].start<<"  "<<node[i].end<<endl;
52         }
53         */
54         
55         //排过序了,最小点一定是1 ,实际上对end排序没有意义 
56         from = node[0].start, to = node[0].end;
57         i = 0;
58         while(i<n && from==node[i].start)
59         {
60             if(to<node[i].end)
61                 to = node[i].end;
62             i++;
63         }
64         
65         cnt = 1;
66         while(to<n)
67         {
68             int temp = -1;
69             from = to + 1;
70             for(j=i; j<n; j++)
71             {
72                 if(node[j].start<=from)
73                 {
74                     if(node[j].end>temp)
75                         temp = node[j].end;
76                 }
77                 else
78                 {
79                     i = j;
80                     break;
81                 } 
82             }
83             /*
84             必须在for外判断,判断是必须要的,因为可能前一个区间完全包含选择好的最大区间
85             比如
86             前一个:(1,9)
87              后:(2,3)(2,6)(4,7),则选择了(4,7) 不过7还是小于9的,不用增加下载次数,卡在这了一直TLE 
88             */
89             if(temp>to)
90             {
91                 cnt++;
92                 to = temp;
93             }
94         }
95         cout<<cnt<<endl;   
96     }
97     return 0;
98 }

 省赛时遇到一道类似的,直接AC了,哈哈,貌似是最早提交的……

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
大数据
使用钉钉扫一扫加入圈子
+ 订阅

大数据计算实践乐园,近距离学习前沿技术

其他文章
最新文章
相关文章