ural 1100. Final Standings -排序

来源:互联网 发布:查看linux oracle监听 编辑:程序博客网 时间:2024/06/17 02:41

1100. Final Standings

Time limit: 1.0 second
Memory limit: 16 MB
Old contest software uses bubble sort for generating final standings. But now, there are too many teams and that software works too slow. You are asked to write a program, which generates exactly the same final standings as old software, but fast.

Input

The first line of input contains only integer 1 <N ≤ 150000 — number of teams. Each of the nextN lines contains two integers 1 ≤ID ≤ 107 and 0 ≤M ≤ 100.ID — unique number of team, M — number of solved problems.

Output

Output should contain N lines with two integersID andM on each. Lines should be sorted by M in descending order as produced by bubble sort (see below).

Sample

inputoutput
81 216 311 220 33 526 47 122 4
3 526 422 416 320 31 211 27 1

Notes

Bubble sort works following way:
while (exists A[i] and A[i+1] such as A[i] < A[i+1]) do
   Swap(A[i], A[i+1]);


题意
根据m将n个数按冒泡排序法则排序 输出结果

解题思路
冒泡排序 TLE 了,sort 是不稳定的排序。
stable_sort的用法与sort一致,区别是stable_sort函数遇到两个数相等时,不对其交换顺序
当函数参数传入的是结构体时,会发现两者之间的明显区别;

代码:
#include <iostream>#include <cstdio>#include <cstring>#include <functional>#include <algorithm>#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;struct Node{    int id,m;}node[150010];bool cmp(const Node &a,const Node &b){    return a.m>b.m;}int main(){    int n;    scanf("%d",&n);    for(int i=0;i<n;++i){        scanf("%d%d",&node[i].id,&node[i].m);    }    stable_sort(node,node+n,cmp);    for(int i = 0;i<n;++i){        printf("%d %d\n",node[i].id,node[i].m);    }    return 0;}

1 0
原创粉丝点击