2020 cattle off winter training camp algorithm base 6 A pair

https://ac.nowcoder.com/acm/contest/3007/A
Title Description

There are positive integers A and B, each collection, there are number N, you have to build one mapping between them

Each pair of numbers can be added and the N, you have to do is maximize the K-large and

All digital inputs does not exceed 1e8 1≤K≤N≤100,000

Thinking

~ Greedy   for the thing I initially thought up 

The first section is a large K (N - K + 1) is small, so that it is smaller than as small as possible, to give the latter a small number, and then adding the rest of the staggered, above the maximum + minimum remaining below, from select a minimum inside is the answer

#include <bits/stdc++.h>
using namespace std;
int n, k;
const int N = 1e5 + 10;
int a[N], b[N];
int main()
{
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++){
		scanf("%d", a + i);
	}
	for (int i = 1; i <= n; i++){
		scanf("%d", b + i);
	}
	k = n - k + 1;
	sort(a + 1, a + 1 + n);
	sort(b + 1, b + 1 + n);
	vector<int> vec;
	for (int i = k, j = n; i <= n; i++, j--){
		vec.push_back(a[i] + b[j]);
	}
	sort(vec.begin(), vec.end());
	printf("%d\n", *vec.begin());
	return 0;
}

 

Published 204 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/104331855