ZOJ 3197

简介: Google BookTime Limit: 1 Second      Memory Limit: 32768 KBYou, 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了,哈哈,貌似是最早提交的……

目录
相关文章
|
机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
POJ 1775 (ZOJ 2358) Sum of Factorials
122 0
|
人工智能 BI 应用服务中间件
|
机器学习/深度学习
|
Java 测试技术 C++
HDU 3783 ZOJ
ZOJ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2779    Accepted Submission(s): 1840 Problem Description 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出。
1079 0
|
人工智能 机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions t...
1113 0
|
人工智能 BI JavaScript
POJ 2260(ZOJ 1949) Error Correction 一个水题
Description A boolean matrix has the parity property when each row and each column has an even sum, i.
1118 0

热门文章

最新文章