给出一些子串,问你如何组合这些子串,可以使得总串中的s-h对最多.
4
ssh
hs
s
hhhs
=>ssshhshhhs = 18
想法是,如果两者交换,可以使得总和增加,那么就交换.
块内块外的细节比较有意思,但是不难.
#include <iostream>
#include <algorithm>
using namespace std;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
typedef long long LL;
const int MAXN = 1e5+17;
struct dap
{
LL s,h;
bool operator<(const dap & b) const
{
return s*b.h>b.s*h;
}
}all[MAXN];
int main(int argc ,char const *argv[])
{
#ifdef noob
freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
#endif
int n;
cin>>n;
LL ans = 0;
for (int i = 0; i < n; ++i)
{
string str;
cin>>str;
all[i].s=all[i].h=0;
int sn = 0;
for (int j = 0; j < str.length(); ++j)
{
if(str[j]=='h') all[i].h++,ans+=sn;
if(str[j]=='s') all[i].s++,sn++;
}
}
sort(all, all+n);
LL sn=0;
for (int i = 0; i < n; ++i)
{
ans+=(sn*all[i].h);
sn+=all[i].s;
}
cout<<ans<<endl;
return 0;
}
妈耶我写这个的时候才意识到这个贪心排序好像没有证明,试着搞了一下发现搞不出.T^T.大佬们将究看下吧,证出来了可以教我…
看了下官方题解,这个传递性,我是知道的,但是为什么传递性就等于正确呢…?
好菜啊不懂