题目链接:
题意:
思路:
代码:
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int N = 1e5 + 10;
typedef pair<int, int> PII;
PII a[N];
int s[N];
unordered_map<int, int> pos;
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
int x;
char b;
scanf("%d %c", &x, &b);
a[i].x = x;
if(b == 'G') a[i].y = 1;
else a[i].y = -1;
}
sort(a + 1, a + n + 1);
pos[0] = a[1].x;
int ans = 0, last = 0;
s[0] = a[1].y;
for(int i = 1; i <= n; i++)
{
s[i] = a[i].y;
if(s[i] != s[i - 1])
{
last = a[i].x;
}
ans = max(ans, a[i].x - last);
a[i].y += a[i - 1].y;
int x = a[i].y, d = a[i].x;
if(pos.count(x)) ans = max(ans, d - pos[x]);
else if(i < n) pos[x] = a[i + 1].x;
}
printf("%d\n", ans);
return 0;
}