sort

Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40503    Accepted Submission(s): 11823

Problem Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
 
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
 
Output
对每组测试数据按从大到小的顺序输出前m大的数。
 
Sample Input
5 3
3 -35 92 213 -644
 
 
Sample Output
213 92 3

其实就是一排序问题,我想麻烦了。。开了两个数组,一个储存0~正数,一个储存负数。是显得麻烦了,绕了一圈最后也一发A了。

AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAX=; int n,m;
int a[MAX];
int b[MAX]; int main(){
while(~scanf("%d %d",&n,&m)){
int t=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i=;i<n;i++){
scanf("%d",&a[i]);
if(a[i]>=){
b[t++]=a[i];//储存大于等于0的数
a[i]=;
}
}
stable_sort(a,a+n-);//稳定
stable_sort(b,b+t);
if(t+>=m){
for(int i=t-;i>t-m;i--){
printf("%d ",b[i]);
}
printf("%d\n",b[t-m]);
}
else{
printf("%d",b[t-]);
for(int i=t-;i>=;i--){
printf(" %d",b[i]);
m--;
}
for(int i=n-t-;i>=;i++){
if(m==)
break;
if(a[i]!=){
printf(" %d",a[i]);
m--;
}
}
printf("\n");
}
}
return ;
}

HDOj-1425的更多相关文章

  1. Hdoj 1425.sort 题解

    Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含 ...

  2. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  3. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  4. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. hihoCoder 1425 : What a Beautiful Lake(美丽滴湖)

    hihoCoder #1425 : What a Beautiful Lake(美丽滴湖) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 ...

  9. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  10. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

随机推荐

  1. C#文件操作与编程

    一:驱动器System.IO 软盘,优盘,光盘,硬盘 DriveInfo/DriveType DriveInfo:确定有关驱动器的信息:盘符,类型,可用空间 DriveType:确定DriveInfo ...

  2. JavaScript2种构造函数创建对象的模式以及继承的实现

    第一种模式: function Person(){ } Person.prototype.say=function(){ alert('hello'); } var person=new Person ...

  3. OpenCV4Android编译

    http://blog.sina.com.cn/s/blog_602f87700102vdnw.html (2015-04-02 11:10:01) 转载▼     最近的一个项目中,需要自己编译Op ...

  4. eclipse如何查问题?

    问题:从SVN上拉了一个项目,莫名奇妙就报错?看着就很纠结 解决之道:在Window下拉菜单show View 找到Problems 就可以查到具体的原因,我的这个是需要添加一个tomcat服务器

  5. wince c# 创建桌面快捷方式 .

    static void Create() { string PathGPRS = System.IO.Path.GetDirectoryName(System.Reflection.Assembly. ...

  6. Erlang function guards NOTE

    Note: I've compared , and ; in guards to the operators andalso and orelse. They're not exactly the s ...

  7. springboot实战--笔记

    由于这本书看过一遍,所以这里是二次复习,记录的东西比较少,就不分章节了. 共12章,524页,预计时间是18h 第一章 spring基础: 第二章 spring常用配置: bean的Scope:sin ...

  8. mac 权限问题

    终端输入sudo chown -R zjtc /usr/local

  9. 通过css选择器class给元素添加cursor的坑

    笔者在chrome中遇到了奇特的问题,在通过class给元素添加cursor的自定义图片时.出现了"Invald property value"提示,crosshair.help等 ...

  10. appium(11)-java-client

    Welcome to the Appium Java client wiki! This framework is an extension of the Selenium Java client. ...