Files
python/TangDou/XiTi/1268.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

67 lines
1.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int a[110] = {0};
int n, k, s = INT32_MAX;
vector<pair<int, int>> v1;
//准备返回两个参数值.就是判断数组a中哪个索引号的值最大最大值是多少
void maxA(int &inx, int &num) {
num = INT32_MIN;
for (int i = 1; i <= n; i++) {
if (a[i] > num) {
num = a[i];
inx = i;
}
}
}
//准备返回两个参数值.就是判断数组a中哪个索引号的值最小最小值是多少
void minA(int &inx, int &num) {
num = INT32_MAX;
for (int i = 1; i <= n; i++) {
if (a[i] < num) {
num = a[i];
inx = i;
}
}
}
int main() {
//输入+输出重定向
freopen("../1268.txt", "r", stdin);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
//计算,从最大的中拿出1个放到最小的里面去
int m = 0;
while (m <= k) {
int maxIndex, maxNumber, minIndex, minNumber;
maxA(maxIndex, maxNumber);
minA(minIndex, minNumber);
a[maxIndex]--;
a[minIndex]++;
if (a[maxIndex] - a[minIndex] < s) {
s = a[maxIndex] - a[minIndex];
v1.push_back({maxIndex, minIndex});
} else {
break;
}
m++;
}
cout << s << " " << m << endl;
for (int i = 0; i < v1.size(); ++i) {
cout << v1[i].first << " " << v1[i].second << endl;
}
//关闭文件
fclose(stdin);
return 0;
}