正确的贪心方法:按照比例排序.
code:
#include <bits/stdc++.h> #define N 200000 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; int n; char str[N],S[N]; struct Node { int l,r; double perc; }t[N]; bool cmp(Node a,Node b) { return a.perc>b.perc; } int main() { int i,j,cnt=0; // msetIO("input"); scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%s",str+1); int len=strlen(str+1),s=0; t[i].l=cnt+1; for(j=1;j<=len;++j) ++cnt, S[cnt]=str[j],s+=(str[j]==‘s‘); t[i].r=cnt; t[i].perc=(double)s/len; } ll pre=0,ans=0; sort(t+1,t+1+n,cmp); for(i=1;i<=n;++i) { int l=t[i].l; int r=t[i].r; for(j=l;j<=r;++j) { if(S[j]==‘s‘) { ++pre; } else { ans+=pre; } } } printf("%lld\n",ans); return 0; }
原文地址:https://www.cnblogs.com/guangheli/p/11614789.html
时间: 2024-11-08 16:11:07