Codeforces Round #152 (Div. 2) D. Sweets for Everyone!(二分)
大致题意:有一些商店和一些居民在一排上,某人要从起点開始对全部居民发放1kg糖果,每一个商店能够能够且仅能够买一次1kg糖果,每向前或向后走以单位须要1时间单位,求在t时间内发完所以的居民最少须要自身带多少kg糖果
思路:这样的题,明显就是二分,二分答案。然后从边界開始判可行。这题的可行还是有点难判。首先糖果手中充足,遇到一个居民就发放1kg。当糖果不充足的时候。仅仅能有两种决策,直接跑到尽头。再回来。
或者继续往前走,知道糖果数已经集够所需再回来。
来差分后的序列来说就是,当sum[i] == -1 && sum[i-1] == 0 时。直接跑到尽头,或者。继续走到sum[i] == 0 && sum[i-1] == -1的位置,再回去,再回来。
始终是这样两种决策,能够画绘图就能看出来
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <string> #include <vector> #include <cstdio> #include <ctime> #include <bitset> #include <algorithm> #define SZ(x) ((int)(x).size()) #define ALL(v) (v).begin(), (v).end() #define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i) #define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i) #define REP(i,n) for ( int i=1; i<=int(n); i++ ) #define rep(i,n) for ( int i=0; i< int(n); i++ ) using namespace std; typedef long long ll; #define X first #define Y second typedef pair<int,int> pii; template <class T> inline bool RD(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c<'0' || c>'9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ?0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void PT(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) PT(x / 10); putchar(x % 10 + '0'); } const int N = 5e5+100; int n,lim,MAX; int sum[N]; int a[N]; char s[N]; bool ok(int x,int maxx){ int cnt = x; int cur = 0; int tmp = 0x3f3f3f3f; int pos,last = 0; REP(i,n) { if( s[i] == 'H') cnt--; else if( s[i] == 'S') cnt++; if( cnt == 0 && last == -1) maxx = max(maxx,i); last = cnt; } cnt = x , last = 0; for(int i = 1; i <= maxx; i++){ cur ++; if( s[i] == 'H') cnt--; else if( s[i] == 'S') cnt++; if( last == 0 && cnt == -1) { tmp = min(tmp,cur + 2*(maxx-i)); pos = i; }if( last == -1 && cnt == 0){ cur += 2*(i-pos); } last = cnt; } return min(tmp,cur) <= lim; } int main(){ scanf("%d%d%s",&n,&lim,s+1); int ch = 0,cs = 0; REP(i,n){ if( s[i] == 'H') MAX = i,ch++; else if( s[i] == 'S') cs++; } int lb = max(0,ch-cs)-1; int ub = 0x3f3f3f3f; if(MAX > lim){ puts("-1"); return 0; } while(ub-lb > 1){ int mid = (lb+ub)/2; if( ok(mid,MAX) ) ub = mid; else lb = mid; } printf("%d\n",ub); }
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.
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.
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.
6 6 HSHSHS
1
14 100 ...HHHSSS...SH
0
23 50 HHSS.......SSHHHHHHHHHH
8
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.