[NJUPSOJ]Top2
description:
输入\(n\)个数, 求其中出现次数最多的两个数, 从小到大输出(这两个数出现次数不小于\(\lceil \frac{n}{3} \rceil\)).
solution:
如果\(3\)个数互不相同, 把他们除掉, 剩下的里面众数和第二众数不变.
code:
#include<cstdio>
int t, n, top1, top2, ct1, ct2;
int main() {
scanf("%d", &n);
while (n--) {
scanf("%d", &t);
if (!ct1)top1 = t, ++ct1;
else if (!ct2)top2 = t, ++ct2;
else if (t == top1)++ct1;
else if (t == top2)++ct2;
else --ct1, --ct2;
}
if (top1 > top2)t = top1, top1 = top2, top2 = t;
printf("%d %d", top1, top2);
}