[ABC268F] Best Concatenation 题解
[ABC268F] Best Concatenation Solution
更好的阅读体验戳此进入
题面
给定 X
和数字构成的字符串,你需要对其进行排列并拼接成新的字符串 X
的数量,并求和。输出分数最大值。
Solution
首先一个显然的结论即为对于题目定义的分数,同一字符串内部的 X
对其数字的贡献,与字符串在排列中的顺序无关。
于是我们考虑其它字符串的 X
对字符串数字的贡献,我们考虑字符串 X
的数目,令
则不难想到若我们要将
所以我们直接考虑对字符串进行排序,比较规则则按照刚才的式子跑一下即可。
同时我们也可以从意义上感性理解,显然只有前面的 X
对后面的数字产生贡献,所以我们将 X
更多数字更少的放在前面,则
同时对于此贪心的证明,考虑若满足偏序关系
存在双倍经验 LG-P1080 [NOIP2012 提高组] 国王游戏。
Code
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW void* Edge::operator new(size_t){static Edge* P = ed; return P++;}
using namespace std;
mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}
typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;
template < typename T = int >
inline T read(void);
int N;
struct Node{string S; int sum, cnt;}d[210000];
ll ans(0);
int main(){
N = read();
for(int i = 1; i <= N; ++i){
cin >> d[i].S;
for(auto c : d[i].S)
if(c == 'X')++d[i].cnt;
else d[i].sum += c - '0';
}sort(d + 1, d + N + 1, [](const Node &a, const Node &b)->bool{return (ll)a.sum * b.cnt < (ll)b.sum * a.cnt;});
ll cur(0);
for(int i = 1; i <= N; ++i)
for(auto c : d[i].S)
if(c == 'X')++cur;
else ans += cur * (c - '0');
printf("%lld\n", ans);
fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
template < typename T >
inline T read(void){
T ret(0);
int flag(1);
char c = getchar();
while(c != '-' && !isdigit(c))c = getchar();
if(c == '-')flag = -1, c = getchar();
while(isdigit(c)){
ret *= 10;
ret += int(c - '0');
c = getchar();
}
ret *= flag;
return ret;
}
UPD
update-2023_01_18 初稿
本文作者:tsawke
本文链接:https://www.cnblogs.com/tsawke/p/17124346.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
ABC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步