【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个数告诉她就 ...
随机推荐
- Creating a new dynamic form project, business modeling.
The domain logic is like there are a bunch of objects, as well as a lot of configurations, according ...
- [Unity]用PropertyDrawer自定义struct/class的外观
一般来说,当我们要扩展编辑器时,我们会从Editor类继承,为自己的MonoBehaviour实现不同的外观. 但是如果有一个struct/class,在许多地方被使用,Unity默认的外观又不够好看 ...
- Optimal Milking(POJ2112+二分+Dinic)
题目链接:http://poj.org/problem?id=2112 题目: 题意:有k台挤奶机,c头奶牛,每台挤奶机每天最多生产m的奶,给你每个物品到其他物品的距离(除了物品到自己本省的距离为0外 ...
- Git 常用命令(二)
用 git init 在目录中创建新的 Git 仓库. $ mkdir test $ cd test/ $ git init Initialized empty Git repository in ...
- spring-retry 重试机制
业务场景 应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务逻辑包装给处理方法返回处理结果:第二步拿 ...
- Ribbon/Feign/Zuul retry
原文 https://github.com/spring-cloud/spring-cloud-netflix/issues/1577 I'm using Spring Cloud Camden SR ...
- [Leetcode Week12]Unique Paths
Unique Paths 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths/description/ Description A ...
- 中断中处理延时及一些函数的调用规则(中断调i2c驱动有感)--中断中的延迟delay与printk函数的冲突【转】
转自:http://blog.csdn.net/psvoldemort/article/details/8222371 1,中断处理程序中不能使用有睡眠功能的函数,如ioremap,kmalloc,m ...
- (十七)vmware无法将网络更改为桥接状态
故障现象,导致虚拟机无法正常上网 设备管理器中的驱动设备正常加载,但是注意这两个虚拟网卡是有问题的 将这两个虚拟网卡删除 只剩物理网卡了,重新启动电脑 将虚拟机里的网络设置删除 清空网卡后点击恢复默认 ...
- 单从软件层面来说,Maya 和 Blender 差别在哪?
单从软件层面来说,Maya 和 Blender 差别在哪? https://www.zhihu.com/question/21975571