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了,哈哈,貌似是最早提交的……

目录
相关文章
|
10月前
|
Java 测试技术
hdu 1228 A + B
hdu 1228 A + B
56 0
畅通工程 HDU - 1232
畅通工程 HDU - 1232
91 0
|
人工智能 Java
2021杭电多校5-Arrary-hdu7020
Array Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 965 Accepted Submission(s): 312 Problem Description Given an integer array a[1…n].
194 0
2021杭电多校5-Arrary-hdu7020
|
机器学习/深度学习 算法
|
测试技术
HDOJ(HDU) 2153 仙人球的残影(谜一样的题、、、)
Problem Description 在美丽的HDU,有一名大三的同学,他的速度是众所周知的,跑100米仅仅用了2秒47,在他跑步过程中会留下残影的哎,大家很想知道他是谁了吧,他叫仙人球,既然名字这样了,于是他的思想是单一的,他总是喜欢从一点出发,经过3次转折(每次向右转90°),回到出...
1367 0