zoukankan      html  css  js  c++  java
  • 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. 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了,哈哈,貌似是最早提交的……

  • 相关阅读:
    C++ Builder 全部API函数列表
    返回对应对象的克隆方法
    如何求出三角形的面积
    通用序列化基类 只须实现该类抽象成员即可进行序列化与反序列化
    【分享】JS添加/删除事件处理函数(支持IE,FF,opera,safari)
    如何将字符串序列化到CDATA块中(Serializing A String Within a CDATA Element )
    反射调用静态类的方法,字段
    浏览器选项
    AppDomain 和动态加载
    c#中Enum类型定义与获取值或描述方法 半语小马哥 CSDNBlog
  • 原文地址:https://www.cnblogs.com/hxsyl/p/3025900.html
Copyright © 2011-2022 走看看