问题 G: 想上分的鲸鱼
来源:互联网 发布:知乎电子书 编辑:程序博客网 时间:2024/04/27 23:17
这道题来自NEUQ
想上分的鲸鱼
题目描述
王者荣耀是现在比较火的手机游戏,游戏中有70个英雄可供玩家挑选,玩家可以用这70个英雄参加排位赛提升段位。而刚玩这个游戏的鲸鱼学姐想提升到白金段位,但是她靠自己单独排位很难上分,于是她求助了另外几个学长,另外的学长也答应帮她,但是他们需要趁手的英雄,另外几个学长把他们想玩的英雄的编号告诉了鲸鱼学姐,鲸鱼学姐要根据他们给出的编号挑出五个需求量最大的英雄。然而,学姐要练车,没有时间来统计,于是他找到了plyjdz,然而比较懒的plyjdz(其实就是皮卡丘啦),想把这个任务交给学弟学妹们。
输入
学长们给出了很多套不同方案。(方案不超过100套)
每套方案有不超过m个英雄的编号x,编号间以空格隔开,每套方案以0为结束的标志。(5<=m<=1000,1<=x<=70)
保证输入数据合法
保证输入至少有一套方案
输出
每行输出一套方案所求出的最大需求量,按照数字的升序输出需求量最大的五个英雄的编号。
关于升序:
就是一串变量按照由小到大(数字)、由开始到结束(事件)由A到Z(字母)的顺序排列。
样例输入
1 2 3 4 5 0
1 1 2 3 4 5 6 6 0
5 5 4 1 1 2 3 2 1 4 6 6 7 8 0
15 45 12 54 45 12 45 8 48 12 45 48 45 41 2 45 48 65 23 11 25 23 16 14 15 18 19 20 12 15 1 23 45 48 12 45 54 12 45 48 45 14 41 45 48 65 23 11 25 23 16 14 15 15 15 15 15 15 0
样例输出
1 2 3 4 5
1 2 3 4 6
1 2 4 5 6
12 15 23 45 48
这道题需要用到sort
提示:algorithm头文件中的sort可以给任意对象排序,包括内置类型和自定义类型,前提是类型定义了“<”运算符。排序之后可以用lower_bound查找大于或等于x的第一个位置。带排序/查找的元素可以放在数组里,也可以放在vector里面(前者用sort(a,a+n)后者用sort(v.begin(),v.end())的方式调用)
#include <iostream>#include <map>#include<algorithm>#include <queue>using namespace std;struct node { int key, count; bool operator < (const node& n) const { //定义排序 if (count == n.count) return key > n.key; else return count < n.count; }};map<int, int> m;//函数输入bool init(){ int v; bool flag = false; while (~scanf("%d", &v) && v) { flag = true; m[v]++; } return flag;}int main(){ int maxn[5]; priority_queue<node> q; while (init()){ node keyval; for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) { keyval.key = it->first; keyval.count = it->second; q.push(keyval); } int i = 0; while (!q.empty()) { keyval = q.top(); q.pop(); maxn[i] = keyval.key; i++; if(i==5) break; } //清空队列 while (!q.empty()) { q.pop(); } sort(maxn, maxn + 5); for (int i = 0; i < 4; i++) printf("%d ",maxn[i]); printf("%d\n",maxn[4]); //清空map m.erase(m.begin(), m.end()); } return 0;}
这道题和ccf201503-2(这道是老师博客的解题)数字排序差不多,嗯么,改了一下输入和其中的一些,道理一样。get了新的函数输入和sort关于结构体的排序的方法。
这是在下的一位大佬写的。相比于我写的那个用map更加简单,所以这题直接用hero的结构体,定义一个数组,对输入的进行排序。最后输出的时候注意一下形式。
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int N=70+2;struct hero{ int id; int num; bool operator < (const hero &a) const { return num>a.num?1:(num==a.num?(id<a.id):0); }}h[N];bool init(){ for(int i=0;i<N;i++) { h[i].id=i; h[i].num=0; } int v; bool flag=false; while(~scanf("%d",&v)&&v) { flag=true; h[v].num++; } return flag;}int main(){ int ans[5]; while(init()) { sort(h,h+N); for(int i=0;i<5;i++) ans[i]=h[i].id; sort(ans,ans+5); for(int i=0;i<4;i++) printf("%d ",ans[i]); printf("%d\n",ans[4]); } return 0;}
此处的重点应该是
会使排序更简单。
bool operator < (const node& n) const { //定义排序 if (count == n.count) return key > n.key; else return count < n.count; }
还有一个就是函数输入
- 问题 G: 想上分的鲸鱼
- 问题:鲸鱼等濒危物种之个体出事了,目前是谁来救的?
- 社交游戏的鲸鱼理论【信息图】
- 拥有鲸鱼的身体,但像鲨鱼一样的灵活
- 每个家庭都是曾经搁浅在沙滩上的鲸鱼
- 鲸鱼哲学
- 关于m//g的问题
- G-sensor的抖动问题
- printf %g 的精度问题
- 为了鲸鱼开发的移动设备游戏——傲娇小女生
- 因为docker酱的小鲸鱼宝宝很可爱所以很喜欢!
- docker4dotnet #3 .net猿和小鲸鱼的苹果山之旅
- Docker:替代传统虚拟机的“鲸鱼”(Docker: A 'Whale' Replacing Traditional VM)
- 那个让鲸鱼跃出篮球场的Magic Leap,终于发布了一款AR眼镜
- 那个让鲸鱼跃出篮球场的Magic Leap,终于发布了一款AR眼镜
- 4G七问, 读懂4G的核心问题
- 鲸鱼与池塘
- 鲸鱼币挖矿教程
- 非库存采购的自动记帐
- 第六天:图像的基本运算
- 浅析GridView制作九宫格列表
- 代码综合后的电路对比(不定期更新)
- 一文读懂集成学习
- 问题 G: 想上分的鲸鱼
- Linux编程5中IO模型
- unity实现任意多边形三角剖分
- KazaQ's Socks
- Mayor's posters(区间覆盖+点离散化)
- [ubuntu]安装virtualenv,virtualenvwrapper出错
- Zookeeper系列(一)
- 进程的概念及编程1
- 实现HTML5 裁剪图片并上传