装进肚子<每日一题>

题目链接:登录—专业IT笔试面试备考平台_牛客网牛客网是互联网求职神器,C++、Java、前端、产品、运营技能学习/备考/求职题库,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力icon-default.png?t=M0H8https://ac.nowcoder.com/acm/contest/19850/B

题目: 

 代码详解:

#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:妙蛙种子说方言————————————妙不可言

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CTGU-Yoghurt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值