D - RGB Triplets
Time Limit: 2 sec / Memory Limit: 1024 MB
ps:我的vector需要加强,用错了。
Problem Statement
We have a string SS of length N consisting of R, G, and B.
Find the number of triples (i, j, k) (1≤i<j<k≤N) that satisfy both of the following conditions:
- Si≠Sj , Si≠Sk , and Sj≠Sk .
- j−i≠k−j .
Constraints
- 1≤N≤40001≤N≤4000
- SS is a string of length NN consisting of
R,G, andB.
Input
Input is given from Standard Input in the following format:
N S
Output
Print the number of triplets in question.
Sample Input 1 Copy
Copy
4 RRGB
Sample Output 1 Copy
Copy
1
Only the triplet (1, 3, 4)(1, 3, 4) satisfies both conditions. The triplet (2, 3, 4)(2, 3, 4) satisfies the first condition but not the second, so it does not count.
Sample Input 2 Copy
Copy
39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB
Sample Output 2 Copy
Copy
1800
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
const int N=2e5;
int a[4005],b[4005],c[4005];//若改为vector<int>a,b,c错误,vector不能达成a[i]用法
int main(){
int n,l=0,m=0,r=0;
ll ans=0;
string s;
cin>>n>>s;
rep(i,n){
if(s[i]=='R')
a[l++]=i+1;
if(s[i]=='G')
b[m++]=i+1;
if(s[i]=='B')
c[r++]=i+1;
}
for(int i=0;i<l;i++)
for(int j=0;j<m;j++){
int sum=r;
int a1=min(a[i],b[j]);
int b1=max(a[i],b[j]);
if(2*a1-b1-1>=0&&2*a1-b1-1<n&&s[2*a1-b1-1]=='B')
sum--;
if(2*b1-a1-1>=0&&2*b1-a1-1<n&&s[2*b1-a1-1]=='B')
sum--;
if((a1+b1)%2==0&&s[(a1+b1)/2-1]=='B')
sum--;
ans+=sum;
//cout<<ans<<endl;
}
cout<<ans<<endl;
return 0;
}
浙公网安备 33010602011771号