Codeforces 1315B Homecoming (二分)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are nn crossroads in the line in the town, and there is either the bus or the tram station at each crossroad.
The crossroads are represented as a string ss of length nn , where si=Asi=A , if there is a bus station at ii -th crossroad, and si=Bsi=B , if there is a tram station at ii -th crossroad. Currently Petya is at the first crossroad (which corresponds to s1s1 ) and his goal is to get to the last crossroad (which corresponds to snsn ).
If for two crossroads ii and jj for all crossroads i,i+1,…,j−1i,i+1,…,j−1 there is a bus station, one can pay aa roubles for the bus ticket, and go from ii -th crossroad to the jj -th crossroad by the bus (it is not necessary to have a bus station at the jj -th crossroad). Formally, paying aa roubles Petya can go from ii to jj if st=Ast=A for all i≤t<ji≤t<j .
If for two crossroads ii and jj for all crossroads i,i+1,…,j−1i,i+1,…,j−1 there is a tram station, one can pay bb roubles for the tram ticket, and go from ii -th crossroad to the jj -th crossroad by the tram (it is not necessary to have a tram station at the jj -th crossroad). Formally, paying bb roubles Petya can go from ii to jj if st=Bst=B for all i≤t<ji≤t<j .
For example, if ss ="AABBBAB", a=4a=4 and b=3b=3 then Petya needs:

- buy one bus ticket to get from 11 to 33 ,
- buy one tram ticket to get from 33 to 66 ,
- buy one bus ticket to get from 66 to 77 .
Thus, in total he needs to spend 4+3+4=114+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character snsn ) does not affect the final expense.
Now Petya is at the first crossroad, and he wants to get to the nn -th crossroad. After the party he has left with pp roubles. He's decided to go to some station on foot, and then go to home using only public transport.
Help him to choose the closest crossroad ii to go on foot the first, so he has enough money to get from the ii -th crossroad to the nn -th, using only tram and bus tickets.
Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104 ).
The first line of each test case consists of three integers a,b,pa,b,p (1≤a,b,p≤1051≤a,b,p≤105 ) — the cost of bus ticket, the cost of tram ticket and the amount of money Petya has.
The second line of each test case consists of one string ss , where si=Asi=A , if there is a bus station at ii -th crossroad, and si=Bsi=B , if there is a tram station at ii -th crossroad (2≤|s|≤1052≤|s|≤105 ).
It is guaranteed, that the sum of the length of strings ss by all test cases in one test doesn't exceed 105105 .
For each test case print one number — the minimal index ii of a crossroad Petya should go on foot. The rest of the path (i.e. from ii to nn he should use public transport).
5
2 2 1
BB
1 1 1
AB
3 2 8
AABBBBAABB
5 3 4
BBBBB
2 1 1
ABABAB
2
1
3
1
6
大意就是这个人有p元,公交车在乘车区间a元,有轨电车b元,要到达n的话钱不一定够,问先步行再坐交通工具的话最少需要走几站。从后往前考虑比较方便。根据题意,其实s[n]是什么不用管,因为要到达的就是s[n],所以分别求出s[n]到s[i](0<=i<=n-1,这里是字符串下标)所需要花的钱。容易看出花费构成的序列是单调的因此直接从里面找第一个小于等于p的数对应的位置输出即可,没有的话直接输出最后一个位置。理论上这里可以直接二分lower_bound,不知道为什么这么写会挂在第56个点...最后O(n)竟然也能过...还请大佬指教。
#include <bits/stdc++.h>
using namespace std;
long long a,b,p;
char s[];
unsigned long long n[];
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%lld%lld%lld",&a,&b,&p);
long long i,pre=;
scanf("%s",s);
for(i=strlen(s)-;i>=;i--)//不用管最后一个是啥
{
long long temp=;
if(s[i]=='A')temp=a;
else if(s[i]=='B')temp=b;
if(i==strlen(s)-)
{
pre=temp;
n[i]=temp;
continue;
}
if(s[i]==s[i+])n[i]=n[i+];
else
{
pre=n[i+];
n[i]=pre+temp;
}
}
// long long pos=lower_bound(n,n+strlen(s)-1,p,greater<int>())-n;//找到递减序列里第一个小于等于p的数 //为啥二分过不了???????
// if(pos==strlen(s))
// {
// cout<<strlen(s)<<endl;
// continue;
// }
// cout<<pos+1<<endl;
int pos=;
bool flag=;
for(i=strlen(s)-;i>=;i--)
{
if(i==)
{
if(n[]<=p)
{
pos=;
flag=;
}
}
if(n[i]<=p&&n[i-]>p)
{
pos=i+;
flag=;
break;
}
}
if(!flag)pos=strlen(s);
cout<<pos<<endl;
}
return ;
}
Codeforces 1315B Homecoming (二分)的更多相关文章
- codeforces 1165F1/F2 二分好题
Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- CodeForces 163B Lemmings 二分
Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...
- CodeForces - 589A(二分+贪心)
题目链接:http://codeforces.com/problemset/problem/589/F 题目大意:一位美食家进入宴会厅,厨师为客人提供了n道菜.美食家知道时间表:每个菜肴都将供应. 对 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- CodeForces - 1059D(二分+误差)
链接:CodeForces - 1059D 题意:给出笛卡尔坐标系上 n 个点,求与 x 轴相切且覆盖了所有给出点的圆的最小半径. 题解:二分半径即可.判断:假设当前二分到的半径是 R ,因为要和 x ...
- Letters CodeForces - 978C (二分)
Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...
- Codeforces 475D 题解(二分查找+ST表)
题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...
随机推荐
- eclipse 项目的创建 编写 'Hello World'
写项目之前确保 eclipse 安装完成 以及JDK 环境配置 成功 开始: 打开eclipse 右键file ->new->java project 如图: 然后输入项目名 点击Fin ...
- Wannafly Camp 2020 Day 2E 阔力梯的树 - set,启发式合并
搞一波启发式合并即可 #include <bits/stdc++.h> using namespace std; #define int long long #define iter se ...
- (转)classload和class.forname()区别
转自:http://carl-java.iteye.com/blog/978680 java中class.forName和classLoader都可用来对类进行加载.前者除了将类的.class文件加载 ...
- gogs迁移至gitlab
一.先来聊一聊迁移的思路,不管是gogs迁移至gitlab,还是gitlab迁移至gogs,亦或gitlab迁移至gitlab,手动迁移的步骤都是如下图所示(在gitlab上新建一个空项目就会有这样的 ...
- Eclipse项目工程导入到IDEA继续开发-超详细
现在Java开发的主流工具是IDEA,不是说Eclipse,各有各的特色.不过我现在深深的爱上了idea这个工具. 但是之前很多项目都是用eclipse开发的,现在就转入到idea中进行继续开发. 1 ...
- Python之tcp server模拟Http通信
1.python tcp server代码: import socket def main(): tcp_server_socket = socket.socket(socket.AF_INET, s ...
- Wannafly Camp 2020 Day 2C 纳新一百的石子游戏
为什么为了这么个简单题发博客呢? 因为我又因为位运算运算符优先级的问题血了 #include <bits/stdc++.h> using namespace std; #define in ...
- 使用tableExport.js直接导出web页面上的table
1,需要导入两个js文件,一个tableExport.js,另一个是jquery.base64.js,前一个文件是导出数据和核心类库,后一个是为了避免导出中文时乱码的js文件,如果你导出的数据没有中文 ...
- PHP+jPaginate插件制作无刷新分页实例
jPaginate是一款动感滚动分页插件,它的表现形式是像分页的按钮一样,有意思的是这些按钮却可以左右滚动,可以通过单击或 鼠标滑向点两侧的小箭头来控制按钮的左右滚动. 读取第一页数据: <di ...
- wap网站开发小记(转载)
一:网页的一些标记 1:声明 <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http ...