ural1100--Final Standings--数据结构--结构体排序
2013-09-21 10:46
417 查看
ORZ翁教把URAL的数据结构刷完了,于是也尝试着开始刷URAL的数据结构。。。。FIGHTTING!
很囧的在URAL的第一跑就WRONG了,还是跪给了数据结构的第一题。
诶,好讨厌= =#。。。
以下是题目。。。。。。
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.
0 ≤ M ≤ 100. ID — unique number of team, M — number of solved problems.
Problem Author: Pavel Atnashev
Problem Source: Tetrahedron Team Contest May 2001
Tags: (
show tags for all problems
)
题意很简单。。。。
原以为用sort排个序即可。。。
wrong了后才发现,在value相等的情况下,不能对元素进行交换。。而sort(用的是快排,是不稳定的排序方式,因而会打乱顺序)
那要怎么办呢?
问了学长才知道有个叫做stable_value的东西~~
所谓stable_sort,是指对一个序列进行排序之后,如果两个元素的值相等,则原来乱序时在前面的元素现在(排好序之后)仍然排在前面。STL中提供stable_sort()函数来让我们进行稳定排序。为了更好的说明稳定排序的效果,我们定义了一个结构体元素,一个value成员和一个index成员,前者表示元素的值,后者表示乱序时的索引。
基础知识还是太弱啊!要多刷题!
以下是AC代码:
很囧的在URAL的第一跑就WRONG了,还是跪给了数据结构的第一题。
诶,好讨厌= =#。。。
以下是题目。。。。。。
1100. Final Standings
Time limit: 1.0 secondMemory 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 ≤ 107 and0 ≤ 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 using bubble sort (or analog).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 |
Hint
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]);
Problem Author: Pavel Atnashev
Problem Source: Tetrahedron Team Contest May 2001
Tags: (
show tags for all problems
)
题意很简单。。。。
原以为用sort排个序即可。。。
wrong了后才发现,在value相等的情况下,不能对元素进行交换。。而sort(用的是快排,是不稳定的排序方式,因而会打乱顺序)
那要怎么办呢?
问了学长才知道有个叫做stable_value的东西~~
所谓stable_sort,是指对一个序列进行排序之后,如果两个元素的值相等,则原来乱序时在前面的元素现在(排好序之后)仍然排在前面。STL中提供stable_sort()函数来让我们进行稳定排序。为了更好的说明稳定排序的效果,我们定义了一个结构体元素,一个value成员和一个index成员,前者表示元素的值,后者表示乱序时的索引。
基础知识还是太弱啊!要多刷题!
以下是AC代码:
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxnum=150010; struct student { int id; int val; bool operator<(const student&temp) const { return val>temp.val; } }a[maxnum]; int main() { //freopen("input.txt","r",stdin); int n; scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d%d",&a[i].id,&a[i].val); stable_sort(a+1,a+1+n); for(int i=1;i<=n;++i) printf("%d %d\n",a[i].id,a[i].val); return 0; }
相关文章推荐
- ural 1100. Final Standings(数据结构)
- [URAL 1100]Final Standings(排序技巧)
- URAL 1100. Final Standings (排序)
- Ural 1100. Final Standings
- Ural 1100 Final Standings
- ural1100 Final Standings
- Ural_1100. Final Standings(stable_sort)
- NSWOJ ~ 1100 ~ —种排序(结构体排序)
- URAL 1100 Final Standings
- 开始学数据结构——(三):插入排序
- 排序问题(结构体排序)
- 数据结构66:简单选择排序、树形选择排序和堆排序算法
- 【算法设计与数据结构】二分法解决最大值最小化问题—进阶篇— URAL 2034 Caravans
- 数据结构-栈--结构体实现
- UVA-11134-Fabled Rooks (结构体排序+贪心)
- Problem F: 结构体--学生信息排序
- vector中自定义结构体的排序
- python数据结构学习笔记-2016-10-23-02-排序
- 数据结构排序问题_2
- 深层次两张图解经典6大排序与6大基础数据结构——学完这些,妈妈再也不用担心我的排序算法与数据结构,学习笔记大放送