AGC015 B-Evilator (Thinking)

Title:

Insert picture description here

solution:

由于保证s[1]='U',s[n]='D',
那么每个(i,j)数对的贡献要么是1要么是2.

对于s[1]和s[n],到达其他位置显然一次只需要操作一次.
对于s[i],如果s[i]='U',那么到达[i+1,n]只需要一次,到达[1,i-1]需要两次.
如果s[i]='D',那么到达[1,i-1]只需要一次,到达[i+1,n]需要两次.

code:

#include <bits/stdc++.h>
#define int long long
#define PI pair<int,int>
using namespace std;
const int maxm=2e5+5;
char s[maxm];
int n;
void solve(){
    
    
    cin>>(s+1);
    n=strlen(s+1);
    int ans=0;
    ans+=(n-1)*2;
    for(int i=2;i<=n-1;i++){
    
    
        if(s[i]=='U'){
    
    
            ans+=n-i;
            ans+=(i-1)*2;
        }else{
    
    
            ans+=(n-i)*2;
            ans+=i-1;
        }
    }
    cout<<ans<<endl;
}
signed main(){
    
    
    ios::sync_with_stdio(0);
    solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/114899993