zoukankan      html  css  js  c++  java
  • 数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo

    3126: [Usaco2013 Open]Photo

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 222  Solved: 116

    Description

    Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N <= 200,000), which, as always, are conveniently numbered from 1..N. Accordingly, he snapped M (1 <= M <= 100,000) photos, each covering a contiguous range of cows: photo i contains cows a_i through b_i inclusive. The photos collectively may not necessarily cover every single cow. After taking his photos, FJ notices a very interesting phenomenon: each photo he took contains exactly one cow with spots! FJ was aware that he had some number of spotted cows in his herd, but he had never actually counted them. Based on his photos, please determine the maximum possible number of spotted cows that could exist in his herd. Output -1 if there is no possible assignment of spots to cows consistent with FJ's photographic results. 

    给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点

    Input

     * Line 1: Two integers N and M.

    * Lines 2..M+1: Line i+1 contains a_i and b_i.

    Output

    * Line 1: The maximum possible number of spotted cows on FJ's farm, or -1 if there is no possible solution.

    Sample Input

    5 3
    1 4
    2 5
    3 4

    INPUT DETAILS: There are 5 cows and 3 photos. The first photo contains cows 1 through 4, etc.

    Sample Output

    1
    OUTPUT DETAILS: From the last photo, we know that either cow 3 or cow 4 must be spotted. By choosing either of these, we satisfy the first two photos as well. 
      
      考思维的一道好题啊,值得一做。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int INF=2147483647;
     6 const int maxn=200010;
     7 int L[maxn],R[maxn];
     8 int tr[maxn<<2],n,m;
     9 int Query(int node,int l,int r,int a,int b){
    10     if(a<=0)a=1;
    11     if(a>b||b<=0)return 0;
    12 
    13     if(l>=a&&r<=b)return tr[node];
    14     int mid=(l+r)>>1,ret=-INF;
    15     if(mid>=a)ret=Query(node<<1,l,mid,a,b);
    16     if(mid<b)ret=max(ret,Query(node<<1|1,mid+1,r,a,b));
    17     return ret;
    18 }
    19 
    20 void Modify(int node,int l,int r,int g,int d){
    21     
    22     if(l==r){tr[node]=d;return;}
    23     int mid=(l+r)>>1;
    24     if(mid>=g)Modify(node<<1,l,mid,g,d);
    25     else Modify(node<<1|1,mid+1,r,g,d);
    26     tr[node]=max(tr[node<<1],tr[node<<1|1]);
    27 }
    28 
    29 int main(){
    30     freopen("3126.in","r",stdin);
    31     freopen("3126.out","w",stdout);
    32     scanf("%d%d",&m,&n);m++;
    33     for(int i=1;i<=m;i++)
    34         R[i]=i-1;
    35     for(int i=1,a,b;i<=n;i++){
    36         scanf("%d%d",&a,&b);
    37         L[b+1]=max(L[b+1],a);
    38         R[b]=min(R[b],a-1);
    39     }
    40     for(int i=2;i<=m;i++)
    41         L[i]=max(L[i],L[i-1]);
    42     for(int i=m-1;i>=1;i--)
    43         R[i]=min(R[i],R[i+1]);
    44     for(int i=1;i<m;i++)
    45         Modify(1,1,m,i,L[i]<=R[i]?Query(1,1,m,L[i],R[i])+1:-INF);
    46     printf("%d
    ",max(-1,Query(1,1,m,L[m],R[m])));
    47     return 0;        
    48 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    BZOJ1229 USACO2008 Nov toy 玩具 【三分+贪心】*
    BZOJ1304 CQOI2009 叶子的染色 【树形DP】
    BZOJ1131 POI2008 Sta 【树形DP】
    BZOJ1096 ZJOI2007 仓库建设 【斜率优化DP】
    BZOJ4540 Hnoi2016 序列 【莫队+RMQ+单调栈预处理】*
    Codeforces 1012C Hills【DP】*
    POJ1741 Tree + BZOJ1468 Tree 【点分治】
    BZOJ2152 聪聪可可 【点分治】
    HDU1693 Eat the Trees 【插头DP】*
    RUAL1519 Formula 1 【插头DP】
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5396878.html
Copyright © 2011-2022 走看看