problem link :- https://codeforces.me/problemset/problem/1684/C In the given editorial of problem C the code is wrong I think. lets assume that there is a matrix where in which after swapping two colums every row is sorted . but the jth colum will be push_back every time we do it for new row so even if the incorrect columns are 2 there the size of vector bad will increase thus giving the wrong answer. Am i correct please tell me it or wrong if wrong please tell me.
Code in the editorial :-
#include <bits/stdc++.h>
using namespace std;
void solve(vector<vector<int>> &a) {
int n = a.size(), m = a[0].size();
vector<int> bad;
for (int i = 0; i < n && bad.empty(); i++) {
vector<int> b = a[i];
sort(b.begin(), b.end());
for (int j = 0; j < m; j++) {
if (a[i][j] != b[j]) bad.push_back(j);
}
}
if ((int)bad.size() == 0) {
cout << 1 << " " << 1 << "\n";
return;
}
if ((int)bad.size() > 2) {
cout << -1 << "\n";
return;
}
for (int i = 0; i < n; i++) {
swap(a[i][bad[0]], a[i][bad[1]]);
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < m; j++) {
if (a[i][j] < a[i][j - 1]) {
cout << -1 << "\n";
return;
}
}
}
cout << bad[0] + 1 << " " << bad[1] + 1 << "\n";
return;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#endif
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
solve(a);
}
return 0;
}
I think this line prevents that:
i < n && bad.empty()
exits the loop when thebad
vector has any elements (a size greater than 0). So the j-th ispush_back
-ed once. I admit I had to look twice at this code because in my implementation there is an explicit if to avoid that case.