BZOJ1178 [apio2009]convention Conference Center Greedy set

Source: Internet
Author: User

Welcome to the original source-- Blog Park-zhouzhendong go to the blog Park to see the puzzleTopic Portal-BZOJ1178Test Instructions Summary

A bunch of line segments, now take up the maximum number of bars and output the scheme with the smallest dictionary order.

Solving

This is a good hole.

First, notice that there is no extra space at the end.

The first question is the base segment coverage.

The key is the second question.

We are going to prepare a function--get_ans (L,R), which is used to solve the maximum number of segments that can be taken in the L~r interval.

This can be solved by O (log n). The premise is multiplication preprocessing.

Then there is the key part.

We follow the dictionary order from small to large, can choose to choose.

How do you decide if you can choose? It is only necessary to place the segment in the space of the previously selected segment, and to satisfy the Get_ans (the segment is before the first segment on its left) +get_ans (it goes to the first segment on its right) +1 = Get_ans (his left and the opposite), then it can be put in.

Code

#include <cstring> #include <algorithm> #include <cstdio> #include <cmath> #include <cstdlib    > #include <set>using namespace std;const int n=200000+5,inf=1e9;struct seg{int l,r;        BOOL operator < (const SEG x) const {if (L==X.L) return r>x.r;    Return l<x.l; }}s[n],s2[n];int n,rn,hash[n*2],hs,p[n][20],l[n];bool alive[n],vis[n*2];set <int> s;void Hash () {sort (Hash+1,    HASH+HS+1);    int hs_=1;    for (int i=2;i<=hs;i++) if (hash[i]!=hash[i-1]) hash[++hs_]=hash[i]; Hs=hs_;}    void Delete () {for (int i=1;i<=n;i++) s2[i]=s[i];    Sort (s+1,s+n+1);    memset (alive,true,sizeof alive);    int minr=inf,n_=0; for (int i=n;i>=1;i--) if (Minr>s[i]. R) Minr=s[i].        R    else alive[i]=0;    for (int i=1;i<=n;i++) if (Alive[i]) s[++n_]=s[i]; N=n_;}    void Get_pos () {memset (p,0,sizeof p); for (int i=1;i<=n;i++) l[i]=S[i].    L    L[n+1]=inf; for (int i=1;i<=n;i++) {p[i][0]=upper_bound (l+1,l+n+2,s[i].        R)-L;    if (p[i][0]==n+1) p[i][0]=0; } for (int i=1;i<=18;i++) for (int j=1;j<=n;j++) p[j][i]=p[p[j][i-1]][i-1];} int Get_ans (int le,int Ri) {if (le>ri| |    LE&GT;HS) return 0;    int Ans=1,pos=lower_bound (L+1,l+n+2,le)-l; if (pos>n| | Ri<s[pos].    R) return 0; for (int i=18;i>=0;i--) {if (!p[pos][i]| | S[p[pos][i]].        R>ri) continue;        Pos=p[pos][i];    ans+=1<<i; } return ans;    int main () {scanf ("%d", &n), rn=n; for (int i=1;i<=n;i++) {scanf ("%d%d", &s[i]. L,&s[i].        R); Hash[++hs]=s[i].        L Hash[++hs]=s[i].    R    } HASH (); for (int i=1;i<=n;i++) {s[i]. L=lower_bound (Hash+1,hash+hs+1,s[i].        L)-hash; S[i]. R=lower_bound (Hash+1,hash+hs+1,s[i].    R)-hash;    } Delete ();    Get_pos ();    printf ("%d\n", Get_ans (1,HS));    S.clear (); S.insERT (INF), S.insert (-inf);    BOOL empty_block=0; for (int i=1;i<=rn;i++) {int now=*s.lower_bound (S2[i].        L); if (now==inf| |! Vis[now]) {if (Now<=s2[i].            R) continue; int Left=*--s.lower_bound (S2[i].            L); int Right=*s.lower_bound (S2[i].            R); int Val=get_ans (Left+1,s2[i]. L-1) +1+get_ans (S2[i].            R+1,RIGHT-1);            if (Val<get_ans (left+1,right-1)) continue; S.insert (S2[i].            L); S.insert (S2[i].            R); if (S2[i]. L!=s2[i]. R) Vis[s2[i].            R]=1;            if (empty_block) Putchar (");            printf ("%d", I);        empty_block=1; }} return 0;}

  

BZOJ1178 [apio2009]convention Conference Center Greedy set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.