【贪心】【UVA10905】 Children's Game

传送门

Description

  给定n个正整数,求他们相连接后能形成的最大整数。例如:12,23这两个数能连接的最大数是2312,。

Input

  多组数据,每组数据中:

  • 第一行为一个整数n
  • 第二行有n个整数,代表给出的数。

  输入结束的标志为n=0。

Output

  对于每组数据,输出:

  • 能拼成的最大整数

Sample Input

4
123 124 56 90
5
123 124 56 90 9
5
9 9 9 9 9
0

Sample Output

9056124123
99056124123
99999

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>'9'||ch<'0') lst=ch,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    if (lst=='-') x=-x;
}

char buf[20];
inline void write(int x,const char aft,const bool pt) {
    if(x<0) {putchar('-');x=-x;}
    int top=0;
    do {
        buf[++top]=x%10+'0';
        x/=10;
    } 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<0) return -a;return a;}

template <typename T>
inline void mswap(T &a,T &b) {T temp=a;a=b;b=temp;}

const int maxn = 55;

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=1;i<=n;++i) std::cin>>MU[i];
        std::sort(MU+1,MU+1+n,cmp);
        for(int i=1;i<=n;++i) std::cout<<MU[i];
        putchar('\n');
        n=0;qr(n);
    }
    return 0;
}

Summary

  对于这类贪心题目的证明方式需要格外注意和掌握。

  虽然你不想用stl,但是如果你不想手写拼接(其实也不难写)的话还是考虑string的使用

猜你喜欢

转载自www.cnblogs.com/yifusuyi/p/9440228.html