题目:
代码详解:
#include<stdio.h>
#include<iostream>
using namespace std;
#include<algorithm>
const int N = (int)1e5 + 3;
typedef long long ll;
int a[N], b[N], c[N];
int main()
{
int n, k;
cin >> n >> k;
ll sum = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++)
{
scanf("%d", &b[i]);
sum += b[i];//都晚上在晚上吃巧克力的甜蜜值总和
c[i] = a[i] - b[i];//早上与晚上甜蜜值的差
}
sort(c, c + n, greater<int>());//将c中的甜蜜值差从对大到小排序
ll arr = 0;
for (int i = 0; i < k; i++)
{
arr += c[i];//k个早上吃所得甜蜜值的和(最大和)
}
cout << arr + sum;//早上和晚上的甜蜜值相加
return 0;
}
总结:该题的思路是设置一组A为总和,将另外一组B相减得到所有的差值集合C组,将A组和B组通过差联系起来,再通过sort排序把差值从大到小排列,题目所求的甜蜜值的最大值,就可以通过A组所有的数的总和加上C组来达到两组相加的效果。
Summary: the idea is to set one group A as the sum, subtract the other group B to get all the difference sets, Group C, and Link Group A and Group B by difference, then sort sort the difference from large to small, the topic of the maximum sweet value, you can through a group of all the sum of the number plus C group to achieve the effect of adding two groups.
PS:妙蛙种子说方言————————————妙不可言