前m大的数

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19208    Accepted Submission(s):
6563

Problem Description
还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了。

给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
 
Input
输入可能包含多组数据,其中每组数据包括两行:
第一行两个数N和M,

第二行N个数,表示该序列。

Output
对于输入的每组数据,输出M个数,表示结果。输出应当按照从大到小的顺序排列。
 
Sample Input
4 4
1 2 3 4
4 5
5 3 6 4
 
Sample Output
7 6 5 5
11 10 9 9 8
 
Author
Gardon
 
Source
 
Recommend
lcy
 
试题分析:这道题非常简单,用堆模拟就好了,学会手写堆在做以后堆的题目比较方便些
手写堆代码:
#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大的数的更多相关文章

  1. 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 ...

  2. hdu1280 前m大的数(数组下标排序)

    前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  3. 【水题】HDU--1280 前m大的数

    还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就 ...

  4. 前m大的数(hdu1280)

    前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. hdu---(1280)前m大的数(计数排序)

    前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. 找出数组前N大的数

    这个题也是个比较有名的面试题.当然有很多变种. 题目意思基本是:从一个数据量很大的数组里找前N大的元素.不允许排序. 这个题有两个比较好的思路: 思路一:用快速排序的思想,是思想,不是要排序; 思路二 ...

  7. HDU 1280 前m大的数

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory L ...

  8. 输出前n大的数(分治)

    描述:给定一个数组包含n个元素,统计前m大的数并且把这m个数从大到小输 出. 输入: 第一行包含一个整数n,表示数组的大小.n < 100000.第二行包含n个整数,表示数组的元素,整数之间以一 ...

  9. 4J - 前m大的数

    还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就 ...

随机推荐

  1. python学习笔记(八)之元组

    元组:和列表十分相似,可以说是一个受限的列表.最大的限制是,元组不能更改. 创建元组 >>> tuple1 = (123,'asd',(1,2,3)) >>> tu ...

  2. 如何使用webpack打包你的项目

    webpack是前端开发中比较常用的打包工具之一,另外还有gulp,grunt.之前没有涉及过打包这块,这里介绍一下使用webpack打包的流程. Grunt和Gulp的工作方式是:在一个配置文件中, ...

  3. 天气api接口

    python调用天气api接口: http://www.sojson.com/open/api/weather/json.shtml?city=北京 http://www.sojson.com/blo ...

  4. 用selenium 模块控制浏览器

    11.8 用selenium 模块控制浏览器selenium 模块让Python 直接控制浏览器,实际点击链接,填写登录信息,几乎就像是有一个人类用户在与页面交互.与Requests 和Beautif ...

  5. 封装构造函数,用canvas写饼状图和柱状图

    封装构造函数,用canvas写饼状图和柱状图 封装函数 // 场景 function XDLScence( options ) { this.stage = options.stage; //执行场景 ...

  6. leetcode 之Implement strStr()(27)

    字符串的匹配,返回匹配开始的位置,直接用暴力方式求解.为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可 直接停止匹配. char *strStr(char *haystack, ...

  7. django的事务

    在某些时候,你可能会在视图修改两张数据表.并且想让他们同时成功或者同时失败.这就是事务的原子性(atomicity).在django中应该怎么做呢? 详细可以参考官方文档:https://yiyibo ...

  8. jstorm系列-2:入门

    有了基本的概念之后,我们用jstorm来做一点小事情吧 做一个很无聊的事情:给定一个时间戳,输出对应的问候语 规则是:时间戳的十位对应的数字对应不同的时间段,0-2代表早上,3代表中午,4-6代表下午 ...

  9. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  10. QT 中怎样使得控件与 界面等比例变化

    转自:https://github.com/exoticknight/blog-post/blob/master/python-with-Qt-application-development/pyth ...