zoukankan      html  css  js  c++  java
  • hdu1280前m大的数

    坑爹啊!弄了半个小时,数组越界啊!

    前m大的数

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


    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
     
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<string.h>
     4 # include<algorithm>
     5 using namespace std;
     6 int cmp(const int a,const int b)
     7 {
     8     return a>b;
     9 }
    10 int a[3005],hash[3000*3000/2];
    11 int main()
    12 {
    13 
    14     int n,m;
    15     while(cin>>n>>m)
    16     {
    17         for(int i = 0;i < n;i++)
    18         {
    19             cin>>a[i];
    20         }
    21         int k = 0;
    22         for(int j = 0;j < n -1;j++)
    23         {
    24             for(int v = j+1;v < n;v++)
    25             {
    26                 hash[k++] = a[j]+a[v];
    27             }
    28         }
    29         sort(hash,hash+(n*(n-1))/2,cmp);
    30         for(int i = 0;i < m;i++)
    31         {
    32             if(!i)
    33             cout<<hash[i];
    34             else
    35             cout<<" "<<hash[i];
    36         }
    37         cout<<endl;
    38     }
    39 }
    View Code
     
  • 相关阅读:
    passenger中的设置ssl
    memcached在rails中的使用介绍
    redis相关
    复制linode镜像
    memcached启动 安装
    A Short History of Character Sets
    du h df h 磁盘空间满处理办法
    硬盘2T 现在也才不到1000了,变得感觉太便宜了
    最近爬虫心得
    20110205网站更新部署过程记录
  • 原文地址:https://www.cnblogs.com/sxmcACM/p/4004371.html
Copyright © 2011-2022 走看看