Link: USACO
Hi,
I was working on the Problem 2 of December 2021 Bronze, and I was wondering why my code is not passing all test cases.
It is only passing 9/10.
This is my code:
#include <bits/stdc++.h>
using namespace std;
bool checkZeros(int array[], int size) {
for (int i = 0; i < size; i++) {
if (array[i] != 0) {
return false;
}
}
return true;
}
int main() {
int n = 0;
cin >> n;
char mode;
int p[n];
int t[n];
int d[n];
int target[n] = { 0 };
vector<int> indexes;
vector<char> modes;
int moves = 0;
for (int i = 0; i < n; i++) {
cin >> p[i];
}
for (int i = 0; i < n; i++) {
cin >> t[i];
}
for (int i = 0; i < n; i++) {
d[i] = p[i] - t[i];
}
while (true) {
for (int i = 0; i < n; i++) {
if (d[i] > 0) {
mode = '-';
d[i]--;
indexes.push_back(i);
modes.push_back(mode);
}
else if (d[i] < 0) {
mode = '+';
d[i]++;
indexes.push_back(i);
modes.push_back(mode);
}
else {
continue;
}
}
if (checkZeros(d, n)) {
break;
}
}
for (int i = 0; i < indexes.size(); i++) {
if (indexes[i] + 1 == indexes[i + 1] && modes[i] == modes[i + 1]) {
continue;
}
else {
moves++;
}
}
cout << moves;
}
I am really confused why. Please let me know.
Thanks!