# 寒武纪重现（HDU）- 1008(6261) - Rikka with Mutex(二分 + 错误总结） - CSDN博客

Rikka with Mutex
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 32 Accepted Submission(s): 12

Problem Description
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 n 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 E. Initially, E=0.

If one person walks through a white gate, he will gain one point of energy, i.e., E will be added by 1. And if one person walks through a black gate, he will lose one point of energy, i.e., E will be subtracted by 1. Since E must be a non-negative integer, if E=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.

Input
The first line contains a single numner t(1≤t≤103), the number of the testcases.

For each testcase, the first line contains a PV string s(1≤|s|≤105) describing the gates.

The input guarantees that there are at most 30 testcases with |S|>1000.

Output
For each testcase, output a single integer, the answer. And if it is impossible, output ?1.

Sample Input
4
VPP
VPPVVVVPPPPPPPP
VPPPPPPPPPPPPPP
P

Sample Output
2
3
14
-1

Source

Recommend
liuyiding | We have carefully selected several similar problems for you: 6263 6262 6261 6260 6259

：至少让一个人走完这个字符串，最少需要多少人？

``````#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 1e5 + 5;
char s[maxn];
long long a[maxn];
int len,all;

void Init(){
bool flag;
if(s == 'V') flag = 1;
else flag = 0;
long long sum = 0LL;all = 0;
for(int i = 1;i <= len;i++){
if(s[i] == 'V' && flag) sum++;
else if(s[i] == 'V' && !flag) a[++all] = -sum,sum = 1LL,flag = 1;
else if(s[i] == 'P' && !flag) sum++;
else if(s[i] == 'P' && flag) a[++all] = sum,sum = 1LL,flag = 0;
}
if(flag) a[++all] = sum;
else a[++all] = -sum;
}

bool check(long long mid){
long long E,sum;
E = 1LL * a * mid;
sum = E;
for(int i = 2;i <= all + 1;i++){
if(E < 0) return false;
if(i == all + 1) return true;
E += a[i];
if(E >= sum){
E += (mid - 1LL) * (E - sum) * 1LL;
sum = E;
}
}
}

int main(){
int t;
cin >> t;
while(t--){
cin >> s + 1;
len = strlen(s + 1);
Init();
int l = 1,r = len,mid;
while(l <= r){
mid = (l + r) >> 1;
if(check(mid)) r = mid - 1;
else l = mid + 1;
}
if(l >= len) printf("-1\n");
else printf("%d\n",l);
}
}``````

(注：此网站文章大都来自公开的博客，如有原作者发现有侵权问题，请在此留言，我们会立刻删除相关文章。)