洛谷P2397 yyy loves Maths VI (mode) 摩尔投票
生活随笔
收集整理的這篇文章主要介紹了
洛谷P2397 yyy loves Maths VI (mode) 摩尔投票
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
求眾數的一種方法
不需要內存,排序
每次記錄上次加的值以及個數,相等個數+1,不相等就-1
因為眾數大于一半 最壞的情況也就是拿一個眾數取走一個非眾數
最后的結果肯定就是眾數
//#pragma comment (linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<time.h>
#include<bitset>#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),z)
#define max3(x, y, z) max(max(x,y),z)
#define max4(a, b, c, d) max(max(a,b),max(c,d))
#define pii make_pair
#define pr pair<int,int>
typedef unsigned long long ull;
typedef long long ll;
const int inff = 0x3f3f3f3f;
const long long inFF = 9223372036854775807;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int mdir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
const double eps = 1e-10;
const double PI = acos(-1.0);
const double E = 2.718281828459;
using namespace std;
const int mod=1e9+7;
int num,cnt;
int main()
{int n,x;cnt=0;cin>>n;while(n--){cin>>x;if(cnt==0) cnt++,num=x;else{if(num!=x) cnt--;else cnt++;}}cout<<num<<endl;
}
?
總結
以上是生活随笔為你收集整理的洛谷P2397 yyy loves Maths VI (mode) 摩尔投票的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P2396 yyy loves Math
- 下一篇: 洛谷P1896 [SCOI2005]互不