2456: mode

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=2456

Description

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

Input

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

Output

表示为了聚会走的路程和最小为多少.

Sample Input

5
3 2 3 1 3

Sample Output

3

HINT

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

题意

 

题解:

因为保证出现最多的数一定是的大于n/2的,那么我们就可以直接利用抵消来搞定就好了

注意不要using namespace std,加了这个就会MLE……

代码:

#include<stdio.h>


int main()
{
    int n,ans1,ans2;
    scanf("%d%d",&n,&ans1);
    ans2=1;
    for(int i=1;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        if(ans2==0)
            ans2=1,ans1=x;
        else if(ans1==x)
            ans2++;
        else
            ans2--;
    }
    printf("%d\n",ans1);
}

 

相关文章:

  • 2022-02-20
  • 2021-10-27
  • 2022-02-16
  • 2022-01-03
  • 2021-08-10
  • 2021-10-28
  • 2021-09-01
  • 2021-12-11
猜你喜欢
  • 2021-05-31
  • 2021-10-10
  • 2021-12-08
  • 2021-12-07
相关资源
相似解决方案