1054 The Dominant Color (20 分)【难度: 简单 / 知识点: 思维】
生活随笔
收集整理的這篇文章主要介紹了
1054 The Dominant Color (20 分)【难度: 简单 / 知识点: 思维】
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://pintia.cn/problem-sets/994805342720868352/problems/994805422639136768
因?yàn)閿?shù)量多于一半,故排序后中間必為答案。
摩爾投票法
#include<bits/stdc++.h> using namespace std; int x,n,m; stack<int>st; int main(void) {cin>>m>>n;for(int i=0;i<n;i++)for(int j=0;j<m;j++){int x; cin>>x;if(st.empty()) st.push(x);else if(st.top()!=x) st.pop();else if(st.top()==x) st.push(x);}cout<<st.top();return 0; }總結(jié)
以上是生活随笔為你收集整理的1054 The Dominant Color (20 分)【难度: 简单 / 知识点: 思维】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 1052 Linked List Sor
- 下一篇: 1055 The World‘s Ric