Codeforces Round #593 (Div. 2) E.Alice and the Unfair Game

Portal: https://codeforces.com/contest/1236/problem/E


Meaning of the questions:

Given a $ 1 * n $ grid, each Alice $ $ move a piece, $ $ Marisa guess where pieces, in order to guess a sequence of the form given

I asked how many sets $ (x, y) $, $ x $ represents the starting point, $ y $ indicates the end, $ Alice $ win

$n<=10^{5}$

sol:

A more obvious nature

A point can be reached leftmost / rightmost point is determined, you can reach the point of a range.

Each series value $ a_i $ affects only a starting point

We just need to claim the starting point for each, limit its left / right limit where on the line qwq

We can go greedy, once considered only a half

For a half, if there is no obstacle we have to go, there are obstacles, then stopped one step before you go

This thing for a half a point, the impact of pretreatment by obstacles, is $ O (1) $ to statistics

Then the overall efficiency is $ O (N) $

#include <bits/stdc++.h>
using namespace std;
int N,M;
int a[100005],b[100005],l[100005],r[100005];
void SolveR(){
    for (int i=1;i<=M;i++)
        b[i]=a[i];
    for (int i=1;i<=M;i++)
        a[i]-=i;
    map<int,int> qwq;
    for (int i=M;i>=1;i--)
        qwq[a[i]]=qwq[a[i]-1]+1;
    for (int i=1;i<=N;i++)
        r[i]=min(i+M+1-qwq[i],N);//get the most right pos
    for (int i=1;i<=M;i++)
        a[i]=b[i];
}
void SolveL(){
    for (int i=1;i<=M;i++)    
        b[i]=a[i];
    for (int i=1;i<=M;i++)
        a[i]+=i;
    map<int,int> qwq;
    for (int i=M;i>=1;i--)
        qwq[a[i]]=qwq[a[i]+1]+1;
    for (int i=1;i<=N;i++)
        l[i]=max(i-M-1+qwq[i],1);//get the most left pos
    for (int i=1;i<=M;i++)    
        a[i]=b[i];
}
int main(){
    scanf("%d%d",&N,&M);
    for (int i=1;i<=M;i++)
        scanf("%d",&a[i]);
    if (N==1) {
        puts("0");
        return 0;
    }
    SolveL();
    SolveR();
    long long ans=0;
    for (int i=1;i<=N;i++)
        ans+=(r[i]-l[i])+1;
    cout<<ans;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/si--nian/p/11716462.html