1100. Final Standings
Time limit: 1.0 second
Memory limit: 16 MB
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 next
N lines contains two integers 1 ≤
ID ≤ 10
7 and 0 ≤
M ≤ 100.
ID — unique number of team,
M — number of solved problems.
Output
Output should contain
N lines with two integers
ID and
M on each. Lines should be sorted by
M in descending order as produced by bubble sort (see below).
Sample
input | output |
---|---|
8 1 2 16 3 11 2 20 3 3 5 26 4 7 1 22 4 | 3 5 26 4 22 4 16 3 20 3 1 2 11 2 7 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;
}