zoukankan      html  css  js  c++  java
  • Atcoder Contest069F:Flag

    题目:https://arc069.contest.atcoder.jp/tasks/arc069_d

    题意就是让你在n对数字每一对都选一个数使得任意两个数做差的绝对值最小值最大。

    关系显然是一个2-sat,然后我们发现二份答案如果差值为x那么a-x+1到a+x-1是绝对不能选的,

    也就是选完以后剩下的一定是这些,所以线段树优化连边覆盖区间。然后在线段树的底层端点再反过来一下即可。

    By:大奕哥

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 const int N=1e6+10;
      4 int head[N],col[N],dfn[N],low[N],s[N],in[N],xx[N],yy[N],pos[N];
      5 int n,c,rt,ans,cnt,tmp,num,pre,top;
      6 struct tree{
      7     int l,r;
      8 }t[N<<2];
      9 struct edge{
     10     int to,nex;
     11 }e[N<<1];
     12 struct node{
     13     int x,y;
     14     bool operator <(const node &b)const{
     15         return x<b.x;
     16     }
     17 }a[N];
     18 void add(int x,int y)
     19 {
     20     e[++cnt].to=y;e[cnt].nex=head[x];head[x]=cnt;
     21 }
     22 void build(int &x,int l,int r)
     23 {
     24     if(l==r){
     25         x=(a[l].y<=n)?a[l].y+n:a[l].y-n;
     26         pos[a[l].y]=l;return;
     27     }x=++num;
     28     int mid=l+r>>1;
     29     build(t[x].l,l,mid);build(t[x].r,mid+1,r);
     30     add(x,t[x].l);add(x,t[x].r);
     31     return;
     32 }
     33 void change(int x,int l,int r,int L,int R,int p)
     34 {
     35     if(L>R)return;
     36     if(l==L&&r==R)
     37     {
     38         add(p,x);return;
     39     }
     40     int mid=l+r>>1;
     41     if(mid>=R)change(t[x].l,l,mid,L,R,p);
     42     else if(L>mid)change(t[x].r,mid+1,r,L,R,p);
     43     else change(t[x].l,l,mid,L,mid,p),change(t[x].r,mid+1,r,mid+1,R,p);
     44     return;
     45 }
     46 void dfs(int x)
     47 {
     48     dfn[x]=low[x]=++tmp;
     49     s[++top]=x;in[x]=1;
     50     for(int i=head[x];i;i=e[i].nex)
     51     {
     52         int y=e[i].to;
     53         if(!dfn[y])
     54         {
     55             dfs(y);
     56             low[x]=min(low[x],low[y]);
     57         }
     58         else if(in[y])low[x]=min(low[x],dfn[y]);
     59     }
     60     if(low[x]==dfn[x])
     61     {
     62         c++;int a=-1;
     63         do{
     64             a=s[top--];
     65             in[a]=0;
     66             col[a]=c;
     67         }while(a!=x);
     68     }
     69     return;
     70 }
     71 void init()
     72 {
     73     memset(dfn,0,sizeof(dfn));
     74     memset(low,0,sizeof(low));
     75     memset(col,0,sizeof(col));
     76     tmp=c=0;
     77     return;
     78 }
     79 bool judge(int x)
     80 {
     81     init();
     82     for(int i=1;i<=n*2;++i)head[i]=0;cnt=pre;
     83     for(int i=1;i<=n;++i)
     84     {
     85         int AL=lower_bound(a+1,a+1+2*n,(node){xx[i]-x+1,0})-a;
     86         int AR=upper_bound(a+1,a+1+2*n,(node){xx[i]+x-1,0})-a-1;
     87         if(AL<=AR)change(rt,1,n*2,AL,pos[i]-1,i),change(rt,1,n*2,pos[i]+1,AR,i);
     88         int BL=lower_bound(a+1,a+1+2*n,(node){yy[i]-x+1,0})-a;
     89         int BR=upper_bound(a+1,a+1+2*n,(node){yy[i]+x-1,0})-a-1;
     90         if(BL<=BR)change(rt,1,n*2,BL,pos[i+n]-1,i+n),change(rt,1,n*2,pos[i+n]+1,BR,i+n);
     91     }
     92     for(int i=1;i<=num;++i)
     93     if(!dfn[i])dfs(i);
     94     for(int i=1;i<=n;++i)
     95     if(col[i]==col[i+n])return 0;
     96     return 1;
     97 }
     98 int main()
     99 {
    100     scanf("%d",&n);
    101     for(int i=1;i<=n;++i)
    102     {
    103         scanf("%d%d",&xx[i],&yy[i]);
    104         a[i*2-1].x=xx[i];a[i*2].x=yy[i];
    105         a[i*2-1].y=i;a[i*2].y=i+n;
    106     }num=n*2;
    107     sort(a+1,a+1+n*2);
    108     build(rt,1,n*2);
    109     int l=0,r=1e9;pre=cnt;
    110     while(l<=r)
    111     {
    112         int mid=l+r>>1;
    113         if(judge(mid))ans=mid,l=mid+1;
    114         else r=mid-1;
    115     }
    116     printf("%d
    ",ans);
    117     return 0;
    118 }
  • 相关阅读:
    struts的action直接输出中文备注【原创】
    iframe高度处理【原创】
    Gel备注【原创】
    关于用jsp生成xml的问题【原创】
    网上流行的flash切换图片之研究【原创】
    c#连mysql的latin1编码乱码问题
    windows phone 7图片搜索小工具(源码)
    家猫js类库
    理解c#中的readonly,const与enum枚举
    Silverlight 雷达图和一种特殊泡泡画法
  • 原文地址:https://www.cnblogs.com/nbwzyzngyl/p/8510377.html
Copyright © 2011-2022 走看看