Codeforces1161 A. Hide and Seek(思维)

Posted live4m

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces1161 A. Hide and Seek(思维)相关的知识,希望对你有一定的参考价值。

题意:

解法:

关键点在于题目最多只进行一次移动.

枚举起点,判断移动和不移动的三个终点是否有解即可.

预处理l[i]和r[i]表示点i第一次和最后一次被检查的时间.

枚举起点i,那么我们需要在l[i]之前将点移走,
如果r[i-1]<l[i],那么移动到左边有解,
如果r[i+1]<l[i],那么移动到右边有解,
如果点i不会被检查,那么步移动有解.

累加有解的次数,输出即可.

code:

#include<bits/stdc++.h>
// #define SYNC_OFF
typedef std::vector<int> VE;
typedef std::pair<int,int> PI;
#define int long long
#define ll long long
#define ull unsigned long long
//fast-coding
#define ST(x) x.begin()
#define ED(x) x.end()
#define RST(x) x.rbegin()
#define RED(x) x.end()
#define CL(x) x.clear();
#define all(a,n) a+1,a+1+n
#define ff(i,n) for(ll i=1;i<=n;i++)
#define rff(i,n) for(ll i=n;i>=1;i--)
#define fff(i,n) for(ll i=0;i<n;i++)
#define rfff(i,n) for(ll i=n-1;i>=0;i--)
#define SC(x) scanf("%s",x)
#define SL(x) strlen(x)
#define pss(a) push_back(a)
#define ps(a) push(a)
#define SZ(x) (int)x.size()
#define pee puts("");
#define eee putchar(' ');
#define re readdd()
#define pr(a) printtt(a)
int readdd(){int x=0,f=1;char c=getchar();//
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c))x=x*10+c-'0',c=getchar();
return f*x;}
void printtt(int x){if(x<0)putchar('-'),x=-x;//
if(x>=10)printtt(x/10);putchar(x%10+'0');}
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}//
int ppow(int a,int b,int mod){a%=mod;//
int ans=1%mod;while(b){if(b&1)ans=(long long)ans*a%mod;
a=(long long)a*a%mod;b>>=1;}return ans;}
bool addd(int a,int b){return a>b;}
int lowbit(int x){return x&-x;}
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
bool isdigit(char c){return c>='0'&&c<='9';}
bool Isprime(int x){
    for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
    return 1;
}
void ac(int x){if(x)puts("YES");else puts("NO");}
//
using namespace std;
// const int mod=998244353;
const int mod=1e9+7;
const int maxm=2e6+5;
int l[maxm],r[maxm];
int x[maxm];
int n,k;
void solve(){
    n=re,k=re;
    ff(i,k)x[i]=re;
    ff(i,n){
        l[i]=1e9;
        r[i]=-1e9;
    }
    ff(i,k){
        l[x[i]]=min(l[x[i]],i);
        r[x[i]]=max(r[x[i]],i);
    }
    int ans=0;
    ff(i,n){//枚举起点
        //不动
        if(l[i]>r[i])ans++;//不会访问
        //需要在l[i]之前移走
        //左移
        if(i-1>=1&&r[i-1]<l[i])ans++;
        //右移
        if(i+1<=n&&r[i+1]<l[i])ans++;
    }
    pr(ans);pee;
}
void Main(){
    // #define MULTI_CASE
    #ifdef MULTI_CASE
    int T;cin>>T;while(T--)
    #endif
    solve();
}
void Init(){
    #ifdef SYNC_OFF
    ios::sync_with_stdio(0);cin.tie(0);
    #endif
    #ifndef ONLINE_JUDGE
    freopen("../in.txt","r",stdin);
    freopen("../out.txt","w",stdout);
    #endif
}
signed main(){
    Init();
    Main();
    return 0;
}

以上是关于Codeforces1161 A. Hide and Seek(思维)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1161C(博弈)

@codeforces - 1161F@ Zigzag Game

Codeforces 1161B(判断旋转对称)

Codeforces1161 C. Thanos Nim(博弈)

Codeforces1161 B. Chladni Figure(枚举因子+check)

Codeforces1161 D. Palindrome XOR(思维,并查集缩点+二分图染色)