题目:https://codeforces.com/contest/1618/problem/D
You are given an array a of n integers, and another integer k such that 2k≤n.
You have to perform exactly k operations with this array. In one operation, you have to choose two elements of the array (let them be ai and aj; they can be equal or different, but their positions in the array must not be the same), remove them from the array, and add ⌊aiaj⌋ to your score, where ⌊xy⌋ is the maximum integer not exceeding xy.
Initially, your score is 0. After you perform exactly k operations, you add all the remaining elements of the array to the score.
Calculate the minimum possible score you can get.
Input
The first line of the input contains one integer t (1≤t≤500) — the number of test cases.
Each test case consists of two lines. The first line contains two integers n and k (1≤n≤100; 0≤k≤⌊n2⌋).
The second line contains n integers a1,a2,…,an (1≤ai≤2⋅105).
Output
Print one integer — the minimum possible score you can get.
Example
inputCopy
5
7 3
1 1 1 2 1 3 1
5 1
5 5 5 5 5
4 2
1 3 3 7
2 0
4 2
9 2
1 10 10 1 10 2 7 10 3
outputCopy
2
16
0
6
16
Note
Let’s consider the example test.
In the first test case, one way to obtain a score of 2 is the following one:
choose a7=1 and a4=2 for the operation; the score becomes 0+⌊12⌋=0, the array becomes [1,1,1,1,3];
choose a1=1 and a5=3 for the operation; the score becomes 0+⌊13⌋=0, the array becomes [1,1,1];
choose a1=1 and a2=1 for the operation; the score becomes 0+⌊11⌋=1, the array becomes [1];
add the remaining element 1 to the score, so the resulting score is 2.
In the second test case, no matter which operations you choose, the resulting score is 16.
In the third test case, one way to obtain a score of 0 is the following one:
choose a1=1 and a2=3 for the operation; the score becomes 0+⌊13⌋=0, the array becomes [3,7];
choose a1=3 and a2=7 for the operation; the score becomes 0+⌊37⌋=0, the array becomes empty;
the array is empty, so the score doesn’t change anymore.
In the fourth test case, no operations can be performed, so the score is the sum of the elements of the array: 4+2=6.
解题思路:
根据题目要求,要求一个最小值,那么首先可以想到分子小于分母得到的结果为0是最小的,那么题目要求我们选k对数进行相除的结果进行累加,还要加上剩下没有选过的数,那么就可以想到尽量把最大的数给他除掉,所以就能想到从小到大排序选择最后2*k个数,具体看代码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
int n,k;
cin >> n >> k;
int a;
vector<int> vt;
vt.push_back(-1);//占下标0的位置
for(int i = 1;i<=n;i++){
cin >> a;
vt.push_back(a);
}
sort(vt.begin(),vt.end());
int ans = 0;
for(int i = 1;i<=k;i++){
ans = ans + vt[n-2*k+i] / vt[n-k+i];
}
for(int i = 1;i<=n-2*k;i++){
ans = ans + vt[i];
}
cout << ans << endl;
}
return 0;
}