codeforces # 1248D2. The World Is Just a Programming Task (bracket matching process into a broken line)

Topic links:

http://codeforces.com/contest/1248/problem/D2

Meaning of the questions:

Can perform operations once the characters exchange

String operation such that after cyclic shift and the most successful match program

Program number and the output up to the exchange position

data range:

$ 1 \ leq n \ leq 300 000 $

analysis: 

Reference blog: https://www.cnblogs.com/LLTYYC/p/11718968.html

For a string, it is seeking the number of cyclic shift matching scheme

To turn a string into a line starting from 0, encountered (plus one is encountered) then decrements

If different (and) the number, obviously the answer is 0, otherwise, the answer is the number of vertices of the polyline y minimum

What may be offset, such that the minimum value is 0, then the overall upward shift polyline

Return 0 if the broken line, the answer is a plus

Then consider a case can be exchanged

Upward shift of the first fold line

The answer is the answer number 1 or the number 2 added is not exchanged, i.e., change the minimum value is -1, 0 or increasing the number of the two operation scheme

Wherein the number is a minimum of the interval 1, the number of section 2 of empathy

AC Code:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int maxn=3e5+7;
int n;
char S[maxn];
int val[maxn];
int main(){
    scanf("%d",&n);
    scanf("%s",S+1);
    int off=0,minn=1e9;
    int now=0;
    for(int i=1;i<=n;i++){
        if(S[i]==')')now--;
        else now++;
        if(minn>now){
            minn=now;
            off=i;
        }
    }
    if(now!=0){
        printf("0\n1 1\n");
        return 0;
    }
    int ans=0,ansl=1,ansr=1,anss=0;
    for(int i=1;i<=n-1;i++){
        val[i+1]=val[i];
        int v=(off+i-1)%n+1;
        if(S[v]==')')val[i+1]--;
        else val[i+1]++;
        if(val[i]==0)ans++;
        //cout<<val[i]<<" ";
    }
   // coutMM
    anss=ans;
    for(int i=1;i<=n+1;i++){
        if(val[i]==2){
            int en=i+1,tem=1;
            while(val[en]>=2){
                if(val[en]==2)tem++;
                en++;
            }
            if(tem+anss>ans){
                ans=tem+anss;
                ansl=(i+off-2+n)%n+1;
                ansr=(en+off-2+n)%n+1;
            }
            i=en;
        }
    }
    for(int i=1;i<=n+1;i++){
        //cout<<val[i]<<" ";
        if(val[i]==1){
            int en=i+1,tem=1;
            while(val[en]>=1){
                if(val[en]==1)tem++;
                en++;
            }
            if(tem>ans){
                ans=tem;
                ansl=(i+off-2+n)%n+1;
                ansr=(en+off-2+n)%n+1;
            }
            i=en;
        }
    }

    printf("%d\n%d %d\n",ans,ansl,ansr);
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/carcar/p/11773496.html