10.06 National Day Nineth simulation game

Source: Internet
Author: User
Tags gcd greatest common divisor

Key (Key)
Description

In this problem, a key refers to a binary sequence of length \ (3n\) , where \ (n\) is a positive integer.
Each of the sequences is numbered \ (1\) to \ (3n\) from left to right, and the weight of a key refers to the number of neighboring bits with different numbers plus \ (1\) . Like what:
\ (000\) is the weighted value of \ (1\), \ (011010100\) is \ ( 7\).

The key can be modified. To be exact, you can continue to do the following: Select two adjacent bits, and then reverse them. For example, you can modify \ (000\) to 110 by one operation.

Given a key of length \ (3n\) , modify it to a key with a weight of not less than \ (2n\) by not exceeding \ (n\) operations. You can assume that the legal scheme must exist.

Input

Enter a line that contains a \ (01\) sequence of multiples of \ (3\) .

Output

The first line contains an integer \ (m\)that represents the number of operations you need to ensure \ (0 \leq m \leq n\) .

The second line contains \ (m\) positive integers \ (a_1,a_2,\dots,a_m (1 \leq a_i < n) \), which in turn represents each flip of the first \ (a_i\) and the first \ (a_{i+1} \) bit.

If the weight of the initial key is already no less than \ (2n\), you can output only one line of an integer \ (0\).

XJBAnalysis of metaphysical practices\ (50pts\)

? Considering that the answer contributes to the situation, the current position differs from the next position character.

We \ (O (n) \) enumerate the locations to determine if the current position \ (i\) is the same as the next location \ (i+1\) .

If it's different, we can try to flip both positions, but want to contribute to the answer? We need to Judge \ (i\) position and \ ( i+2\) position with \ (i-1\) If the characters exist the same, And decide if we're going to change these two positions.

For example:

At this time we change the position of \ (i\) ,\ (i+1\) ,

At this time, the contribution to the answer will be much more \ (+1\).

And then somehow did it (get\) \ (50pts\)

Positive solution

See \ (3n\), we consider three cases.

These conditions exist.

  1. \ (000\) 5.\ (111\)
  2. \ (001\) 6.\ (110\)
  3. \ (011\) 7.\ (100\)
  4. \ (010\) 8.\ (101\)

We found that

\ (2.6\) situation flipping the middle position can make a contribution larger.

The \ (3.7\) situation flips the leftmost position to make the contribution larger.

This can be tried by hand.

So you can qwq the code simply by simulating it.

Code
#include <cstdio> #include <algorithm> #include <iostream> #include <cstdlib> #include < cmath> #include <string> #include <cstring> #define R registerusing namespace Std;char s[300008];int len,     Cnt,ans[300008],val;int Main () {//Freopen ("Key.in", "R", stdin);//Freopen ("Key.out", "w", stdout);    scanf ("%s", s+1);    Len=strlen (s+1);    for (R int i=1;i<=len;i++) if (s[i]!=s[i-1] and i!=1) val++;        if ((val+1) >=2* (LEN/3)) {puts ("0");    Exit (0); } for (R int i=1;i<=len-2;i+=3) {if (s[i]== ' 0 ' and s[i+1]== ' 0 ' and s[i+2]== ' 1 ') {Ans[++c            nt]=i+1;        s[i+1]= ' 1 '; s[i+2]= ' 0 ';            } if (s[i]== ' 0 ' and s[i+1]== ' 1 ' and s[i+2]== ' 1 ') {ans[++cnt]=i;        s[i]= ' 1 '; s[i+1]= ' 0 ';            } if (s[i]== ' 1 ' and s[i+1]== ' 1 ' and s[i+2]== ' 0 ') {ans[++cnt]=i+1;        s[i+1]= ' 0 '; s[i+2]= ' 1 '; } if (s[i]== ' 1 ' and s[i+1]== ' 0 ' and s[i+2]== ' 0 ')        {ans[++cnt]=i;        s[i]= ' 0 '; s[i+1]= ' 1 ';            } if (s[i]== ' 1 ' and s[i+1]== ' 1 ' and s[i+2]== ' 1 ') {if (i==1) ans[++cnt]=i;                else if (s[i-1]== ' 1 ') {ans[++cnt]=i;                s[i]= ' 0 ';            s[i+1]= ' 0 ';        } else ans[++cnt]=i+1,s[i+1]= ' 0 ', s[i+2]= ' 1 ';            } if (s[i]== ' 0 ' and s[i+1]== ' 0 ' and s[i+2]== ' 0 ') {if (i==1) ans[++cnt]=i;                else if (s[i-1]== ' 0 ') {ans[++cnt]=i;                s[i]= ' 1 ';            s[i+1]= ' 1 ';        } else ans[++cnt]=i+1,s[i+1]= ' 1 ', s[i+2]= ' 0 ';    }} printf ("%d\n", CNT);    for (R int i=1;i<=cnt;i++) printf ("%d", ans[i]);    Fclose (stdin);    Fclose (stdout); return 0;}
Greatest common divisor (GCD)
Description

Makik is an industrious student. He wanted to do some more exercises when he had just finished Euclid's algorithm, so he wrote down a line of length \ (n\) on the paper. After that, he did the \ (m\) round exercise. For the i\ round exercise, he selects an interval from the arrangement \ ([l_i,r_i]\) and greatest common divisor the element 22 in the interval, and then finds the maximum value in these greatest common divisor.

Makik has become a master of greatest common divisor, and has graciously introduced this practice to you. Now, please do it.

Input

The first line contains two numbers \ (n,m\), each representing the length of the arrangement and the number of rounds practiced.
The next line \ (n\) numbers, which in turn represent each element in the arrangement.
Then the \ (m\) line, where the i\ line contains two numbers \ (l_i,r_i\), represents the interval that is picked out in the practice of the first (i\) round.

Output

The output \ (m\) line, one number per line, represents the maximum value in the greatest common divisor of elements in the interval within the range of the round exercise.

XJBAnalysis of violence

For \ (m\) queries, the number of direct enumeration \ ([l_i,r_i]\) interval, 22 ( gcd\) fetch \ (max\).

The code is written roughly as follows:

for(int i=1;i<=m;i++){    scanf("%d%d",&l,&r);    int now=0;    for(int j=l;j<=r;j++)        for(int k=j+1;k<=r;k++)            now=max(now,gcd(a[j],a[k]));}

Time complexity of \ (O (m\times n^2 loga_i) \)

The tle\is that the violent part is all.

Positive solution

Considering the contribution to the answer is the approximate number of these numbers.

So we're doing something about these approximations.

Practice

For each number to calculate the approximate, in a range of more than two times the approximate, then it must be a certain two number of \ (gcd\).

This allows us to maintain the maximum number of occurrences \ (\geq 2\) in the segment tree and update it each time .

But if you do it online, it still doesn't work.

We consider taking the inquiry offline.

How to Offline?

Considering that we have to know the approximate number of the entire interval, we sort the right endpoint of the query.

We sort from small to large right endpoint \ (r_i\) and output ans when we encounter a right endpoint.

Code
#include <cstdio> #include <iostream> #include <algorithm> #include <cctype> #define LS o< <1#define rs o<<1|1#define N 100005#define R registerusing namespace std;inline void in (int &x) {int f=1;x    =0;char S=getchar ();    while (!isdigit (s)) {if (s== '-') F=-1;s=getchar ();}    while (IsDigit (s)) {x=x*10+s-' 0 '; S=getchar ();} X*=f;}    struct cod{int l,r,idx;    BOOL operator < (const cod&a) const {return r<a.r; }}que[100008];int ans[n];int n,m,a[n],tr[n<<3],exist[n];inline void up (int o) {Tr[o]=max (Tr[ls],tr[rs]);}        void build (r int o,r int L,r int r) {if (l==r) {tr[o]=0;    Return    } int mid= (L+R) >>1;    Build (Ls,l,mid);    Build (Rs,mid+1,r); Up (o);}    void change (int o,int l,int r,int pos,int del) {//printf ("%d%d%d%d", O,l,r,pos,del);    if (l==r) {Tr[o]=max (Tr[o],del); return;}    int mid= (L+R) >>1;    if (pos<=mid) change (Ls,l,mid,pos,del);    else change (Rs,mid+1,r,pos,del); Up (o);} int query (int o,int l,int r, int x,int y) {//printf ("%d%d%d%d%d\n", o,l,r,x,y);    if (X<=l and r<=y) return tr[o];    int mid= (L+R) >>1,res=0;    if (x<=mid) Res=max (Res,query (ls,l,mid,x,y));    if (y>mid) Res=max (Res,query (rs,mid+1,r,x,y)); return res;}    int main () {//Freopen ("Gcd.in", "R", stdin);//Freopen ("Gcd.out", "w", stdout);    In (n), in (m);        {for (R int i=1;i<=n;i++) in (A[i]);        Build (1,1,n);        for (R int i=1;i<=m;i++) in (QUE[I].L), in (QUE[I].R), que[i].idx=i;        Sort (que+1,que+m+1);        int now=1;                for (r int i=1;i<=n;i++) {for (r int j=1;j*j<=a[i];j++) {if (a[i]%j==0) {if (exist[j]) change (1,1,N,EXIST[J],J);//Before judging if there is an if (exist[a[i]/j]) ch                Ange (1,1,n,exist[a[i]/j],a[i]/j);//The existence of exist[j]=i,exist[a[i]/j]=i before judging;         }} while (I==QUE[NOW].R and now<=m) {       Ans[que[now].idx]=query (1,1,N,QUE[NOW].L,QUE[NOW].R);            now++;    }} for (R int i=1;i<=m;i++) printf ("%d\n", Ans[i]);    } fclose (stdin);    Fclose (stdout); return 0;}

10.06 National Day Nineth simulation game

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.