题解 | #投票统计#

投票统计

https://ac.nowcoder.com/acm/problem/15203

一看离散化,一手STL,来个map,用个sort,反手再加一个unique,提交,发现95%,用时:1001ms。这时候,再细看一下题,1e5,也不是很大呀,em... 发现STL用的多了,去掉sort,unique。就用map. alt 解题思路: 输入,存入map,并将map[输入元素]++。遍历map,用迭代器或者auto,先找最大值,之后再遍历,找跟最大值相同的,让ans++。最后判断最大值个数与输入个数是否相等,相等--"-1",否则输出ans,再遍历一遍,将与最大值相等的打印出来。 [it->first 表示题目编号 it->second 表示题目编号表示的作品的选票]

#include <vector>
#include <string>
#include <set>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;

#define rep(i,x,n) for(int i = x; i <= n; i++)

typedef long long LL;
typedef pair<int,int> PII;

const int N = 100021;
int p[N];
map<int,int> mp;
void test();

void solve() {
    int n;
    cin>>n;
    mp.clear(); // mp 全局变量,每次清零。
    int ma = 0;
    for(int i = 0; i < n; ++i) {
        int k;
        cin>>k;
        mp[k]++; // 映射
        ma = max(ma, mp[k]); // 找最大票数。
    }
    int cnt = 0;
    // 迭代器遍历
    // it->second  -- 票数;
    // it->first  -- 编号;
    for(map<int,int>::iterator it = mp.begin(); it != mp.end(); ++it) {
        if(ma == it->second) cnt++;
    }
    // 票数都一样
    if(cnt == mp.size()) {
        cout<<-1<<endl;
        return ; // 结束
    }
    cout<<cnt<<endl;
    for(map<int,int>::iterator it = mp.begin(); it != mp.end(); ++it) {
        // 查找编号
        if(ma == it->second) cout<<it->first<<" ";
    }
    cout<<endl;

}
int main()
{
    //test();
    //ios::sync_with_stdio(false); cin.tie(0),cout.tie();
    int t;
    scanf("%d", &t);
    while(t--) solve();

    return 0;
}

void test() {
    #define mytest
    #ifdef mytest
    freopen("ANSWER.txt", "w",stdout);
    #endif
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
2024-12-30 18:02
程序员牛肉:1.可以标记一下自己的学校是985,有一些hr可能没想到你这个院校是985的。 2.简历所呈现出来的能力还是有点差的,苍穹外卖+黑马点评。这在java技术域里面也就是刚学三四个月的样子,大厂现在招人少,小厂又更加希望你能直接过来干活。就你简历上呈现出来的能力,确实是有点难找,肉眼可见的不懂技术。 第一个项目中:简单的使用redis也算是亮点嘛?使用jwt,threadlocal也算是亮点?你不就是调了几个包嘛?Nginx作为服务器也能写出来,这不是前端的活嘛? 第二个项目中:分布式锁+mq消息队列+Lua队列。真没啥好问的。属于面试官看一眼就阳痿的简历,没有任何想提问的欲望。 我给你建议是好好的挖一挖这个项目吧,其实苍穹外卖和黑马点评这两个项目很不错了,只不过是太烂大街了导致面试官没啥问的兴趣,所以不太推荐写简历上。
点赞 评论 收藏
分享
头发暂时没有的KFC总裁:找廉价劳动力罢了
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务