• [NOIP2007]统计数字


    NOIP 2007 提高第一题

    题目描述

    某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。

    输入输出格式

    输入格式:

    输入文件count.in包含n+1行;
    第一行是整数n,表示自然数的个数;
    第2~n+1每行一个自然数。

    输出格式:

    输出文件count.out包含m行(m为n个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。

    输入输出样例

    输入样例#1:

    8
    2
    4
    2
    4
    5
    100
    2
    100
    
    

    输出样例#1:

    2 3
    4 2
    5 1
    100 2
    

    说明

    40%的数据满足:1<=n<=1000
    80%的数据满足:1<=n<=50000
    100%的数据满足:1<=n<=200000,每个数均不超过1500 000 000(1.5*109)

    思路

    看到这道题的数据,我首先就机智地想到了这一定不能用桶排,用也要离散化重标号,于是我就机制的写了一个离散化。然而只有70分,这是怎么回事口牙?

    type ss=record
        sum,shu:longint;
        end;
    
    var a:array[0..100000] of ss;
        i,j,x,n,kg:longint;
        xx:longint=1;
    
    procedure sort(l,r: longint);
          var
             i,j,x:longint;
          begin
             i:=l;
             j:=r;
             x:=a[(l+r) div 2].shu;
             repeat
               while a[i].shu<x do
                inc(i);
               while x<a[j].shu do
                dec(j);
               if not(i>j) then
                 begin
                    a[0]:=a[i];
                    a[i]:=a[j];
                    a[j]:=a[0];
                    inc(i);
                    j:=j-1;
                 end;
             until i>j;
             if l<j then
               sort(l,j);
             if i<r then
               sort(i,r);
          end;
    
    begin
        fillchar(a,sizeof(a),0);
        readln(n);
        readln(x);
        a[xx].shu:=x;
        a[xx].sum:=1;
        for i:=1 to n-1 do
            begin
                readln(x);
                kg:=0;
                for j:=1 to xx do
                    if x=a[j].shu then
                        begin
                            inc(a[j].sum);
                            kg:=1;
                            break;
                        end;
                if kg=0 then
                    begin
                        inc(xx);
                        a[xx].shu:=x;
                        a[xx].sum:=1;
                    end;
            end;
        sort(1,xx);
        for i:=1 to xx do writeln(a[i].shu,' ',a[i].sum);
    end.
    70%

    后来又机制的想了想,我干嘛要去做上面那个时间复杂度O(kn+nlogn)的算法,快排一遍扫一下重不就是轻松加愉快吗,所以AC代码出炉!

    var a:array[0..2000001] of longint;
        x,i,j,n,sum:longint;
    
    procedure sort(l,r: longint);
          var
             i,j,x:longint;
          begin
             i:=l;
             j:=r;
             x:=a[(l+r) div 2];
             repeat
               while a[i]<x do
                inc(i);
               while x<a[j] do
                dec(j);
               if not(i>j) then
                 begin
                    a[0]:=a[i];
                    a[i]:=a[j];
                    a[j]:=a[0];
                    inc(i);
                    j:=j-1;
                 end;
             until i>j;
             if l<j then
               sort(l,j);
             if i<r then
               sort(i,r);
          end;
    
    begin
        readln(n);
        for i:=1 to n do readln(a[i]);
        sort(1,n);
        x:=a[1];
        sum:=1;
        for i:=2 to n+1 do
            begin
                if a[i]=x then inc(sum);
                if a[i]<>x then
                    begin
                        writeln(x,' ',sum);
                        x:=a[i];
                        sum:=1;
                    end;
            end;
    end.
    AC
  • 相关阅读:
    win7 下如何安装 Microsoft Web Application Stress Tool
    [译文]casperjs的API-mouse模块
    【性能测试】jmeter的坑(1)——如何在多网卡情况下正确连接
    [性能分析]端口限制
    [性能分析]linux文件描述符
    python 对mongodb进行压力测试
    常用jar包信息
    Basic Grammer
    Maven 知识汇总
    【Linux】Linux常用命令
  • 原文地址:https://www.cnblogs.com/yangqingli/p/4751951.html
Copyright © 2020-2023  润新知