【贪心】【UVA10905】 Children's Game
Description
给定n个正整数,求他们相连接后能形成的最大整数。例如:12,23这两个数能连接的最大数是2312,。
Input
多组数据,每组数据中:
- 第一行为一个整数n
- 第二行有n个整数,代表给出的数。
输入结束的标志为n=0。
Output
对于每组数据,输出:
- 能拼成的最大整数
Sample Input
Sample Output
Hint
1≤n≤50
Solution
简单的贪心。对给出的数进行排序,A在B的前面当且仅当A接上B比B接上A大。最后按顺序输出答案即可。
值得引起注意的是对贪心的证明,对于这类贪心题的证明方式常常为:考虑两个相邻的数,这样这两个数无论如何交换都对其他数没有影响,所以需要取这两个数间的最优答案,而所有不相邻的情况都可以通过相邻的情况数学归纳得到。这样就证明了贪心的正确性。需要同样证明的题目还有Uva11729 Commando War,以及NOIP2012 国王游戏。
小技巧上,std::string型已经重载了'+’运算符为两个字符串相接,重载了'<’运算符为两个字符串字典序的大小
Code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rg register
#define ci const int inline void qr(int &x) {
char ch=getchar(),lst=' ';
while(ch>''||ch<'') lst=ch,ch=getchar();
while(ch>=''&&ch<='') x=(x<<)+(x<<)+(ch^),ch=getchar();
if (lst=='-') x=-x;
} char buf[];
inline void write(int x,const char aft,const bool pt) {
if(x<) {putchar('-');x=-x;}
int top=;
do {
buf[++top]=x%+'';
x/=;
} while(x);
while(top) putchar(buf[top--]);
if(pt) putchar(aft);
} template <typename T>
inline T mmax(const T &a,const T &b) {if(a>b) return a;return b;}
template <typename T>
inline T mmin(const T &a,const T &b) {if(a<b) return a;return b;}
template <typename T>
inline T mabs(const T &a) {if(a<) return -a;return a;} template <typename T>
inline void mswap(T &a,T &b) {T temp=a;a=b;b=temp;} const int maxn = ; int n; std::string MU[maxn];
inline bool cmp(const std::string &_a,const std::string &_b) {
return _a+_b>_b+_a;
} int main() {
qr(n);
while(n) {
for(int i=;i<=n;++i) std::cin>>MU[i];
std::sort(MU+,MU++n,cmp);
for(int i=;i<=n;++i) std::cout<<MU[i];
putchar('\n');
n=;qr(n);
}
return ;
}
Summary
对于这类贪心题目的证明方式需要格外注意和掌握。
虽然你不想用stl,但是如果你不想手写拼接(其实也不难写)的话还是考虑string的使用
【贪心】【UVA10905】 Children's Game的更多相关文章
- UVA10905 Children's Game
题意:给定n个正整数,把它们连接成一个最大的整数.比如,123,124,556,90有24种连接方法,最大的结果为9 056 124 123. 贪心.一开始就想用string水过.注意不能直接用str ...
- UVA10905: Children's Game(排序)
题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/A 题目需求:,给n个数字,将它们重新排序得到一个最大的数 ...
- 【一坨理论AC的题】Orz sxy大佬
1.UVA10891 Game of Sum 2.LA4254 Processor . 3.UVA10905 Children's Game 4.UVA11389 The Bus Driver Pro ...
- UVA 10905 Children's Game 孩子的游戏 贪心
题意:给出N个数,要求把它们拼凑起来,让得到的数值是最大的. 只要分别比较两个数放前与放后的值的大小,排序后输出就可以了. 比如123和56,就比较12356和56123的大小就行了. 写一个比较函数 ...
- UVA 10905 Children's Game (贪心)
Children's Game Problem Description There are lots of number games for children. These games are pre ...
- HDU 4001 To Miss Our Children Time(2011年大连网络赛 A 贪心+dp)
开始还觉得是贪心呢... 给你三类积木叫你叠楼房,给你的每个积木包括四个值:长 宽(可以互换) 高 类型d d=0:你只能把它放在地上或者放在 长 宽 小于等于 自己的积木上面 d=1:你只能把它放在 ...
- UVA 10905 Children's Game (贪心)
贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止. #include<bits/stdc++.h> using namespace std; ; stri ...
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- candy(贪心)
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- MySQL☞abs函数
abs( )函数:求出绝对值 格式: select abs(数值) from 表名 如下图:
- 腾讯云API弹性公网IP踩坑
由于自己管理的云服务器数量比较多,时不时需要更换IP,在管理台上一下下点击,实在浪费时间,于是就想到了通过API调用的方式,将更换IP一系列动作,全部集成到Python代码里面,实现一行命令,完成IP ...
- hexo部署失败如何解决
- [leetcode-783-Minimum Distance Between BST Nodes]
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- [leetcode-666-Path Sum IV]
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【转】jQuery最佳实践
上周,我整理了<jQuery设计思想>. 那篇文章是一篇入门教程,从设计思想的角度,讲解"怎么使用jQuery".今天的文章则是更进一步,讲解"如何用好jQu ...
- canvas学习(一):线条,图像变换和状态保存
canvas学习(一):线条,图像变换和状态保存 一:绘制一条线段: var canvas = document.getElementById('canvas') var ctx = canvas.g ...
- wf效能分析
听从了老师的建议我请教了其他的同学,修改了代码实现了功能四,以下是我的效能测试: 1.采用ptime.exe测试的3次截图 可以看到的是三次执行时间分别为:1.449秒:0.915秒:0.871秒,取 ...
- 2019寒假训练营寒假作业(三) 对Sketch——CM-Sketch的理解(理论题部分)
目录 实验题部分 基本题 1.简述sketch: 2.Count-min Sketch: 开放题部分 理论部分 1.解释为什么 sketch 可以省空间 2.用流程图描述Count-min sketc ...
- 【Linux】- CentOS 7 安装.NET Core 2.1
添加dotnet产品Feed 在安装.NET Core之前,您需要注册Microsoft产品Feed. 这只需要做一次. 首先,注册Microsoft签名密钥,然后添加Microsoft产品Feed. ...