【贪心】【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 ...
随机推荐
- word record 3
word record 3 tabloid : a half size page of a newspaper, or a newspaper or magazine with short, exci ...
- 【text】 文本组件说明
text文本组件:在小程序里除了文本节点以外的其他节点都无法长按选中. 原型: <text selectable="[Boolean]" space="[ensp ...
- LeetCode 240——搜索二维矩阵 II
1. 题目 2. 解答 2.1. 方法一 从矩阵的左下角开始比较 目标值等于当前元素,返回 true: 目标值大于当前元素,j 增 1,向右查找,排除掉此列上边的数据(都比当前元素更小): 目标值小于 ...
- 简析@Resource 和 @Autowired的区别
@Autowird @Autowird 属于spring框架,默认使用类型(byType)进行注入,例如下面代码: @Autowired public IUserService userService ...
- SpringCloud IDEA 教学 番外篇 后台运行Eureka服务注册中心
写在开头 研发过程中经常要做的事就是启动Eureka服务注册中心,每每都要启动一个IDEA,很是困扰.现在分享一个后台启动服务注册中心的方法. 准备工作 1打包一个eureka服务注册中心jar包(注 ...
- Eclipse安装颜色主题,个性化你的IDE,让你的IDE焕然一新
我们都知道eclipse默认的颜色主题是白色的背景,但是如果想改变代码编辑区的背景颜色,需要怎么办呢? 今天给大家介绍一个非常赞的eclipse,可以很方便的根据自己的需求选择喜欢的颜色主题,其他的不 ...
- UESTC 1717 Journey(DFS+LCA)(Sichuan State Programming Contest 2012)
Description Bob has traveled to byteland, he find the N cities in byteland formed a tree structure, ...
- 硬件PCB Layout布局布线Checklist检查表(通用版)
按部位分类 技术规范内容 1 PCB布线与布局 PCB布线与布局隔离准则:强弱电流隔离.大小电压隔离,高低频率隔离.输入输出隔离.数字模拟隔离.输入输出隔离,分界标准为相差一个数量级.隔离方法包括:空 ...
- iconFont 阿里巴巴矢量图标使用方法
挑选图标的过程(共6步) 进入网站:Iconfont网址:http://www.iconfont.cn 点击网站上方的“官方图标库”,选择自己喜欢的图标.在这里我选择天猫的图标库. 选择好自己喜欢的图 ...
- 自测之Lesson9:时钟与信号
题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...