codechef Turbo Sort 题解
2014-06-14 21:30
309 查看
[title3]
Input[/title3]
t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]
[title3]
Output[/title3]
Output given numbers in non decreasing order.
[title3]
Example[/title3]
Input:
5
5
3
6
7
1
Output:
1
3
5
6
7
大数据的排序,输入和输出。一開始使用了cout,那么就超时了,后来换用printf,结果过了,速度快上五倍以上。 putchar应该更加快,然后更快的就是使用buffer了,使用函数fwrite.
本oj站点首先学到的就是数据输入输出问题了。
#include<stdio.h> #include <iostream> #include <algorithm> using namespace std; int TurboSort() { int T = 0, num = -1, c = 0, j = 0; scanf("%d\n", &T); char buffer[1000000]; int *A = new int[T]; while ((c = fread(buffer, 1, 1000000, stdin)) > 0) { for (int i = 0; i < c; i++) { if (buffer[i] == '\n') { A[j++] = num; num = -1; } else { if (-1 == num) num = buffer[i] - '0'; else num = num * 10 + buffer[i] - '0'; } } } if (-1 != num) A[T-1] = num; sort(A, A+T); for (int i = 0; i < T; i++) { printf("%d\n", A[i]);//使用cout会超时,最少慢5倍 } delete [] A; return 0; }
相关文章推荐
- codechef Turbo Sort 题解
- Codechef Observing the Tree
- www.codechef.com/
- [bzoj4299]Codechef FRBSUM
- codechef FUN WITH TREES
- CodeChef:A temple of Snakes(思维 & 二分)
- Chef and Graph Queries (codechef)
- Educational Codeforces Round 30-D-Merge Sort(瞎搞)
- [题解]CodeChef JUNE Challenge 17
- Quick sort C# code
- 【codechef FNCS】【Chef and Churu】【分块】
- codechef-Chef and Prime Divisors
- CodeChef 2015年12月问题
- bzoj 4546: codechef XRQRS 可持久化trie
- 4260: Codechef REBXOR
- Codeforces Round #277.5 (Div. 2)A. SwapSort
- Codechef GRAPHCNT 支配树学习及tarjan算法求解
- CodeChef:L-R queries(线段树 & set)
- codechef Factorial 算法 fread读入数据
- codechef Permutation Cycles 题解