题目链接
https://www.acwing.com/problem/content/1989/
思路通过map进行差分处理,每次处理到前缀和从大于1变到小于等于1的时候就进行一次区间长度统计,每次从前缀和小于等于1变到大于1的时候就更新左区间的位置
代码#include
using namespace std;
#define ll long long
#define mod 1000000009
#define endl "\n"
#define PII pair
ll ksm(ll a,ll b) {
ll ans = 1;
for(;b;b>>=1LL) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll lowbit(ll x){return -x & x;}
const int N = 2e6+10;
map vis;
int main()
{
int n;
cin>>n;
ll step;
char op;
ll loc = 0;
for(int i = 1;i >step>>op;
ll r;
if(op == 'L'){
r = loc - step;
vis[r]++;
vis[loc]--;
}
else{
r = loc + step;
vis[loc]++;
vis[r]--;
}
loc = r;
}
ll sum = 0;//前缀和
ll ans = 0;//答案
ll l = 0;//左区间的位置
for(auto [x,y] : vis) {
if(sum 1)
l = x;//如果当前的前缀和小于等于1并且加上当前的这个值后大于1,我们就吧左区间往右移
else if(sum > 1 && sum + y
关注
打赏