Purple rain falls in the magic kingdom of Linearland which is a straight, thin peninsula.
On close observation however, Professor Nelson Rogers finds that the purple rain is actually a mix of red and blue raindrops.
In his zeal, he records the location and color of the raindrops in different locations along the peninsula. Looking at the data, Professor Rogers wants to know which part of Linearland had the “least” purple rain.
After some thought, he decides to model this problem as follows. Divide the peninsula into nn sections and number them west to east from 11to nn. Then, describe the raindrops as a sequence of RR and BB, depending on whether the rainfall in each section is primarily red or blue. Finally, find a subsequence of contiguous sections where the difference between the number of RR and the number of BB is maximized.
1 Input
The input consists of a single line containing a string of nn characters (1 ≤ n ≤ 10^{5} )(1≤n≤105), describing the color of the raindrops in sections 11 to nn.
It is guaranteed that the string consists of uppercase ASCIIASCII letters ‘RR’ and ‘BB’ only.
2 Output
Print, on a single line, two space-separated integers that describe the starting and ending positions of the part of Linearland that had the least purple rain. These two numbers should describe an inclusive range; both numbers you print describe sections included in the range.
If there are multiple possible answers, print the one that has the westernmost starting section. If there are multiple answers with the same westernmost starting section, print the one with the westernmost ending section.
样例输入1复制
BBRRBRRBRB
样例输出1复制
3 7
样例输入2复制
BBRBBRRB
样例输出2复制
1 5
题目大意
紫色的雨落在神奇的线性大陆王国,该大陆是一个直而细的半岛。
但是仔细观察,纳尔逊·罗杰斯教授发现紫色的雨实际上是红色和蓝色雨滴的混合。
他热情地记录了沿半岛不同位置的雨滴的位置和颜色。通过查看数据, 教授想知道 的
哪部分有“最少”的紫色雨。
经过一番思考,他决定按照以下方式对该问题进行建模。将半岛分为 个部分,并从 到 从西向东编号。然
后,根据每个部分的降雨主要是红色还是蓝色,将雨滴描述为 和 序列。最后,找到一个连续部分的子序
列,其中 的数量和 的数量之间的差异最大。
输入
输入由包含 个字符的字符串 ( ) 的一行组成,描述了第 至 节中雨滴的颜色。
确保该字符串仅由大写 字母“ ”和“ ”组成。
输出
在一行上打印两个以空格分隔的整数,这些整数描述 紫雨最少的部分的开始和结束位置。
这两个数字应描述一个范围。您打印的两个数字均描述了该范围内的各个部分。
如果有多个可能的答案,请打印具有最西端开始部分的答案。如果有相同的最西端开始部分的多个答案,则
打印最西端结束部分的答案。
题目解析
假如把半岛看成数轴,把 看成 , 看出 ,那么就能画出一条折线,因为你找一个区间开始点,肯定
是里这个点越高或者越低,差距越大,所以最高点和最低点的差就是红蓝差距最大的地方,统计最高点和最
低点的出现位置即可。
(就是找最大点和最小点,排序,完事)
# include <iostream>
# include <algorithm>
# include <cstring>
# include <math.h>
# include <stdio.h>
# include <vector>
# include <map>
using namespace std;
# define ll long long
# define ull unsigned long long
char a[100100];
int pre[100100];
int main(){
scanf("%s",a+1);
int len = strlen(a+1);
memset(pre,0,sizeof(pre));
for(int i = 1; i <= len; i++){
if(a[i] == 'B'){
pre[i]++;
}
else{
pre[i]--;
}
}
int l = 0,r = 0;
int max1 = -100100;
int min1 = 100100;
pre[0] = 0;
max1 = max(pre[0],max1);
min1 = min(pre[0],min1);
for(int i = 1; i <= len; i++){
pre[i] = pre[i-1]+pre[i];
if(max1 < pre[i]){
max1 = pre[i];
r = i;
}
if(min1 > pre[i]){
min1 = pre[i];
l = i;
}
//cout << max1 << " " << min1 << endl;
}
int l1 = min(l,r);
int r1 = max(l,r);
printf("%d %d\n",l1+1,r1);
return 0;
}