'commit'
This commit is contained in:
66
TangDou/XiTi/1268.cpp
Normal file
66
TangDou/XiTi/1268.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user