题解
2023-08-27 09:27:14
发布于:广东
8阅读
0回复
0点赞
#include <iostream>
using namespace std;
const int MAX_N = 110;
const int MAX_X = 1010;
int N, cows[MAX_N], cowsPassingTo[MAX_N];
int passToCow(int cow) {
int leftCow = -1;
int rightCow = -1;
int leftRange = MAX_X;
int rightRange = MAX_X;
for (int i = 0; i < N; i++) {
if (cows[i] < cows[cow] && cows[cow] - cows[i] < leftRange) {
leftCow = i;
leftRange = cows[cow] - cows[i];
}
if (cows[i] > cows[cow] && cows[i] - cows[cow] < rightRange) {
rightCow = i;
rightRange = cows[i] - cows[cow];
}
}
if (leftRange <= rightRange) { return leftCow; }
return rightCow;
}
int main() {
cin >> N;
int ballsNeeded = 0;
for (int i = 0; i < N; i++) { cin >> cows[i]; }
for (int i = 0; i < N; i++) { cowsPassingTo[passToCow(i)]++; }
for (int i = 0; i < N; i++) {
if (cowsPassingTo[i] == 0) { ballsNeeded++; }
if (i < passToCow(i) && passToCow(passToCow(i)) == i &&
cowsPassingTo[i] == 1 && cowsPassingTo[passToCow(i)] == 1) {
ballsNeeded++;
}
}
cout << ballsNeeded << "\n";
}
内存击败法兰西
这里空空如也
有帮助,赞一个