QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#466884 | #7678. The Game | le6666 | RE | 4ms | 5920kb | C++14 | 2.0kb | 2024-07-08 11:35:19 | 2024-07-08 11:35:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 3e5 + 5;
int t, n, m, a[MAXN], b[MAXN], ans[MAXN];
void check() {
multiset<int> u, v;
for (int i = 1; i <= n; i++) u.insert(a[i]);
for (int i = 1; i <= m; i++) v.insert(b[i]);
for (int i = 1; i <= n - m; i++) {
u.erase(u.find(ans[i]));
u.insert(ans[i] + 1);
u.erase(u.begin());
}
assert(u == v);
}
bool sovle() {
ll suma = 0, sumb = 0;
for (int i = 1; i <= m; i++) {
if (a[i] > b[i]) return false;
suma += a[i], sumb += b[i];
}
if (sumb - suma > n - m) return false;
multiset<int> s, v;
for (int i = 1; i <= m; i++) v.insert(a[i]);
for (int i = 1; i <= n; i++) s.insert(a[i]);
int num = (n - m) - (sumb - suma);
for (int i = 1; i <= n - m; i++) {
if (num > 0) {
int x = *s.begin(); s.erase(s.begin()), s.insert(x + 1), s.erase(s.begin());
ans[i] = x;
if (x < *v.begin()) num--;
else v.insert(*v.begin() + 1), v.erase(v.begin());
}
else {
for (int j = m; j >= 1; j--) a[j] = *v.begin(), v.erase(v.begin());
for (int j = 1; j <= m; j++) {
if (a[j] > b[j]) return false;
for (int k = a[j]; k < b[j]; k++) ans[i++] = k;
}
}
}
if (num > 0) return false;
check();
printf("%d\n", n - m);
for (int i = 1; i <= n - m; i++) printf("%d ", ans[i]);
printf("\n");
// printf("1\n");
return true;
}
int main() {
// freopen("B.in", "r", stdin);
// freopen("gcd.out", "w", stdout);
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
sort(a + 1, a + 1 + n, [](int x, int y) { return x > y; });
sort(b + 1, b + 1 + m, [](int x, int y) { return x > y; });
if (!sovle()) printf("-1\n");
}
return 0;
}
/*
6
5 3
1 2 2 3 3
2 3 4
4 2
1 2 2 4
2 4
5 2
2 3 3 4 4
5 5
6 1
1 1 1 1 1 1
4
4 2
1 1 1 2
2 2
4 1
1 1 1 1
2
1000
20 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
5
20 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4
5 5
1 2 3 4 5
5 4 2 1 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4076kb
input:
6 5 3 1 2 2 3 3 2 3 4 4 2 1 2 2 4 2 4 5 2 2 3 3 4 4 5 5 6 1 1 1 1 1 1 1 4 4 2 1 1 1 2 2 2 4 1 1 1 1 1 2
output:
2 1 3 -1 3 2 4 4 5 1 1 1 2 3 2 1 1 -1
result:
ok ok (6 test cases)
Test #2:
score: 0
Accepted
time: 4ms
memory: 5920kb
input:
7056 4 3 1 1 1 1 1 1 1 4 3 1 1 1 1 1 1 2 4 3 1 1 1 1 1 1 3 4 3 1 1 1 1 1 1 4 4 3 1 1 1 1 1 1 5 4 3 1 1 1 1 1 1 6 4 3 1 1 1 1 1 2 2 4 3 1 1 1 1 1 2 3 4 3 1 1 1 1 1 2 4 4 3 1 1 1 1 1 2 5 4 3 1 1 1 1 1 2 6 4 3 1 1 1 1 1 3 3 4 3 1 1 1 1 1 3 4 4 3 1 1 1 1 1 3 5 4 3 1 1 1 1 1 3 6 4 3 1 1 1 1 1 4 4 4 3 1 1...
output:
-1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 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