QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#401281 | #2555. Two Bullets | nguyentunglam | WA | 1ms | 6544kb | C++17 | 1.7kb | 2024-04-28 13:02:45 | 2024-04-28 13:02:46 |
Judging History
answer
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int N = 1e5 + 10;
int a[N], deg[N];
int n;
vector<int> adj[N];
int32_t main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
if (fopen(task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) if (a[i] > a[j]) {
deg[j]++;
adj[i].push_back(j);
}
queue<int> q, free;
for(int i = 1; i <= n; i++) if (deg[i] == 0) {
if (adj[i].empty()) free.push(i);
else q.push(i);
}
auto del = [&] (int u) {
for(int &v : adj[u]) {
deg[v]--;
if (deg[v] == 0) {
if (adj[v].empty()) free.push(v);
else q.push(v);
}
}
};
vector<pair<int, int> > ans;
while (1) {
if (q.size() >= 2) {
int x = q.front(); q.pop();
int y = q.front(); q.pop();
ans.emplace_back(a[x], a[y]);
del(x);
del(y);
}
else if (q.size() == 1) {
int x = q.front(); q.pop();
int y = free.front(); free.pop();
ans.emplace_back(a[x], a[y]);
del(x);
}
else {
if (free.empty()) break;
int x = free.front(); free.pop();
if (free.empty()) {
ans.emplace_back(a[x], a[x]);
break;
}
int y = free.front(); free.pop();
ans.emplace_back(a[x], a[y]);
}
}
cout << ans.size() << endl;
for(auto &[x, y] : ans) cout << x << " " << y << endl;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5896kb
input:
8 4 3 8 2 1 7 6 5
output:
4 4 8 3 7 2 6 1 5
result:
ok da
Test #2:
score: 0
Accepted
time: 1ms
memory: 6544kb
input:
8 5 6 7 1 2 8 3 4
output:
4 5 6 7 8 1 2 3 4
result:
ok da
Test #3:
score: 0
Accepted
time: 1ms
memory: 5896kb
input:
4 1 2 4 3
output:
2 4 1 2 3
result:
ok da
Test #4:
score: 0
Accepted
time: 0ms
memory: 6160kb
input:
2 1 2
output:
1 1 2
result:
ok da
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 5948kb
input:
2 2 1
output:
1 2 0
result:
wrong answer Integer parameter [name=y] equals to 0, violates the range [1, 3]