1 solutions

  • 0
    @ 2024-12-10 23:39:22

    C :

    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<string.h>
    #include<time.h>
     
    int num[1000005];
     
    int hash( int x )
    {
        return x+ 500000;
    }
     
    int back( int x )
    {
        return x- 500000;
    }
     
    int main(  )
    {
        int N, M, cnt, min, max;
        while( ~scanf( "%d%d", &N, &M ) )
        {
            cnt= 0;
            min= 0x7fffffff;
            max= 0x7fffffff+ 1;
            memset( num, 0, sizeof( num ) );
            while( N-- )
            {
                int c;
                scanf( "%d", &c );
                if( min> hash( c ) )
                {
                    min= hash( c );
                }
                if( max< hash( c ) )
                {
                    max= hash( c );
                }
                num[ hash( c ) ]= 1;
            }
            for( int i= max; i>= min; --i )
            {
                if( num[i] )
                {
                    cnt++;
                    printf( "%d", back( i ) );
                    if( cnt< M )
                    {
                        printf( " " );
                    }
                    if( cnt== M )
                    {
                        break;
                    }
                }
            }
            puts( "" );
        }
    } 
    

    C++ :

    #include <iostream>
    #include <string.h>
    #include <cstdio>
    using namespace std;
    const int N=1000005;
    int num[N];
    int partition(int low,int high){
    	int i=low,j=high,key=num[low];
    	while(i<j){
    	  while(i<j&&num[j]>key) --j;
    	  int t=num[i]; num[i]=num[j];num[j]=t;
    	  while(i<j&&num[i]<key)  ++i;
    	  t=num[i];num[i]=num[j];num[j]=t;
    	}
    	return i;
    }
    void quick_sort(int low,int high){
      if(low<high){
        int x=partition(low,high);
        quick_sort(low,x-1);
        quick_sort(x+1,high);
      }
    }
    int main(){
      int n,m;
      while(~scanf("%d%d",&n,&m)){
        for(int i=0;i<n;++i)
    		scanf("%d",&num[i]);
    	quick_sort(0,n-1);
    	for(int i=n-1;i>n-m;--i)
    		printf("%d ",num[i]);
    	printf("%d",num[n-m]);
    	printf("\n");
      }
      return 0;
    }
    

    Java :

    import java.util.Comparator;
    import java.util.PriorityQueue;
    import java.util.Queue;
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Worshiper
     * Date: 13-10-28
     * Time: 下午10:39
     */
    public class Main{
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNext()) {
                int n = in.nextInt();
                int m = in.nextInt();
                Queue<Integer> queue = new PriorityQueue<Integer>(16, new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
    
                        return o1 > o2 ? -1 : 1;
                    }
                });
                for (int i = 0; i < n; i++) {
                    queue.add(in.nextInt());
                }
    
                for (int i = 0; i < m - 1; i++) {
                    System.out.print(queue.poll() + " ");
                }
    
                System.out.println(queue.poll());
            }
        }
    }
    
    

    Python :

    import sys
    x = []
    for line in sys.stdin:
      x += line.strip('\n').split()
    x = [int(i) for i in x]
    while x:
      n,m = x[0], x[1]
      s = x[2:n+2]
      ss = sorted(s,reverse=True)[:m]
      print ' '.join(str(x) for x in ss)
      x = x[2+n:]
    
    • 1

    Information

    ID
    615
    Time
    1000ms
    Memory
    32MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By