CCF/CSP-201812-3-CIDR合并

 

 

 

按照题意模拟,但是只有90,不想找bug了,毕竟算法不是自己想的,找起来也麻烦。

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define inf 0x3f3f3f3f
 4 #define LL long long
 5 #define pui pair<unsigned int,int>
 6 #define mp make_pair
 7 #define x first
 8 #define y second
 9 
10 const int maxn=1000005;
11 pui a[maxn];
12 vector<pui>ans;
13 int n,q[4]={24,16,8,0},l[]={0,8,16,24,32};
14 int base[]={(1<<24),(1<<16),(1<<8),1};
15 ostream& operator << (ostream& out,pui& u){
16     for(int i=0;i<4;++i){
17         out<<u.x/base[i]%base[2];
18         if(i<3)out<<".";
19     }out<<"/"<<u.y;
20     return out;
21 }
22 bool issub(int i,int j){
23     if(a[i].y==a[j].y){
24         if(a[i].x==a[j].x){return 1;}
25         else{return 0;}
26     }else if(a[i].y>a[j].y)return 0;
27     else{
28         if((a[j].x>>(32-a[i].y))==(a[i].x>>(32-a[i].y)))return 1;
29         else return 0;
30     }
31 }
32 bool ok(pui A,pui B){
33     pui tmp;tmp.x=A.x;tmp.y=A.y-1;
34     if(A.y==B.y &&tmp.y>=0  && ((A.x>>(32-A.y)>>1)==(B.x>>(32-A.y)>>1))
35        && (((A.x>>(32-A.y))&1)^((B.x>>(32-A.y))&1))==1 ) return 1;
36     return 0;
37 }
38 int main(){
39     string str;
40     ios::sync_with_stdio(false);
41     cin>>n;
42     for(int i=0;i<n;++i){
43         a[i].y=a[i].x=0;
44         cin>>str;
45         int pos=str.find('/'),cur=0,sz=str.size();
46         unsigned tmp=0;
47         for(int ii=0;ii<sz&&str[ii]!='/';++ii){
48             if(str[ii]=='.'){
49             a[i].x+=(tmp<<q[cur++]);
50             tmp=0;
51             }else{
52             tmp=tmp*10+(str[ii]-'0');
53             }
54         }
55         if(tmp){
56             a[i].x+=(tmp<<q[cur++]);
57         }
58         if(pos!=string::npos){
59             for(int iii=pos+1;iii<sz;++iii) a[i].y=a[i].y*10+(str[iii]-'0');
60         }else{
61             a[i].y=l[cur];
62         }
63     }
64     sort(a,a+n);
65     for(int i=0;i<n;++i){
66         if(a[i].y==-1)continue;
67         int k=1;
68         while(i+k<=n && issub(i,i+k)) a[i+k].y=-1,k++;
69         i=i+k-1;
70     }
71 
72     for(int i=0;i<n;++i){
73         if(a[i].y==-1){continue;}
74         pui B=a[i];
75         while(ans.size()&&ok(ans.back(),B)){
76             B.x=ans.back().x;B.y=ans.back().y-1;
77             ans.pop_back();
78         }
79         ans.push_back(B);
80     }
81     for(pui u:ans) cout<<u<<endl;
82     return 0;
83 }

 

posted @ 2020-03-10 10:44  *zzq  阅读(280)  评论(0编辑  收藏  举报