标签:bin style tca 一个人 nat send stream %s poi
Sometimes, technical terms implicate some life philosophy. Mutex is one of them. On your way to dream, you may be locked by some difficulties, and you need someone to stop his step, and help you get through them.
To help you know better about the life philosophy inside mutex, Rikka comes up with a simple task. Maybe some of you know little about mutex, so she uses another scene to replace it.
There are nn gates in a row, several people in the left side of the gates and all of them want to go to the right side. There are two kinds of gates: black and white. These people share energy, which is represented by a non-negative number EE. Initially, E=0E=0.
If one person walks through a white gate, he will gain one point of energy, i.e., EE will be added by 11. And if one person walks through a black gate, he will lose one point of energy, i.e., EE will be subtracted by 11. Since EE must be a non-negative integer, if E=0E=0, no one can walk through a black gate until someone walks through a white gate. You can assume there won‘t be two people moving at the same time and all the people are selfless.
We use P to represent a black gate, V to represent a white gate and use a PV string to represent the row. Initially, all the people are at the beginning of the string, and all of them want to go through the whole string. But unfortunately, sometimes it may be impossible. So, they want to send at least one person to the right side.
Your task is to find out the minimal number of people which this group needs to achieve this goal.
For example, if the row is VPP, they need at least two people: The first person walk through the first white gate and the second person can use this point of energy to go through the whole string.InputThe first line contains a single numner t(1≤t≤103)t(1≤t≤103), the number of the testcases.
For each testcase, the first line contains a PV string s(1≤|s|≤105)s(1≤|s|≤105) describing the gates.
The input guarantees that there are at most 3030 testcases with |S|>1000|S|>1000.OutputFor each testcase, output a single integer, the answer. And if it is impossible, output −1−1.
Sample Input4 VPP VPPVVVVPPPPPPPP VPPPPPPPPPPPPPP PSample Output
2 3 14 -1
题目大意:给定一串字符串由V和P组成,一个人的初始生命值为0,遇V+1,遇P-1,同伴中可以分享生命值,在保证生命值不为负的情况下,求最少需要多少人,能让一个人走到终点
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn = 100010; 6 char str[maxn]; 7 8 int solve(int num){//判断派n个人是否能有人能通过 9 int now = 0, change = 0, i; 10 int len = strlen(str); 11 for(i=0; i<len; i++){ 12 change += (str[i]==‘P‘? -1:1);//派一个人前进 13 if(now + change < 0) return 0;//生命值用完,说明n个人不够 14 else if(change > 0){//派一个人前进能获得更多生命值,则让所有人前进 15 now += change*num; 16 change = 0; 17 } 18 } 19 return 1; 20 } 21 22 int main() 23 { 24 int t; 25 scanf("%d",&t); 26 while(t--){ 27 scanf("%s", str); 28 if(str[0]==‘P‘) printf("-1\n"); 29 else{ 30 int left = 0, right = strlen(str)-1, mid; 31 while(right > left){ 32 mid = left+right>>1; 33 if(solve(mid)) right = mid; 34 else left = mid+1; 35 } 36 printf("%d\n",right); 37 } 38 } 39 }
(1)因为初始生命值为0,所以当第一个字符即为P的时候,任务不可能完成
(2)可设left为0,设字符总长度为为right,对人数进行二分,然后判断该人数能否完成任务
(3)每次只派一个人前进,如果那个人前进过程中,能够获得更多的生命值,则让所有人都前进到那里;如果前进过程中生命值耗尽,则不能完成任务
标签:bin style tca 一个人 nat send stream %s poi
原文地址:https://www.cnblogs.com/0424lrn/p/12228140.html