E - Rikka with Mutex

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 Input

4
VPP
VPPVVVVPPPPPPPP
VPPPPPPPPPPPPPP
P

Sample Output

2
3
14
-1

题目大意:给定一串字符串由V和P组成,一个人的初始生命值为0,遇V+1,遇P-1,同伴中可以分享生命值,在保证生命值不为负的情况下,求最少需要多少人,能让一个人走到终点

 #include<iostream>
#include<cstring>
using namespace std; const int maxn = ;
char str[maxn]; int solve(int num){//判断派n个人是否能有人能通过
int now = , change = , i;
int len = strlen(str);
for(i=; i<len; i++){
change += (str[i]=='P'? -:);//派一个人前进
if(now + change < ) return ;//生命值用完,说明n个人不够
else if(change > ){//派一个人前进能获得更多生命值,则让所有人前进
now += change*num;
change = ;
}
}
return ;
} int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%s", str);
if(str[]=='P') printf("-1\n");
else{
int left = , right = strlen(str)-, mid;
while(right > left){
mid = left+right>>;
if(solve(mid)) right = mid;
else left = mid+;
}
printf("%d\n",right);
}
}
}

 (1)因为初始生命值为0,所以当第一个字符即为P的时候,任务不可能完成
(2)可设left为0,设字符总长度为为right,对人数进行二分,然后判断该人数能否完成任务
(3)每次只派一个人前进,如果那个人前进过程中,能够获得更多的生命值,则让所有人都前进到那里;如果前进过程中生命值耗尽,则不能完成任务

二分-E - Rikka with Mutex的更多相关文章

  1. 2019 HZNU Winter Training Day 14 Comprehensive Training

    A - Choosing Capital for Treeland CodeForces - 219D 题意:有一颗单向边的树,要选取一个结点作为首都.要求是这个结点到其它结点,总共需要翻转的路径数量 ...

  2. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  3. java开发中的Mutex vs Semaphore

    先看一下stackoverflow上是怎么说的吧 原文地址:http://stackoverflow.com/questions/771347/what-is-mutex-and-semaphore- ...

  4. HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5

    JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...

  5. C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

    看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...

  6. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  7. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  8. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  9. [bzoj2653][middle] (二分 + 主席树)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

随机推荐

  1. B样条曲线方程和C++实现

    功能:根据参数u值和k(大小为阶数值)与节点矢量,计算第i个k次B样条基数 输入参数: u—参数值:k—大小值为阶数:i—第i个k次B样条的支撑区间左端节点的下标:aNode为节点向量. 输出参数:返 ...

  2. Educational Codeforces Round 32 E 二分

    题意:从数组中选几个(任意),使他们的和modm的值最大 题解:我一开始是直接暴力写,然后就会t 其实这题可以用二分的方法写,一半数组的值用来遍历,一般数组的值用来查询. 二分查询就能把时间继续缩短 ...

  3. Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…

    php打印小票错误提示:Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activate ...

  4. 我眼中的ASP.NET.MVC

    MVC MVC全名 : Model View Controller ( Model-模型 View-视图  Controller-控制器)是一种经典的,经久不衰的,屹立不倒的软件设计框架.实现了业务逻 ...

  5. Python 类 初学者笔记

    面对象编程:编写表现世界中的事物和景象的类,并基于这些类创建对象,被创建的对象称为实例化. 创建类 class Dog(): #Python中类名称中的首字母要大写 def __init__(self ...

  6. C语言简单编译预处理-笔记

    预处理指令 所谓预处理,是指,在编译之前对程序源码进行初步的转换,产生新的源代码提供给编译器.预处理时主要完成:包含其他源文件,定义宏,根据条件决定编译时是否包含某些代码(>防止重复包含某些文件 ...

  7. XGBoost学习笔记1

    XGBoost XGBoost这个网红大杀器,似乎很好用,完事儿还是自己推导一遍吧,datacamp上面有辅助的课程,但是不太涉及原理 它究竟有多好用呢?我还没用过,先搞清楚原理,hahaha~ 参考 ...

  8. UVA1635-唯一分解定理的基本应用2

    原题:https://vjudge.net/problem/UVA-1635 这是一个极其典型的“从素因子角度出发”的题目,下面是我的代码: #include<iostream> #inc ...

  9. node--处理一个增加上传操作和渲染页数条

    一段学习的结束 我也不知道我这是在写些什么,只是觉得学完了一些东西,就是想把它记录 一下,这样我就可以知道我是学过这一块的,要多激励自己,^^O(∩∩)O哈哈~ 以下内容并不完全与标题匹配,不过以下内 ...

  10. Linux环境搭建及基础操作

    一.Linux环境搭建 1.安装虚拟机软件(VMWare,Parallel) 虚拟机的作用:将本来不是适合当前操作系统的分区虚拟化成适合当前操作系统的分区格式 2.新建虚拟机: 类似买了一台新的电脑, ...