NWERC 2011 E - Please, go first(模拟,思维)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37360631/article/details/81913956

这里写图片描述
这里写图片描述
模拟过的,可能写烦了,看别人代码十行左右。。

//ICPC-E Please,go first
#include<iostream>
#include<map>
#include<cstring>
#define maxn 30000
typedef long long ll;
using namespace std;
int T,n,ha[maxn],h[maxn],A[maxn];
char ss[maxn],ch;
map<char,int> mp[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    cin>>T;
    while(T--){
        memset(ha,0,sizeof(ha));
        memset(h,0,sizeof(h));
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>ch;
            //范围0-9,10-35,40-75
            if(ch>='A'&&ch<='Z') {
                A[i]=ch-'A'+10;
                ha[A[i]]++;
            }
            else if(ch>='a'&&ch<='z'){
                A[i]=ch-'a'+40;
                ha[A[i]]++;
            }
            else if(ch>='0'&&ch<='9'){
                A[i]=ch-'0';
                ha[A[i]]++;
            }
        }
        ll ans1=0;
        for(int i=0;i<100;i++){
            if(ha[i]>1) {ans1+=(ha[i]-1)*ha[i]/2;}
        }
        //cout<<ans1<<endl;

        ll ans2=0,tmp=0;
        for(int i=0;i<n;i++){
            h[A[i]]++;
            if(h[A[i]]<ha[A[i]])  {tmp++; ans2+=tmp;}
            else if(ha[A[i]]==1) {ans2+=tmp;}
            else if( h[A[i]]==ha[A[i]]) {tmp++;tmp-=ha[A[i]];ans2+=tmp;}
           // cout<<ans2<<endl;
        }
        ll ans=(ans2-ans1)*5;
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37360631/article/details/81913956