codevs 1860_最大数_优先队列

发布于 2019-05-12  968 次阅读


题目描述

 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数。


思路

用字符串建一个优先队列,然后输出就可以了
O(nlogn)

#include <stdio.h>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
struct cmp
{
 bool operator()(string a,string b)
 {
  return a+b<b+a;
 }
};
priority_queue<string,vector<string>,cmp > q;
int main()
{
    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        char xx[100];
        char *x=xx;
        scanf("%s",x);
        q.push(string(x));
    }
    while (!q.empty())
    {
        cout<<q.top();
        q.pop();
    }
    printf("\n");
}