BZOJ2456 Mode & zju2132 The Most Frequent Number

题目

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。

代码

MLE的map= =

本来还以为用个map可以划划水过了

1M内存= =数据应该是专门卡map的

#include<cstdio>
#include<bits/stdc++.h>
#include<algorithm>
#define R0(i,n) for(register int i=0;i<n;++i)
#define R1(i,n) for(register int i=1;i<=n;++i)
#define INF 707406378
using namespace std;
typedef long long ll;
int n,ans,t,mxcnt=0;
map<int,int>m;
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int main(){
    n=read();
    R0(i,n){
        t=read(),m[t]++;
        if(mxcnt<m[t])mxcnt=m[t],ans=t;
    }
    printf("%d",ans);
    return 0;
}

O(n)求众数

原=>kivienst

众数性质

【性质一】如果一个数在某区间内出现的次数大于区间长度的一半,则这个数一定是该区间的第(r - l + 1) >> 1 大 –>可以用划分树求区间第k大

【性质二】如果一个数在某区间内出现的次数大于区间长度的一半,则这个数的每个二进制位在该区间内出现的次数也一定大于区间长度的一半!!!所以就可以直接确定出那个数!!!!!

【性质三】对两两不相等的数进行抵消,则最后剩下的数则是该众数!!!

用num记录当前抵消后剩下的数,当num=0的时候把该数变为tmp,不等则cnt–,相等则cnt++

#include <cstdio>
#include <cstdlib>
int n, num;
int main(){
scanf("%d%d", &n, &num);
int cnt = 1;
for (int i = 2; i <= n; i++){
    int tmp; scanf("%d", &tmp);
    if (cnt == 0){
        cnt = 1;
        num = tmp;
        continue;
    }
    if (tmp == num) cnt++;
    else cnt--;
    }
    printf("%d\n", num);
    return 0;
}
时间: 2024-08-14 09:29:35

BZOJ2456 Mode & zju2132 The Most Frequent Number的相关文章

zoj2132-The Most Frequent Number

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2132 题意很简单.这里采用O(1)算法. 1 #include <cstdio> 2 3 int main() 4 { 5 int n,ans; 6 while(~scanf("%d",&n)) 7 { 8 int m,cnt=0; 9 for(int i=0;i<n;i++) 10 { 11 scanf("%d",

【BZOJ2456】mode 神奇的卡内存题

以后把题解放在前面,估计没人看题解先看题... 内存1M,4个int(其实对内存的概念十分模糊),众数即为出现次数最多的数,可以用抵消的思想(但是众数不是可以是一大坨么...) 1 #include <cstdio> 2 using namespace std; 3 int now,n,a,t; 4 int main() 5 { 6 scanf("%d",&n); 7 for (int i=1;i<=n;i++) 8 { 9 scanf("%d&qu

[BZOJ2456] mode

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. zju2132 The Most Frequent Number Source 鸣谢 黄祎程 Solution 历

2456: mode

2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 4798  Solved: 2009[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 HI

BZOJ 2456 杂题 卡内存

2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 3702  Solved: 1551[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 HI

BZOJ 2456

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. zju2132 The Most Frequent Number Source 鸣谢 黄祎程 AC代码: #incl

BZOJ 2456: mode(新生必做的水题)

2456: mode Time Limit: 1 Sec  Memory Limit: 1 MB Submit: 4868  Solved: 2039 [Submit][Status][Discuss] Description 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数. Input 第1行一个正整数n.第2行n个正整数用空格隔开. Output 一行一个正整数表示那个众数. Sample Input 53 2 3 1 3 Sample Output 3 H

[BZOJ] 2456: mode #众数计数法

2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 5969  Solved: 2414[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 HI

[题解]poj 3368 Frequent values

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16537   Accepted: 5981 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indice