D. Sweets for Everyone!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
For he knew every Who down in Whoville beneath, Was busy now, hanging a mistletoe wreath. "And they're hanging their stockings!" he snarled with a sneer, "Tomorrow is Christmas! It's practically here!"
Dr. Suess, How The Grinch Stole Christmas
Christmas celebrations are coming to Whoville. Cindy Lou Who and her parents Lou Lou Who and Betty Lou Who decided to give sweets to all people in their street. They decided to give the residents of each house on the street, one kilogram of sweets. So they need as many kilos of sweets as there are homes on their street.
The street, where the Lou Who family lives can be represented as n consecutive sections of equal length. You can go from any section to a neighbouring one in one unit of time. Each of the sections is one of three types: an empty piece of land, a house or a shop. Cindy Lou and her family can buy sweets in a shop, but no more than one kilogram of sweets in one shop (the vendors care about the residents of Whoville not to overeat on sweets).
After the Lou Who family leave their home, they will be on the first section of the road. To get to this section of the road, they also require one unit of time. We can assume that Cindy and her mom and dad can carry an unlimited number of kilograms of sweets. Every time they are on a house section, they can give a kilogram of sweets to the inhabitants of the house, or they can simply move to another section. If the family have already given sweets to the residents of a house, they can't do it again. Similarly, if they are on the shop section, they can either buy a kilo of sweets in it or skip this shop. If they've bought a kilo of sweets in a shop, the seller of the shop remembered them and the won't sell them a single candy if they come again. The time to buy and give sweets can be neglected. The Lou Whos do not want the people of any house to remain without food.
The Lou Whos want to spend no more than t time units of time to give out sweets, as they really want to have enough time to prepare for the Christmas celebration. In order to have time to give all the sweets, they may have to initially bring additional k kilos of sweets.
Cindy Lou wants to know the minimum number of k kilos of sweets they need to take with them, to have time to give sweets to the residents of each house in their street.
Your task is to write a program that will determine the minimum possible value of k.
Input
The first line of the input contains two space-separated integers n and t (2 ≤ n ≤ 5·105, 1 ≤ t ≤ 109). The second line of the input contains n characters, the i-th of them equals "H" (if the i-th segment contains a house), "S" (if the i-th segment contains a shop) or "." (if the i-th segment doesn't contain a house or a shop).
It is guaranteed that there is at least one segment with a house.
Output
If there isn't a single value of k that makes it possible to give sweets to everybody in at most t units of time, print in a single line "-1" (without the quotes). Otherwise, print on a single line the minimum possible value of k.
Examples
input
Copy
6 6 HSHSHS
output
Copy
1
input
Copy
14 100 ...HHHSSS...SH
output
Copy
0
input
Copy
23 50 HHSS.......SSHHHHHHHHHH
output
Copy
8
Note
In the first example, there are as many stores, as houses. If the family do not take a single kilo of sweets from home, in order to treat the inhabitants of the first house, they will need to make at least one step back, and they have absolutely no time for it. If they take one kilogram of sweets, they won't need to go back.
In the second example, the number of shops is equal to the number of houses and plenty of time. Available at all stores passing out candy in one direction and give them when passing in the opposite direction.
In the third example, the shops on the street are fewer than houses. The Lou Whos have to take the missing number of kilograms of sweets with them from home.
给出一系列字符串,H代表家,S代表商店,A可以带着一些糖果K,A需要给所有的H一个糖果,途经糖果店只能这一次获得一个糖果,问是否可以在t范围内给予糖果,求符合条件下最小的需要带的糖果数。
思路:
二分,先判断出带了这些糖果,走完一遍是否可以发放给所有的H。同时标记出我们需要处理的最后位置ends,这个ends代表ends以后的不需要处理了。糖果数不满足直接return 0。
然后遍历从0到ends,时间ans初始化为0,nums值为二分的那个值。
出现H,num值-1,这时如果num为-1,我们从最后ends到i这个点,计算这个时间2*(ends-i),如果这个时间+ans<=t,直接return 1,否则last=i,记录第一个不足的位置,因为这时不需要考虑糖果是否满足了,因为第一步的时候已经判断过。
出现S,num+1,这时如果num为0,这说明它正好填补了以前的空缺,从它走到刚开始空缺的地方,计算之间时间为2*(i-last)。ans加上这个数。
走完一遍,判断时间即可。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn=500000+66;
const ll mod=1e9+7;
ll n,k;
char ch[maxn];
bool check(ll x)
{
ll num=x;
ll ends=0;
for(ll i=0; i<n; i++)
{
if(ch[i]=='H')
{
num--;
ends=i;
}
else if(ch[i]=='S')
{
num++;
if(num==0)
ends=i;
}
}
if(num<0)
return 0;
ll ans=0;
ll last=-1;
num=x;
for(ll i=0; i<=ends; i++)
{
ans++;
if(ch[i]=='H')
{
num--;
if(num==-1)
{
ll backs=(ends-i)*2;
if(backs+ans<=k)
return 1;
last=i;
}
}
else if(ch[i]=='S')
{
num++;
if(num==0)
{
ll backs=(i-last)*2;
ans+=backs;
}
}
}
return ans<=k;
}
int main()
{
scanf("%lld %lld",&n,&k);
//不超过k个
cin>>ch;
ll l=0,r=n;
while(l<r)
{
//求最小值
ll mid=(l+r)/2;
if(check(mid))//最小需要带的糖果
{
r=mid;
}
else
{
l=mid+1;
}
}
printf("%lld\n",check(l)?l:-1);
}