Codeforces1071c triple flips [constructor] [four Russians]

Source: Internet
Author: User

Question Analysis:

Which of the following questions can be considered first. We found that we can choose three consecutive operations without considering the number of operations.

In this way, we can always convert a sequence into the form of $000... 000xy $. In other words, for $000... 0001 $, $000... 0010 $, $000... 0011 $, consider no solution conditions.

At this time, write a brute-force program and use the $ O (N ^ 2*2 ^ N) $ algorithm to find that the total length is less than $7 $, the other two values are less than $8 $.

Then, observe that the number of operations required by the question is related to $ N/3 $. It is not difficult to think of every $3 $ into a group, and then change it to $0 $ in the group to allow the use of out-of-group information, each group can only be operated once. In this case, the number of operations exceeds ($011 or 110 $ ).

Then, let's relax the limit. Every $6 $ is divided into one group and adjusted to $0 $ in the group. You can use the information outside the group. Each group can only be operated twice. In this way, I thought that $110111 was not acceptable at first. After I wrote a brute force code, I found my brain dead ).

In this case, we will consider four Russians, pre-process how to proceed to each $6 $ group, and finally we will be able to defend against the remaining violence.

 

Code:

  1 #include<bits/stdc++.h>  2 using namespace std;  3   4 const int maxn = 102000;  5   6 int n,a[maxn],f[1<<12];  7 struct node{int x,y,z;};  8 vector <node> des[64];  9  10 void init(){ 11     queue<int> q; 12     int N = 6; 13     memset(f,0x3f,sizeof(f)); 14     f[0] = 0; q.push(0); 15     while(!q.empty()){ 16     int k = q.front();q.pop(); 17     for(int i=0;i<N;i++){ 18         if(f[k^(1<<i)] > f[k] + 1){ 19         for(int j=0;j<des[k].size();j++) 20             des[k^(1<<i)].push_back(des[k][j]); 21         des[k^(1<<i)].push_back((node){i,6,12-i}); 22         f[k^(1<<i)] = f[k]+1; 23         q.push(k^(1<<i)); 24         } 25         for(int j=i+1;j<N;j++){ 26         int z = (1<<i)+(1<<j); 27         if(j+(j-i) < N) z |= (1<<2*j-i); 28         if(f[k^z] > f[k]+1){ 29             for(int fr=0;fr<des[k].size();fr++) 30             des[k^z].push_back(des[k][fr]); 31             des[k^z].push_back((node){i,j,2*j-i}); 32             f[k^z] = f[k]+1; 33             q.push(k^z); 34         } 35         } 36     } 37     } 38 } 39  40 node opt[maxn]; 41 int num; 42  43 node lst[maxn]; 44  45 void Remain(){ 46     queue<int> q; 47     memset(f,0x3f,sizeof(f)); 48     //for(int i=0;i<64;i++) des[i].clear(); 49     f[0] = 0;q.push(0); 50     int N = min(n,12); 51     while(!q.empty()){ 52     int k = q.front();q.pop(); 53     for(int i=0;i<N;i++){ 54         for(int j=i+1;j<N;j++){ 55         if(j+(j-i) >= N) break; 56         int z = (1<<i)+(1<<j)+(1<<2*j-i); 57         if(f[k^z] > f[k]+1){ 58             f[k^z] = f[k]+1; 59             lst[k^z] = (node){i,j,2*j-i}; 60             q.push(k^z); 61         } 62         } 63     } 64     } 65     int z = 0; 66     for(int i=n-N+1;i<=n;i++){z = (z<<1)+a[i];} 67     if(f[z] > 1e8){puts("NO");} 68     else{ 69     puts("YES"); 70     printf("%d\n",num+f[z]); 71     for(int i=1;i<=num;i++){ 72         printf("%d %d %d\n",opt[i].x,opt[i].y,opt[i].z); 73     } 74     while(z){ 75         printf("%d %d %d\n",n-lst[z].z,n-lst[z].y,n-lst[z].x); 76         int pp = z; 77         z ^= (1<<lst[pp].x); z ^= (1<<lst[pp].y); z^= (1<<lst[pp].z); 78     } 79     } 80 } 81  82 void work(){ 83     for(int i=1;i<=n;i+=6){ 84     if(i+12 > n) break; 85     int z = (a[i+5]<<5)+(a[i+4]<<4)+(a[i+3]<<3)+(a[i+2]<<2)+(a[i+1]<<1)+a[i]; 86     for(int j=0;j<des[z].size();j++){ 87         opt[++num] = des[z][j]; 88         opt[num].x += i; opt[num].y += i; opt[num].z += i; 89         a[opt[num].x] ^= 1; a[opt[num].y] ^= 1; a[opt[num].z] ^= 1; 90     } 91     } 92     Remain(); 93 } 94  95 int main(){ 96     scanf("%d",&n); 97     for(int i=1;i<=n;i++) scanf("%d",&a[i]); 98     init(); 99     work();100     return 0;101 }

 

Codeforces1071c triple flips [constructor] [four Russians]

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.