Codeforces 1236E. Alice and the Unfair Game

Portal

First, it can be noted for a fixed starting point $ S $, it will eventually come to the end of a certain period is the interval

This is easy to prove by contradiction, assuming that there is a legitimate breakpoint interval around this point can be used as the end point

Then divided into sections breakpoint at the beginning of the left and right starting point to discuss, the starting point must be clear that as the end itself

Then now as far as the starting point can come from each of the leftmost and rightmost position to calculate the answer

Consider first right (For convenience the first $ i $ times said to be asking $ i $ the first day)

Set the starting point for the $ x $ then we can always go to the right until it encounters one day just had to go ask the next position, and then the time will stop once

Set up walking for $ y $-day encounter this situation, then there is $ x + y = a [y] $, ie, $ x = a [y] -y $, then wait step after completion we will find the next $ so that Y $ $ x + 1 + y = a [y] $

Set $ ​​f (x, y) $ represents the position of the first $ y $ from $ x $-day departure to reach the far right, as long as the calculated $ f (x, y) $ right the first stop position then becomes the right position became more and greater number of days of sub-problems

Provided $ z $ satisfies the minimum $ x + z = a [y + z] $ number, then the conversion is equivalent to about $ xy = a [y + z] - (y + z) $

The first stop is clearly the right position can be maintained as long as the time considered one upside down, with a $ map $ maintain at the current time in the future leftmost first $ a [i] -i $ inquiry

While noting that only when the position just $ a [i] -1 $, $ i $ when the time will stop once, then we want to know the location and would only ask about the time, so you can maintain time

It boils down to this time $ i $ descending enumeration, put forward a $ map $ maintenance at the current minimum of $ j> i $ so that $ a [j] -j == (a [i] -1) - i $

Then you can find the $ R [i] $ express right away just in time $ i $ stop after a day of continued walking and went to the far right ($ R [i] = R [j] $, $ j = map [ (a [i] -1) -i] $, meaning to find a point of time in the transfer is stopped)

Of course, if there are no $ map $ legally $ j $, then that do not stop, go to the end of time

To the left is the same practice, learn to push yourself above ideas about it

Indeed Map $ $ buckets can substitute, as long as the index can be collectively added $ m $

Reference Code:  Farhod_Farmon

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=1e5+7;
int n,m,A[N],L[N],R[N];
map <int,int> ml,mr;
inline int find_r(int x,int i) { return mr[x-i] ? R[mr[x-i]] : min(x+(m-i)+1,n); }
inline int find_l(int x,int i) { return ml[x+i] ? L[ml[x+i]] : max(x-(m-i)-1,1); }
int main()
{
    n=read(),m=read();
    for(int i=1;i<=m;i++) A[i]=read();
    if(n==1)  { printf("0\n"); return 0; }
    for(int i=m;i>=1;i--)
    {
        L [I]find_l = (A [I] + . 1 , I); R & lt [I] = find_r (A [I] - . 1 , I); 
        ml [A [I] + I] = I; of Mr [A [I] -i ] = I; 
    } 
    LL ANS = 0 ;
     for ( int I = . 1 ; I <= n-; I ++ ) 
        ANS + = find_r (I, 0 ) -i + . 1 + I-find_l (I, 0 ); // intermediate that +1 is the starting point itself as a contribution to the end of the 
    printf ( " % LLD \ the n- " , ANS);
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/LLTYYC/p/11706387.html