zoukankan      html  css  js  c++  java
  • Codeforces Round #426 (Div. 2)

    题目链接:http://codeforces.com/contest/834/problem/B

    题意:一共有26个门(A-Z表示),然后现在有n个人要走的门和k个守卫。每当有人要经过某个门时,门要一直打开直到最后一个要通过这扇门的人通过之后才关闭,并且在门打开的期间要有一个守卫看守,每个守卫只能看守一个门,当门关闭后守卫该门的守卫才能分配到其他的门。问你是否存在守卫不够用的情况。存在输出YES,否则输出NO。

    思路:由于只有26个门,那么记录每个门打开的时间点和关闭的时间点。当遇到门打开的时,k-1,当遇到门关闭时k+1,如果存在k<0说明守卫不够用了。

    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<string>
    #include<queue>
    #include<vector>
    #include<time.h>
    #include<cmath>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long int LL;
    const int MAXN = 1e6 + 24;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9 + 7;
    char str[MAXN];
    int st[26], ed[26];
    int main(){
        int n, k;
        while (~scanf("%d%d", &n,&k)){
            scanf("%s", str); 
            for (int i = 0; i < 26; i++){
                st[i] = ed[i] = -1;
            }
            for (int i = 0; i < n; i++){
                if (st[str[i] - 'A'] == -1){
                    st[str[i] - 'A'] = i;
                    ed[str[i] - 'A'] = i;
                }
                else{
                    ed[str[i] - 'A'] = max(ed[str[i]-'A'],i);
                }
            }
            bool flag = true;
            for (int i = 0; i < n&&flag; i++){
                if (st[str[i] - 'A'] == i){
                    k--;
                    if (k < 0){ flag = false; }
                }
                if (ed[str[i] - 'A'] == i){
                    k++;
                }
            }
            printf(flag ? "NO
    " : "YES
    ");
        }
        return 0;
    }
  • 相关阅读:
    学习笔记2
    带有循环的存储过程
    经典SQL语句大全
    关于职业的一些看法
    把dataTable表批量的写入数据库
    抽奖接口,每天只能抽奖3次,而且必须先登录才能抽奖的小程序
    调用获取学生信息的接口,保存到excel里面的小程序
    内置函数补充
    好用的模块
    网络编程
  • 原文地址:https://www.cnblogs.com/kirito520/p/7264102.html
Copyright © 2011-2022 走看看