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): 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
    ",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
    ",ans[1]); 
        }
        return 0;
    }
    

      

     

      

  • 相关阅读:
    Delphi 获取不重复随机数《LceMeaning》
    轻松一下
    MS SQL字段类型详解《转》
    Go语言优势与劣势
    go语言特点
    初始go语言
    django 短链接改成长连接
    extjs [1]
    Supervisor安装与配置
    InfluxDB命令使用
  • 原文地址:https://www.cnblogs.com/wxjor/p/7223054.html
Copyright © 2011-2022 走看看