珠海做网站报价,搭配网站开发的开题报告,兰州市政建设集团网站,邯郸装修网站建设文章目录1. 题目2. 解题1. 题目
来源#xff1a;https://tianchi.aliyun.com/oj/210874425247820050/215397455965131520
今天有N个面试者需要面试#xff0c;公司安排了两个面试的城市A和B#xff0c;每一个面试者都有到A城市的开销costA和到B城市的开销costB。
公司需要…
文章目录1. 题目2. 解题1. 题目
来源https://tianchi.aliyun.com/oj/210874425247820050/215397455965131520
今天有N个面试者需要面试公司安排了两个面试的城市A和B每一个面试者都有到A城市的开销costA和到B城市的开销costB。
公司需要将面试者均分成两拨使得 total cost最小。
N是偶数
2N1e5
答案确保在int范围内
1costA,costB 1e6说明 It is required that the number to go to A is equal to the number to go to B
示例
输入: cost [[5,4],[3,6],[1,8],[3,9]]
输出: 14
说明: 第一个和第二个人去B城市剩下的去A城市2. 解题
差值小的去A城市
class Solution {
public:/*** param cost: The cost of each interviewer* return: The total cost of all the interviewers.*/int TotalCost(vectorvectorint cost) {// write your code heresort(cost.begin(), cost.end(),[](auto a, auto b){return a[0]-a[1] b[0]-b[1];// 差值小的去A城市});int sum 0;for(int i 0; i cost.size(); i){if(i cost.size()/2)sum cost[i][0];// 差值小的去A城市elsesum cost[i][1];}return sum;}
};603ms C 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步