QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#791879 | #3040. Container | caijianhong | RE | 0ms | 0kb | C++23 | 3.3kb | 2024-11-28 21:36:33 | 2024-11-28 21:36:34 |
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
template <class T> T& chkmin(T& x, const T& y) { return x = min(x, y); }
template <class T> T& chkmax(T& x, const T& y) { return x = max(x, y); }
constexpr int N = 1e3 + 10;
int n, suma[N], sumb[2][N], sub, C, f[N][N], a[N];
vector<int> posa, posb[2];
char buf[N];
vector<pair<int, int>> vec;
void ins(int l, int r) { vec.emplace_back(l, r), swap(a[l], a[r]); }
void movl(int l, int r) {
if (l >= r) return ;
if (l + 1 == r) return ins(l, r);
if (a[l] == a[l + 2]) return movl(l + 2, r), ins(l, l + 2);
ins(l, l + 2), movl(l + 2, r);
}
void movr(int l, int r) {
if (l >= r) return ;
if (l + 1 == r) return ins(l, r);
if (a[r] == a[r - 2]) return movr(l, r - 2), ins(r - 2, r);
ins(r - 2, r), movr(l, r - 2);
}
int main() {
#ifndef LOCAL
#ifndef NF
freopen("machine.in", "r", stdin);
freopen("machine.out", "w", stdout);
#endif
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cin >> n >> C;
cin >> buf;
for (int i = 1; i <= n; i++) {
int w = buf[i - 1] == '2';
suma[i] = suma[i - 1] + w;
a[i] = w;
if (w) posa.push_back(i);
}
cin >> buf;
for (int i = 1; i <= n; i++) {
int w = buf[i - 1] == '2';
sumb[0][i] = sumb[0][i - 1] + (w && i % 2 == 0);
sumb[1][i] = sumb[1][i - 1] + (w && i % 2 == 1);
if (w) posb[i & 1].push_back(i);
}
memset(f, 0x3f, sizeof f);
f[0][0] = 0;
for (int i = 1; i <= (int)posa.size(); i++) {
for (int j = 1; j <= (int)posb[0].size(); j++) if (f[i - 1][j - 1] < 1e9) {
int d = abs(posb[0][j - 1] - posa[i - 1]);
f[i][j] = f[i - 1][j - 1] + max(0, i - j - sumb[1][posb[0][j - 1]]) + (d / 2) * (C + 4) + (d & 1) * (C + 3);
}
for (int j = max(0, i - (int)posb[1].size()); j <= min(i - 1, (int)posb[0].size()); j++) if (f[i - 1][j] < 1e9) {
int d = abs(posb[1][i - j - 1] - posa[i - 1]);
chkmin(f[i][j], f[i - 1][j] + max(0, j - sumb[0][posb[1][i - j - 1]]) + (d / 2) * (C + 4) + (d & 1) * (C + 3));
}
for (int j = 0; j <= (int)posb[0].size(); j++) if (f[i][j] < 1e9) debug("f[%d][%d] = %d\n", i, j, f[i][j]);
}
//cout << -1 << endl;
//cout << f[(int)posa.size()][(int)posb[0].size()] << endl;
vector<pair<int, int>> opt[2];
for (int i = (int)posa.size(), j = (int)posb[0].size(); i > 0; ) {
int d0 = j ? abs(posb[0][j - 1] - posa[i - 1]) : 0;
int lhs = posa[i - 1], rhs;
if (j && f[i][j] == f[i - 1][j - 1] + max(0, i - j - sumb[1][posb[0][j - 1]]) + (d0 / 2) * (C + 4) + (d0 & 1) * (C + 3)) {
rhs = posb[0][j - 1];
opt[lhs <= rhs].emplace_back(lhs, rhs);
--i, --j;
} else {
rhs = posb[1][i - j - 1];
opt[lhs <= rhs].emplace_back(lhs, rhs);
--i;
}
}
sort(opt[0].begin(), opt[0].end(), [&](auto lhs, auto rhs) { return lhs.second < rhs.second; });
sort(opt[1].begin(), opt[1].end(), [&](auto lhs, auto rhs) { return lhs.second > rhs.second; });
for (auto e : opt[0]) movr(e.second, e.first);
for (auto e : opt[1]) movl(e.first, e.second);
for (int i = 1; i <= n; i++) debug("%c", a[i] ? 'B' : 'R');
debug("\n");
cout << vec.size() << endl;
for (auto e : vec) cout << e.first << " " << e.second << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
5 2 11221 21112