QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#558768 | #8940. Piggy Sort | ucup-team3519 | RE | 0ms | 3540kb | C++20 | 2.5kb | 2024-09-11 18:27:58 | 2024-09-11 18:27:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define V vector
#define pb push_back
#define all1(x) (x).begin() + 1, (x).end()
#define all0(x) (x).begin(), (x).end()
typedef long long LL;
typedef pair<int, int> pi;
void solve() {
int n; cin >> n;
int m; cin >> m;
V<V<int>> v(m + 1);
for(int i = 1; i <= m; i++) {
v[i].resize(n + 1);
for(int j = 1; j <= n; j++) cin >> v[i][j];
}
V<LL> sumini(m + 1);
V<int> ord(m + 1);
iota(all1(ord), 1);
for(int i = 1; i <= m; i++) sumini[i] = accumulate(all1(v[i]), 0LL);
sort(all1(ord), [&](int x, int y) {
return sumini[x] < sumini[y];
});
map<int, int> id;
for(int i = 1; i <= n; i++) {
id[v[ord[1]][i]] = i;
}
V<multiset<int>> vst(m + 1);
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) vst[i].insert(v[ord[i]][j]);
}
V<LL> sum(m + 1);
for(int i = 1; i <= m; i++) {
sum[i] = accumulate(all1(v[ord[i]]), 0LL);
}
auto get_mx = [&](multiset<int> & st) {
return *st.rbegin();
};
auto pos = [&](int p1, int x, int y, int pos) -> LL {
if(sum[2] == sum[1]) {
if(x != y) return -1;
return x;
}
LL tmp = 1LL * (y - x) * (sum[pos] - sum[p1]);
if(tmp % (sum[p1 + 1] - sum[p1])) return -1;
return tmp / (sum[p1 + 1] - sum[p1]) + x;
};
V<int> ans(n + 1);
for(int i = 1; i <= n; i++) {
int x = 1, y = 2;
for(int j = 2; j < m; j++) {
int p = pos(j - 1, get_mx(vst[j - 1]), get_mx(vst[j]), j + 1);
if(p == get_mx(vst[j + 1])) {
x = j, y = j + 1;
break;
}
}
int vx = get_mx(vst[x]), vy = get_mx(vst[y]);
int a = pos(x, get_mx(vst[x]), get_mx(vst[y]), 1);
int b = pos(x, get_mx(vst[x]), get_mx(vst[y]), 2);
ans[id[a]] = b - a;
for(int i = 1; i <= m; i++) {
vst[i].erase(vst[i].find(pos(x, vx, vy, i)));
}
}
V<int> ansord(n + 1);
iota(all1(ansord), 1);
stable_sort(all1(ansord), [&](int x, int y) -> bool {
return ans[x] < ans[y];
});
for(int i = 1; i <= n; i++) ans[ansord[i]] = i;
for(int i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t; cin >> t;
while(t--) solve();
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3540kb
input:
3 2 4 1 2 3 4 5 6 7 8 1 2 1 1 3 4 1 2 3 6 9 9 10 15 17 12 18 21
output:
1 2 1 3 1 2
result:
ok 3 lines
Test #2:
score: -100
Runtime Error
input:
41 1 2 -19 9531 2 3 11 13 3175 4759 2211 3313 10 19 -54 -25 -19 -18 -1 3 61 63 85 88 -54 753 863 2397 3111 4649 4671 4756 5507 7762 -54 369 479 1245 1575 2345 2367 2452 2819 3922 -54 553 663 1797 2311 3449 3471 3556 4107 5762 -54 87 197 399 447 653 675 760 845 1102 -54 320 430 1098 1379 2051 2073 21...
output:
1 1 2 1 2 6 10 5 7 9 4 3 8 8 7 5 9 2 1 6 3 4 1 6 5 9 8 2 3 10 4 7 3 5 10 6 7 4 9 8 2 1 4 3 1 8 2 5 6 7 3 1 5 2 6 9 8 7 4 10 2 3 1 4 3 2 9 1 4 6 7 5 8 1 5 2 6 7 3 4 8 9 9 8 5 7 2 3 4 6 1 1 2 4 8 5 7 10 6 9 3 7 1 3 2 9 8 10 4 5 6 7 8 1 2 5 6 3 9 4 8 1 2 7 6 9 3 5 4 8 1 2 4 5 10 9 3 6 7...