• codechef Turbo Sort 题解


    Input

    t – the number of numbers in list, then t lines follow [t <= 10^6]. 

    Each line contains one integer: N [0 <= N <= 10^6]

    Output

    Output given numbers in non decreasing order.

    Example

    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
    ", &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] == '
    ')
    			{
    				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
    ", A[i]);//使用cout会超时,最少慢5倍
    	}
    	delete [] A;
    	return 0;
    }



  • 相关阅读:
    洛谷p1056
    __int64
    杭电2057
    4.4清北学堂Day1 主要内容:数论,数学
    递推的一点理解
    高精度减法
    高精度加法
    p1184高手之在一起
    对于rqy今天讲座的一些理解和看法吧
    PHP.21-商品信息管理
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3788728.html
Copyright © 2020-2023  润新知