[CF]922D. Robot Vacuum Cleaner-结论&排序

版权声明:侵删,转载请附带链接或评论 https://blog.csdn.net/corsica6/article/details/81748486

传送门


题解

本来比的是 s i / h i ,但是会爆精度,所以就比较 s i h j , s j h i ,这样会爆 i n t ,一定要加 l o n g l o n g


代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int n,len;
ll sum;char s[N];
struct P{ll a,b;}t[N];
bool cmp(const P&x,const P&y)
{return x.a*y.b>x.b*y.a;} 

int main(){
    int i,j;ll k;
    scanf("%d",&n);
    for(i=1;i<=n;++i){
        k=0;
        scanf("%s",s);
        len=strlen(s);
        for(j=0;j<len;++j){
            if(s[j]=='s') k++;
            else{sum+=k;}
        }
        t[i].a=k;t[i].b=len-k;
    }
    sort(t+1,t+n+1,cmp);
    k=t[n].b;
    for(i=n-1;i>=1;--i){
        sum+=1ll*t[i].a*k;
        k+=t[i].b;
    }
    printf("%I64d\n",sum);
}

猜你喜欢

转载自blog.csdn.net/corsica6/article/details/81748486