【hdu1280】前M大的数
前m大的数
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19208 Accepted Submission(s):
6563
给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
第一行两个数N和M,
第二行N个数,表示该序列。
1 2 3 4
4 5
5 3 6 4
11 10 9 9 8
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M;
int a[100001];
int heap[100001];
int len; void insert(int x){
heap[++len]=x;
for(int k=len;k!=1;k/=2){
if(heap[k]<heap[k/2]) swap(heap[k],heap[k/2]);
else break;
}
return ;
}
void delet(){
if(len==0) return ;
heap[1]=heap[len];len--;
for(int k=1;k*2<=len;){
if((k*2<=len)&&(k*2+1>len)){
if(heap[k*2]<heap[k]) swap(heap[k*2],heap[k]),k*=2;
else break;
}
else{
if(heap[k*2]<heap[k]||heap[k*2+1]<heap[k]){
if(heap[k*2]<heap[k*2+1]) swap(heap[k],heap[k*2]),k*=2;
else swap(heap[k],heap[k*2+1]),k*=2,k++;
}
else break;
}
}
return ;
}
int ans[100001];
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
while(scanf("%d%d",&N,&M)!=EOF){
len=0;
for(int i=1;i<=N;i++) a[i]=read();
for(int i=1;i<=N;i++)
for(int j=i+1;j<=N;j++){
insert(a[i]+a[j]);
if(len>M) delet();
}
for(int i=1;i<=M;i++) ans[i]=heap[1],delet();
for(int i=M;i>1;i--) printf("%d ",ans[i]);
printf("%d\n",ans[1]);
}
return 0;
}
优先队列代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M;
priority_queue<int ,vector<int> , greater<int> > Que;
int a[100001];
int ans[100001];
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
while(scanf("%d%d",&N,&M)!=EOF){
for(int i=1;i<=N;i++) a[i]=read();
for(int i=1;i<=N;i++)
for(int j=i+1;j<=N;j++){
Que.push(a[i]+a[j]);
if(Que.size()>M) Que.pop();
}
for(int i=1;i<=M;i++) ans[i]=Que.top(),Que.pop();
for(int i=M;i>1;i--) printf("%d ",ans[i]);
printf("%d\n",ans[1]);
}
return 0;
}
【hdu1280】前M大的数的更多相关文章
- HDU1280前m大的数creat at 9:51,3.13,2016
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- hdu1280 前m大的数(数组下标排序)
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 【水题】HDU--1280 前m大的数
还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就 ...
- 前m大的数(hdu1280)
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- hdu---(1280)前m大的数(计数排序)
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 找出数组前N大的数
这个题也是个比较有名的面试题.当然有很多变种. 题目意思基本是:从一个数据量很大的数组里找前N大的元素.不允许排序. 这个题有两个比较好的思路: 思路一:用快速排序的思想,是思想,不是要排序; 思路二 ...
- HDU 1280 前m大的数
http://acm.hdu.edu.cn/showproblem.php?pid=1280 前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- 输出前n大的数(分治)
描述:给定一个数组包含n个元素,统计前m大的数并且把这m个数从大到小输 出. 输入: 第一行包含一个整数n,表示数组的大小.n < 100000.第二行包含n个整数,表示数组的元素,整数之间以一 ...
- 4J - 前m大的数
还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就 ...
随机推荐
- [Unity]模拟雨水的折射效果
用GrabPass做的小玩具. 并不是真的计算了折射,只是简单地扰动了uv,对于雨水来说效果已经足够好了. Shader代码: Shader "Unlit/Rain" { Prop ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) F.猴子排序的期望
题目链接:https://www.nowcoder.com/acm/contest/116/F 题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把卡片扔在空 ...
- python中multiprocessing模块
multiprocess模块那来干嘛的? 答:利用multiprocessing可以在主进程中创建子进程.Threading是多线程,multiprocessing是多进程. #该模块和Threadi ...
- Django【进阶】分页功能Pagination
项目中,我们需要很多非业务逻辑的功能,例如分页功能,而且此类功能移植性很好,可以在不同的项目中使用,所以整理好这些功能会一定程度上提高开发效率,下面是分页功能代码,使用时,可单独放在utils目录 & ...
- 很重要的处理项目url[www]
http://www.xdowns.com/soft/10/57/2013/Soft_113319.html https://github.com/TricksterGuy/Morphan http: ...
- shell 智能获取历史记录功能
vim ~/.inputrc 文件内容: "\e[A": history-search-backward"\e[B": history-search-forwa ...
- 计数排序的实现--适用于元素均较小的seq
今天无聊就打算把所有的排序算法都看一遍... 计数排序的时间复杂度是O(n),在算法导论中,用决策树模型中论证了,比较排序的情况为nlogn的复杂度.而计数排序的时间复杂度小于他的原因就是它不需要进行 ...
- 002 Lock和synchronized的区别和使用
转自 https://www.cnblogs.com/baizhanshi/p/6419268.html 今天看了并发实践这本书的ReentantLock这章,感觉对ReentantLock还是不够熟 ...
- vue做购物车
写一点废话,昨天敲代码找bug,找了好久都没找到,后来一哥们找到他说,找代码的bug就像男女朋友吵架,女问男你错了没,男说错啦,女再问错哪了,男傻眼了不知道错哪.在找代码的过程中一直知道我错啦就是找不 ...
- artdialog自定义多个按钮
在实际运用到的过程中artdialog弹出框下面的按钮不止一个 可以自己定义多个按钮 function view_show(cust_id){$.dialog({ id: 'view_c ...