Codeforces Round #461 (Div. 2) D 922D Robot Vacuum Cleaner

给出一些子串,问你如何组合这些子串,可以使得总串中的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.大佬们将究看下吧,证出来了可以教我…
看了下官方题解,这个传递性,我是知道的,但是为什么传递性就等于正确呢…?
好菜啊不懂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值