[HDU] [1280] Top m Largest Numbers

Problem Description

Loading...

Problem Analysis

sort really is so useful

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[3200],b[4500000];
int main() {
	int N,M;
	int i,j,k;
	while(scanf("%d %d",&N,&M)!=EOF) {
		for(i=0; i<N; i++) {
			scanf("%d",&a[i]);
		}
		for(i=k=0; i<N; i++) {
			for(j=i+1; j<N; j++) {
				b[k++]=a[i]+a[j];
			}
		}
		sort(b,b+k);
		for(i=k-1; i>k-1-M; i--) {
			printf("%d",b[i]);
			if(i!=k-M)
				printf(" ");
			else
				printf("\n");
		}
	}
	return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...