zoukankan      html  css  js  c++  java
  • bzoj2456 mode

    2456: mode

    Time Limit: 1 Sec  Memory Limit: 1 MB
    Submit: 3952  Solved: 1661
    [Submit][Status][Discuss]

    Description

    给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。

    Input

    第1行一个正整数n。
    第2行n个正整数用空格隔开。

    Output

        一行一个正整数表示那个众数。

    Sample Input

    5
    3 2 3 1 3

    Sample Output

    3

    HINT

    100%的数据,n<=500000,数列中每个数<=maxlongint。

     
    题解
    好神的题……一开始觉得是裸的hash然后一看内存……然后就去网上膜了题解……
    运用抵消的思想,记录下当前还没被抵消完的值和还没被抵消的次数tot,如果和读入的数相同就tot++否则tot--,最后剩下的就是答案了
    太神了……
     1 /**************************************************************
     2     Problem: 2456
     3     User: 1090900715
     4     Language: Pascal
     5     Result: Accepted
     6     Time:628 ms
     7     Memory:220 kb
     8 ****************************************************************/
     9  
    10 program j01;
    11 var n,t,tot,x:longint;
    12 begin
    13   readln(n);
    14   t:=0;tot:=0;
    15   while n>0 do
    16   begin
    17     dec(n);
    18     read(x);
    19     if tot=0 then
    20     begin
    21       tot:=1;
    22       t:=x;
    23     end;
    24     if x=t then inc(tot) else dec(tot);
    25   end;
    26   writeln(t);
    27 end.
    28 
  • 相关阅读:
    AD 485、422电路
    AD 差分
    思维导图
    68 二叉树的最近公共祖先
    65. 不用加减乘除做加法
    64. 求1+2+…+n
    10- I. 斐波那契数列
    11&12. 旋转数组的最小数字
    12. 矩阵中的路径
    13. 机器人的运动范围
  • 原文地址:https://www.cnblogs.com/oldjang/p/6159056.html
Copyright © 2011-2022 走看看