Codeforces Round #426 B

The Festive Evening

题意:有n个人要进入城堡,但是标号为A的只能从A门进入,并且每个人进入城堡的时间是有顺序的,并且不会有2个人同时进入,每个门在第一个人进入时打开,最后一个人进入后关闭,现在有k个守卫,每个守卫守的门在关闭前都不能离开去其他门,问有没有那个时刻有门没有守卫

思路:打标记wa得莫名其妙,直接map标记扫描线好了,但是由于存在比如A门在i时刻打开,然后就关闭,也就是A只出现一次,所以标记-1的位置应该往后移一位

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define ll long long
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const long long INF = 1e18+1LL;
const int inf = 1e9+1e8;
const int N=1e6+100;
const ll mod=1e9+7;

char s[N];
int n, k, vis[150];
map<int,int> M;
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    cin>>s+1;
    for(int i=1; i<=n; ++i){ //cout<<s[i]-'A'<<" ";
        if(vis[s[i]]==0){
            M[i]++;
            vis[s[i]]=1;
        }
    }
    mem(vis,0);
    for(int i=n; i>=1; --i){
        if(vis[s[i]]==0){
            M[i+1]--;
            vis[s[i]]=1;
        }
    }
    int o=0;
    for(auto i : M){
        o+=i.second;
        if(o>k){
            cout<<"YES
";
            return 0;
        }
    }
    cout<<"NO
";
    return 0;
}
原文地址:https://www.cnblogs.com/max88888888/p/7265435.html