刷题
题目描述
现有两组服务器A和B,每组有多个算力不同的CPU,其中 A[i] 是 A 组第 i 个CPU的运算能力,B[i] 是 B组 第 i 个CPU的运算能力。
一组服务器的总算力是各CPU的算力之和。
为了让两组服务器的算力相等,允许从每组各选出一个CPU进行一次交换,求两组服务器中,用于交换的CPU的算力,并且要求从A组服务器中选出的CPU,算力尽可能小。
输入描述
第一行输入为L1和L2,以空格分隔,L1表示A组服务器中的CPU数量,L2表示B组服务器中的CPU数量。
第二行输入为A组服务器中各个CPU的算力值,以空格分隔。
第三行输入为B组服务器中各个CPU的算力值,以空格分隔。
1 ≤ L1 ≤ 10000
1 ≤ L2 ≤ 10000
1 ≤ A[i] ≤ 100000
1 ≤ B[i] ≤ 100000
输出描述
对于每组测试数据,输出两个整数,以空格分隔,依次表示A组选出的CPU算力,B组选出的CPU算力。
要求从A组选出的CPU的算力尽可能小。
备注
保证两组服务器的初始总算力不同。
答案肯定存在
用例
输入 | 2 2 1 1 2 2 |
输出 | 1 2 |
说明 | 从A组中选出算力为1的CPU,与B组中算力为2的进行交换,使两组服务器的算力都等于3。 |
输入 | 2 2 1 2 2 3 |
输出 | 1 2 |
说明 | 无 |
输入 | 1 2 2 1 3 |
输出 | 2 3 |
说明 | 无 |
输入 | 3 2 1 2 5 2 4 |
输出 | 5 4 |
说明 | 无 |
提示:以下是本篇文章正文内容,下面案例可供参考
一、题目解析
假设A组服务器算力之和为sumA,B组服务器算力之和为sumB,将A组的a和B组的b交换后,A组算力之和等于B组算力之和,则可得公式如下
其中 sumA, sumB 是已知的,因此,我们可以遍历A组所有元素a,计算出
b= a - (sumA - sumB)/2,看B组中是否存在对应b,若存在,则a b就是题解。
二、算法源码
1. C++算法源码
代码如下(示例):
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <utility>
#include <sstream>
#include <numeric>
#include <unordered_set>
#include <climits>
vector<int> strToVector(string str)
{
string target = " ";
vector<int> res;
string::size_type pos = str.find(target);
while (pos != string::npos)
{
res.push_back(stoi(str.substr(0, pos)));
cout << "1" <<endl;
str.erase(str.begin(), str.begin() + pos + 1);
pos = str.find(target);
cout << "2" <<endl;
}
cout << "3" <<endl;
res.push_back(stoi(str));
cout << "4" <<endl;
return res;
}
vector<int> strToVector1(const string& str)
{
std::istringstream iss(str);
string temp;
char target = ' ';
vector<int> res;
while (getline(iss, temp, target))
{
try
{
res.push_back(stoi(temp));
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
continue;
}
}
return res;
}
vector<int> strToVector3(const string& str)
{
std::istringstream iss(str);
string temp;
vector<int> res;
while (iss >> temp)
{
try
{
res.push_back(stoi(temp));
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
continue;
}
}
return res;
}
int getResult()
{
string str;
getline(cin, str);
cout << str <<endl;
vector<int> arr = strToVector(str);
string strA;
getline(cin, strA);
vector<int> arrA = strToVector1(strA);
int sumA = accumulate(arrA.begin(), arrA.end(), 0);
string strB;
getline(cin, strB);
vector<int> arrB = strToVector1(strB);
unordered_set<int> setB(arrB.begin(),arrB.end());
int sumB = accumulate(arrB.begin(), arrB.end(), 0);
int diff = (sumA - sumB)/2;
int minA = INT_MAX;
string res;
for (auto a : arrA)
{
int b = a - diff;
cout << "b=" << b <<endl;
if (setB.find(b) != setB.end() && a < minA)
{
minA = a;
res = std::to_string(a) + " " + std::to_string(b);
}
}
cout << "result=" << res <<endl;
}
int main()
{
getResult();
return 0;
}
2. Python算法源码
代码如下(示例):
import sys
while True:
try:
l1, l2 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sumA = sum(A)
sumB = 0
setB = set()
for b in B:
sumB += b
setB.add(b)
# 由于本题必然存在解,因此sumA-sumB的结果肯定可以整除2,如果不能整除则half_diff为小数,
# 而half_diff = a - b,其中a,b都是整数,因此不可能存在half_diff是小数的情况
half_diff = (sumA - sumB) // 2
# 记录用于交换的最小的a
minA = sys.maxsize
# 记录题解
ans = ""
for a in A:
b = a - half_diff
if b in setB:
if a < minA:
minA = a
ans = f"{a} {b}"
print(ans)
except:
break
2. C算法源码
代码如下(示例):
#include <stdio.h>
#include <limits.h>
#include <string.h>
#define MAX_SIZE 10000
#define MAX_VALUE 100000
int main() {
// 可能有多组测试数据
while(1) {
int l1, l2;
int res = scanf("%d %d", &l1, &l2);
if(res != 2) {
break;
}
int A[MAX_SIZE];
int A_size = 0;
int sumA = 0;
while (scanf("%d", &A[A_size])) {
sumA += A[A_size];
A_size++;
if(getchar() != ' ') break;
}
int B[MAX_SIZE];
int B_size = 0;
int sumB = 0;
int setB[MAX_VALUE + 1] = {0};
while (scanf("%d", &B[B_size])) {
sumB += B[B_size];
setB[B[B_size]] = 1;
B_size++;
if(getchar() != ' ') break;
}
// 由于本题必然存在解,因此sumA-sumB的结果肯定可以整除2,如果不能整除则half_diff为小数,
// 而half_diff = a - b,其中a,b都是整数,因此不可能存在half_diff是小数的情况
int half_diff = (sumA - sumB) / 2;
int minA = INT_MAX;
char ans[15];
for(int i=0; i<A_size; i++) {
int a = A[i];
int b = a - half_diff;
if(b > 0 && setB[b] && a < minA) {
minA = a;
char tmp[15];
sprintf(tmp, "%d %d", a, b);
strcpy(ans, tmp);
}
}
puts(ans);
}
return 0;
}
总结
c++算法使用三种分割字符串的方法,第一种当stoi()转换出错时无法处理;第二、三中使用std::istringstream类,需要新加头文件#include <sstream>。