一道蛮好的贪心题。。
首先显然会想到如果只有s就放最前,只有h就放最后。。
然后当然是希望s尽可能往前放。。所以猜想s是不是多的就有限优先往前放?
然后发现貌似有点问题,如果s和h都一样多要怎么办?
像ssh和hss,一眼觉得是ssh往前放就可以。。然而这2个顺序颠倒也是一样的。。
所以这个结论貌似成立?
所以按照了s个数-h个数这个指标来排序。。
调样例的时候发现s的数量比sssh靠后,显然不对。。
然后又想了下,s和ss其实都一样要放最前,且优先级应该是一样的。。有什么指标能来解决呢。。
然后想到用密度来解决,即s/(s+h)。。这样能保证像ss、s和ssh、hss这样的例子都成立。。
样例过了。。交。。然后发现没开ll...
开ll...AC...然后至于这贪心策略要怎么证明窝也不会。。如果有人会的话欢迎指正_(:зゝ∠)_
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1e9
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 1000005
#define nm 100498
#define pi 3.1415926535897931
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
ll ans,a[NM],b[NM];
int n,cnt,tot,tmp[NM];
string s[NM];
char t[NM];
bool cmp(int x,int y){return (ll)a[x]*(a[y]+b[y])>(ll)a[y]*(a[x]+b[x]);}
int main(){
n=read();
inc(i,1,n)cin>>s[i];
inc(i,1,n)
inc(j,0,s[i].length()-1)
if(s[i][j]=='s')a[i]++;else b[i]++;
inc(i,1,n)tmp[i]=i;
sort(tmp+1,tmp+1+n,cmp);
inc(k,1,n){
int i=tmp[k];
inc(j,0,s[i].length()-1)t[++cnt]=s[i][j];
}
//printf("%s",t+1);
inc(i,1,cnt)if(t[i]=='s')tot++;
else ans+=tot;
printf("%I64d\n",ans);
return 0;
}
Pushok the dog has been chasing Imp for a few hours already.
![](https://i-blog.csdnimg.cn/blog_migrate/15286d3d3176bc0eb4f37ca5b67169c4.png)
Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.
While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and and
.
The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.
Help Imp to find the maximum noise he can achieve by changing the order of the strings.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot's memory.
Next n lines contain the strings t1, t2, ..., tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105.
Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.
4 ssh hs s hhhs
18
2 h s
1
The optimal concatenation in the first sample is ssshhshhhs.