Codeforces Round #426 (Div. 2)B. The Festive Evening

本文介绍了一个关于城堡入口管理和警卫分配的问题。在一个拥有26个入口的城堡中,需要合理安排警卫,确保每个入口在有客人到达时都有警卫在场。文章详细解释了问题背景、输入输出格式及示例,并提供了一段C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. The Festive Evening
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

It’s the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom of Sweetland in case it turns out to reach the wrong hands. So it’s a necessity to not let any uninvited guests in.

There are 26 entrances in Jelly Castle, enumerated with uppercase English letters from A to Z. Because of security measures, each guest is known to be assigned an entrance he should enter the castle through. The door of each entrance is opened right before the first guest’s arrival and closed right after the arrival of the last guest that should enter the castle through this entrance. No two guests can enter the castle simultaneously.

For an entrance to be protected from possible intrusion, a candy guard should be assigned to it. There are k such guards in the castle, so if there are more than k opened doors, one of them is going to be left unguarded! Notice that a guard can’t leave his post until the door he is assigned to is closed.

Slastyona had a suspicion that there could be uninvited guests at the evening. She knows the order in which the invited guests entered the castle, and wants you to help her check whether there was a moment when more than k doors were opened.

Input
Two integers are given in the first string: the number of guests n and the number of guards k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26).

In the second string, n uppercase English letters s1s2… sn are given, where si is the entrance used by the i-th guest.

Output
Output «YES» if at least one door was unguarded during some time, and «NO» otherwise.

You can output each letter in arbitrary case (upper or lower).

Examples
input
5 1
AABBB
output
NO
input
5 1
ABABB
output
YES
Note
In the first sample case, the door A is opened right before the first guest’s arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed after the fifth one arrives. One guard can handle both doors, as the first one is closed before the second one is opened.

In the second sample case, the door B is opened before the second guest’s arrival, but the only guard can’t leave the door A unattended, as there is still one more guest that should enter the castle through this door.
怎们说这是思维问题吧,好吧cf水体都是思维题,好吧,现在还没这个思维,
题意
一个城堡,一共有A-Z入口,A口A进,B口B进,给n个人的进入顺序,有K个警卫,一个口至少要一个警卫,直到该口没人再进他才能去其他口做警卫.

解题,直接模拟 ,记录每个出口的关闭位置,第一次进入cnt+1 到达该口最后一个位置,cnt-1;

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<map>
using namespace std;
string str;
int a[(int)1e6+6];//是否为第一个进入;
int main()
{
   int b[30];
   int n,m,i,j;
   while(~scanf("%d%d",&n,&m))
   {
       cin>>str;
       memset(b,-1,sizeof(b));
       memset(a,0,sizeof(a));
       for(i=n-1;i>=0;i--)
       {
           if(b[str[i]-'A']==-1)
            b[str[i]-'A']=i;
       }
       int ok=0,cnt=0;

       for(i=0;i<n;i++)
       {
           if(!a[str[i]-'A'])
           {
               a[str[i]-'A']=1;
               cnt++;
               if(cnt>m)
               {
                   ok=1;
                   break;
               }
           }
           if(b[str[i]-'A']==i)
            cnt--;
       }
       if(ok)
        puts("YES");
       else
        puts("NO");
   }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值