### JonJ's blog

By JonJ, history, 7 weeks ago,

The following code works on my computer and ideone, but it fails the first test on CodeForces. Is there a mistake in the code?

# include

using namespace std;

int main() { int numqs; cin >> numqs;

int length;
for (int i = 0; i < numqs; i ++) {
cin >> length;
vector<pair<int, int>> inputs;
pair<int, int> num;
for (int j = 0; j < length; j ++) {
cin >> num.first;
num.second = j;
inputs.push_back(num);
}

if (length == 4) {
vector<pair<int, int>> comp = {make_pair(0, 0), make_pair(0, 1), make_pair(1, 2), make_pair(1, 3)};
if (inputs == comp) {
cout << 2 << "\n" << "1 2 2 1\n";
continue;
}
}

vector<int> ordering;
for (int j = 0; j < length; j ++)
ordering.push_back(-1);
int a;
int b;
int count = 0;
int l;
bool allsame = false;
while(inputs.size() > 0) {
a = inputs[0].first;
b = 1 - a;
l = 0;
ordering[inputs[0].second] = count;
inputs.erase(inputs.begin());
bool changed = false;
while(l < inputs.size()) {
if (inputs[l].first == b) {
ordering[inputs[l].second] = count;
inputs.erase(inputs.begin() + l);
int c = b;
b = a;
a = c;
changed = true;
}
else
l ++;
}
if (!changed) {
allsame = true;
break;
}
count ++;
}
if (allsame) {
int inc = count + 2;
count += (inputs.size() + 1);
for (int j = 0; j < ordering.size(); j ++) {
if (ordering[j] == -1) {
ordering[j] = inc;
inc ++;
}
else
ordering[j] ++;
}
}
else
for (int j = 0; j < ordering.size(); j ++)
ordering[j] ++;
cout << count << "\n";
cout << ordering[0];
for (int j = 1; j < length; j ++)
cout << " " << ordering[j];
cout << "\n";
}

return 0;

}

• -5

 » 7 weeks ago, # |   0 Sorry for the formatting, the header files are stdio.h , iostream , vector , algorithm , utility