A1038 Recover the Smallest Number (30)
admin
2024-05-08 23:28:00

Powered by:NEFU AB-IN

Link

文章目录

  • A1038 Recover the Smallest Number (30)
    • 题意
    • 思路
    • 代码

A1038 Recover the Smallest Number (30)

  • 题意

    Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.
    给定一个数组,数组中包含若干个整数,数组中整数可能包含前导 0。你需要将数组中的所有数字拼接起来排成一个数,并使得该数字尽可能小。

  • 思路

    判断数字a, b拼凑成的数字ab还有哪个大,直接用拼接之后的字符串比较大小就可以
    即如果字符串比较ab注意是小于号,没有等于!!别管为啥了,cpm函数都这样

    所以,如果也排成最大的数也是同理

    另原理,一个集合如果能够排序,那么它一定是全序集,全序集需要满足三个性质:反对称性、传递性、完整性
    本题可证明用 return a + b < b + a; 的方法,来达到题目所需的要求,并且能够证明以上三条性质

  • 代码

    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-01-09 12:57:42
    * @FilePath: \GPLT\A1038\A1038.cpp
    * @LastEditTime: 2023-01-09 13:20:26
    */
    #include 
    using namespace std;
    #define int long long
    #undef int#define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS                                                                                                            \ios::sync_with_stdio(false);                                                                                       \cin.tie(nullptr);                                                                                                  \cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'
    typedef pair PII;const int N = 1e5 + 10, INF = 0x3f3f3f3f;signed main()
    {IOS;int n;cin >> n;vector s(n);for (int i = 0; i < n; ++i)cin >> s[i];sort(ALL(s), [&](const string &a, const string &b) { return a + b < b + a; });string res;for (int i = 0; i < SZ(s); ++i)res += s[i];int i = 0;while (i < SZ(res) - 1 && res[i] == '0') // 去掉前导0的同时,一定要保留一位,也就是如果最后剩下0,也要保留++i;cout << res.substr(i);return 0;
    }
    

相关内容

热门资讯

穿越天山遇见四季童话:新疆深度... 穿越天山遇见四季童话:新疆深度旅行指南与秘境故事 序章:一封来自西域的情书 每年六月,当伊犁河谷的薰...
从戈壁到花海:一位新疆本地向导... 从戈壁到花海:一位新疆本地向导的四季旅行手记 在新疆生活了近二十年,我依然记得第一次被这片土地震撼的...
穿越军 - 穿越中国亲子挑战赛... 以前总带孩子去各地旅游,打卡景点、吃美食,回来之后孩子除了照片,什么都没留下,直到带他参加穿越军亲子...
2026最新山东旅行社综合实力... 随着 2026 年旅游市场持续升温,山东凭借 "一山一水一圣人" 的独特魅力和丰富的文旅资源,成为国...
二十年,再赴大熊山 人间忽晚,山河已秋。一别大熊山,已是整整二十载。 那时我匆匆登临,步履轻快,眼里只有山川壮阔、林木葱...