【题解】P9741 「KDOI-06-J」翻转与反转
题目
P9741 「KDOI-06-J」翻转与反转
思路
我们先找个规律(因为直接打暴力就会
经过上面的举例,我们可以发现,
当
for (int i = n; i >= 1; i -= 2) {
a[i] = a[i] == 1 ? 0 : 1;
cout << a[i] << " ";
}
for (int i = 2; i <= n; i += 2) {
cout << a[i] << " ";
}
当
for (int i = n; i >= 2; i -= 2) {
a[i] = a[i] == 1 ? 0 : 1;
cout << a[i] << " ";
}
for (int i = 1; i <= n; i += 2) {
cout << a[i] << " ";
}
这时,时间复杂度是
code
#include<bits/stdc++.h>
using namespace std;
const int _ = 2 * 1e6 + 5;
int n, a[_];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (n % 2 == 1) {
for (int i = n; i >= 1; i -= 2) {
a[i] = a[i] == 1 ? 0 : 1;
cout << a[i] << " ";
}
for (int i = 2; i <= n; i += 2) {
cout << a[i] << " ";
}
} else {
for (int i = n; i >= 2; i -= 2) {
a[i] = a[i] == 1 ? 0 : 1;
cout << a[i] << " ";
}
for (int i = 1; i <= n; i += 2) {
cout << a[i] << " ";
}
}
}