JZYZOJ1140 飞船控制站

p1140

就一道非常普通的二分,但是非常蛋疼的是验证mid left的过程一直错(就是写一个k次循环然后根据可行与否返回0或1的函数),不知道为什么,嗯哈希搞完了,有点纠结是把之前不熟的东西再搞一遍还是直接搞图论?但是还是先把1140ac比较重要
 
  1 #include<iostream>  
  2 #include<cstdio>  
  3 #include<cstring>  
  4 #include<algorithm>  
  5 #include<cstdlib>  
  6 //#include<cmath>  
  7 using namespace std;  
  8 inline int read(){  
  9     int bj=1;char ch=getchar();  
 10     while(ch<'0'||ch>'9'){
 11         if(ch=='-')bj=-1;  
 12         ch=getchar();  
 13     }
 14     int ret=0;  
 15     while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}  
 16     return ret*bj;  
 17 }  
 18 int n,m,k,map[2005][2005]={0},a[4005]={0};  
 19 bool vis[4005]={0};  
 20 void init(){  
 21     n=read();m=read();k=read();  
 22     for(int i=1;i<=n;i++){  
 23         for(int j=1;j<=2*m;j++){  
 24             char ch;  
 25             cin>>ch; 
 26             map[i][j]=ch-'0';  
 27         }  
 28     }  
 29 }  
 30 void orbit(int idx){  
 31     a[0]=0;  
 32     for(int i=1;i<=n;i++)a[++a[0]]=map[i][idx];  
 33     for(int i=n;i;i--)a[++a[0]]=map[i][idx+m];  
 34 }  
 35 int L(int x,int R)  {  
 36     int pos=x-R,lim=2*n;  
 37     pos+=lim;  
 38     if(pos%lim==0)return lim;  
 39     return pos%lim;  
 40 }  
 41 int R(int x,int R)  {  
 42     int pos=x+R,lim=2*n;  
 43     if(pos%lim==0)return lim;  
 44     return pos%lim;  
 45 }  
 46 void cover(int x,int r)  {  
 47     for(int i=0;i<=r;i++)vis[L(x,i)]=vis[R(x,i)]=1;  
 48 }  
 49 bool check(int r)  {  
 50     memset(vis,0,sizeof(vis));  
 51     int st=(n+1)/2,ans=1;  
 52     cover(st,r);  
 53     int pos=R(st,r+1);  
 54     while(pos!=st){  
 55         //if(r==2)cout<<pos<<"<----\n";  
 56         if(vis[pos]){  
 57             pos=R(pos,1);  
 58             continue;  
 59         }  
 60         bool fl=0;  
 61         for(int j=r;j;j--){  
 62             if(a[R(pos,j)]==1){  
 63                 fl=1;  
 64                 ans++;  
 65                 cover(R(pos,j),r);  
 66                 break;  
 67             }  
 68         }  
 69         if(fl)continue;  
 70         if(a[pos]==1){  
 71             fl=1;  
 72             ans++;  
 73             cover(pos,r);  
 74         }  
 75         if(fl)continue;  
 76         for(int j=1;j<=r;j++){  
 77             if(a[L(pos,j)]==1){  
 78                 fl=1;  
 79                 ans++;  
 80                 cover(L(pos,j),r);  
 81                 break;  
 82             }  
 83         }  
 84         if(fl)continue;  
 85         return 0;  
 86     }  
 87     return ans<=k;  
 88 }  
 89 void Binary_Search(){  
 90     int l=0,r=2*n+1,ans=0;  
 91     while(l<=r){  
 92         int mid=(l+r)>>1;  
 93         if(check(mid)){  
 94             ans=mid;  
 95             r=mid-1;  
 96         }  
 97         else l=mid+1;  
 98     }  
 99     printf("%d\n",ans);  
100 }  
101 void work(){  
102     for(int i=1;i<=m;i++){  
103         orbit(i);  
104         Binary_Search();  
105     }  
106 }  
107 int main(){  
108     init();  
109     work();  
110     return 0;  
111 }  
View Code

 

posted @ 2017-11-04 15:42  鲸头鹳  阅读(143)  评论(0编辑  收藏  举报