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;
}
posted @ 2024-08-03 11:08  adam01  阅读(33)  评论(0)    收藏  举报