qwq

ABC268F 题解

考虑贪心。

设字符串 \(S\) 里数字之和为 \(S_d\)X 的个数为 \(S_c\)

考虑相邻的两个字符串 \(A,B\) 的贡献:

考虑临项交换,这只影响到相邻两个串的相互贡献。

注意到交换 \(A,B\) 只会影响到 \(B_dA_c,A_dB_c\),那么产生的贡献 \(\Delta=B_dA_c-A_dB_c\)

因为对于最优解,\(\Delta<0\),所以按照 \(B_dA_c>A_dB_c\) 排序即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll calc(string s)
{
    int cnt = 0;
    ll ans = 0;
    for(char c : s)
    {
        if(c == 'X') cnt ++;
        else ans += cnt * (c - '0');
    }
    return ans;
}

const int N = 2e5 + 5;
struct node {string s; ll c, d;} a[N];
int n;

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i].s;
        for(char c : a[i].s)
            a[i].c += c == 'X', a[i].d += (c != 'X') * (c - '0');
    }
    sort(a + 1, a + n + 1, [](auto &x, auto &y) {return x.c * y.d > x.d * y.c;});
    string anss;
    for(int i = 1; i <= n; i ++)
        anss += a[i].s;
    cout << calc(anss);

    return 0;
}

作者:adam01

出处:https://www.cnblogs.com/adam01/p/18340201

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   adam01  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题