QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#552350 | #8940. Piggy Sort | Max_s_xaM | RE | 56ms | 10100kb | C++17 | 4.9kb | 2024-09-07 22:11:47 | 2024-09-07 22:11:48 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <numeric>
#include <random>
#include <ctime>
#include <chrono>
#include <cassert>
typedef long long ll;
typedef double lf;
// #define DEBUG 1
struct IO
{
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
#endif
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
#define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
template <typename T>
void Read(T &x)
{
#if DEBUG
std::cin >> x;
#else
bool sign = 0; char ch = gc(); x = 0;
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
if (sign) x = -x;
#endif
}
void Read(char *s)
{
#if DEBUG
std::cin >> s;
#else
char ch = gc();
for (; blank(ch); ch = gc());
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
#endif
}
void Read(char &c) {for (c = gc(); blank(c); c = gc());}
void Push(const char &c)
{
#if DEBUG
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <typename T>
void Write(T x)
{
if (x < 0) x = -x, Push('-');
static T sta[35];
int top = 0;
do sta[top++] = x % 10, x /= 10; while (x);
while (top) Push(sta[--top] ^ 48);
}
template <typename T>
void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)
using namespace std;
const int MAXN = 510;
int n, m;
ll f[MAXN][MAXN], a[MAXN][MAXN], b[MAXN], tim[MAXN];
ll dx[MAXN], dy[MAXN];
int vis[MAXN][MAXN];
int id[MAXN], ans[MAXN];
int main()
{
// freopen("I.in", "r", stdin);
// freopen("I.out", "w", stdout);
#if DEBUG
#else
ios::sync_with_stdio(0), cin.tie(0);
#endif
int T, Case = 0, oT;
Read(T), oT = T;
while (T--)
{
Case++;
Read(n), Read(m);
for (int i = 1; i <= m; i++) b[i] = tim[i] = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
Read(a[i][j]), b[i] += a[i][j];
// if (Case == 11 && oT == 46)
// {
// for (int i = 9; i <= m; i++, cout << '\n')
// for (int j = 1; j <= n; j++)
// cout << a[i][j] << ' ';
// }
iota(id + 1, id + m + 1, 1);
sort(id + 1, id + m + 1, [&](int u, int v) { return b[u] < b[v]; });
for (int i = 1; i <= m; i++)
{
tim[i] = b[id[i]];
for (int j = 1; j <= n; j++)
f[i][j] = a[id[i]][j];
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
vis[i][j] = 0;
for (int i = 2; i <= m; i++) tim[i] -= tim[1];
tim[1] = 0;
// for (int i = 1; i <= m; i++) cout << tim[i] << ' '; cout << "\n";
// for (int i = 1; i <= m; i++, cout << "\n")
// for (int j = 1; j <= n; j++)
// cout << f[i][j] << ' ';
for (int _ = 1; _ <= n; _++)
{
ll x = -1, y;
for (int i = 2; i < m && x == -1; i++)
for (int j = 1; j <= n; j++)
{
if (vis[i][j] || vis[i + 1][j]) continue;
ll det = (f[i + 1][j] - f[i][j]) * tim[i] - (tim[i + 1] - tim[i]) * (f[i][j] - f[1][_]);
if (det == 0) { x = tim[i + 1], y = f[i + 1][j]; break; }
}
if (x == -1) dx[_] = x = tim[2], y = f[2][_], dy[_] = y - f[1][_];
else dx[_] = x, dy[_] = y - f[1][_];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
{
if (vis[i][j]) continue;
ll det = (f[i][j] - f[1][_]) * dx[_] - tim[i] * dy[_];
if (det == 0) { vis[i][j] = _; break; }
}
int cnt = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
cnt += (vis[i][j] == _);
// cout << cnt << '\n';
assert(cnt == m);
}
// for (int i = 1; i <= m; i++, cout << "\n")
// for (int j = 1; j <= n; j++)
// cout << vis[i][j] << ' ';
// cout << "\n";
// for (int i = 1; i <= n; i++) cout << dx[i] << ' ' << dy[i] << '\n';
iota(id + 1, id + n + 1, 1);
sort(id + 1, id + n + 1, [&](int u, int v) { ll det = dx[u] * dy[v] - dy[u] * dx[v]; return det == 0 ? u < v : det > 0; });
for (int i = 1; i <= n; i++) ans[id[i]] = i;
// if (oT == 46) continue;
for (int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9784kb
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: 0
Accepted
time: 2ms
memory: 9808kb
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...
result:
ok 41 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 7804kb
input:
46 8 12 -50 -35 -20 -10 -9 4 13 91 -24 30 32 143 146 147 173 221 -44 -8 10 13 26 27 61 103 -46 -12 -3 8 14 15 45 99 -22 32 36 147 158 159 189 237 -47 -14 -11 7 8 9 37 97 -31 18 23 104 105 117 129 165 -45 -10 5 9 20 21 53 101 -36 8 18 74 75 77 119 125 -30 20 24 110 111 125 131 173 -48 -19 -16 2 3 6 2...
output:
1 7 3 5 6 2 8 4 1 2 3 3 6 1 5 10 8 7 2 9 4 2 5 3 6 9 10 8 7 1 4 1 4 5 2 6 3 7 8 6 4 5 7 9 2 3 1 6 7 2 8 9 1 10 3 5 4 4 5 1 3 2 1 2 1 2 4 5 7 6 3 8 9 6 3 4 9 7 1 2 8 10 5 1 6 7 8 4 9 5 3 2 1 4 2 6 5 8 3 7 2 1 5 1 7 6 3 2 4 7 3 8 4 1 2 9 5 6 10 6 1 2 5 3 4 2 1 3 4 7 8 5 9 2 1 4 3 6 ...
result:
ok 46 lines
Test #4:
score: 0
Accepted
time: 1ms
memory: 7808kb
input:
48 3 4 -4952 -1539 836 -4294 5909 12778 -4811 57 3395 -4529 3249 8513 8 11 -9107 -1143 1324 3936 4088 4381 7658 9440 -2753 531 6032 14986 18097 18264 20240 22022 -5224 -120 5276 9673 12692 12763 15347 17129 -2047 717 6248 16504 19621 19856 21638 23420 -6283 -399 4952 7396 10304 10477 13250 15032 -48...
output:
1 2 3 3 1 6 8 2 7 4 5 8 4 6 1 2 9 10 5 3 7 1 2 3 6 1 10 7 5 3 9 4 8 2 1 2 8 3 2 7 10 6 1 9 5 4 9 5 2 7 6 1 8 4 3 6 4 3 2 7 5 1 1 4 2 3 1 3 8 9 6 7 4 1 2 10 5 5 1 6 4 2 3 2 1 9 6 4 5 3 8 7 3 5 6 7 9 2 4 8 1 4 2 1 3 5 6 4 5 6 3 7 1 2 1 9 7 10 1 8 5 3 6 2 4 8 3 6 9 2 5 4 7 1 4 3 9 5...
result:
ok 48 lines
Test #5:
score: 0
Accepted
time: 2ms
memory: 9900kb
input:
40 10 20 -4289879 -3663596 -3442064 -3379220 -670906 -329052 1547135 1640345 2662172 3577480 -4280827 -3609576 -3374758 -3321039 -598417 -319197 1583489 1685532 2700424 3645662 -4276115 -3581456 -3339722 -3290753 -560683 -314067 1602413 1709054 2720336 3681154 -4271279 -3552596 -3303764 -3259670 -52...
output:
1 6 8 7 10 2 3 5 4 9 2 3 1 9 8 3 6 4 2 10 1 7 5 2 1 8 5 3 4 10 7 6 9 7 2 6 1 9 5 8 3 4 2 6 3 9 7 1 4 5 8 3 6 7 5 1 2 8 4 3 4 10 9 8 7 6 2 5 1 3 8 2 6 1 4 5 7 4 2 5 7 1 6 3 1 8 3 10 9 7 4 6 5 2 1 3 5 4 2 8 6 7 3 1 4 5 10 2 9 1 2 3 5 10 2 7 6 4 1 8 3 9 4 10 7 6 8 9 5 1 2 3 5 7 10 1 4 9...
result:
ok 40 lines
Test #6:
score: 0
Accepted
time: 2ms
memory: 9732kb
input:
79 5 7 -5 -1 7 8 10 -1 1 10 19 20 -1 4 10 25 26 -1 5 10 27 28 -2 -1 10 13 14 -4 -1 9 10 10 -1 3 10 23 24 5 6 -7 -5 2 3 5 2 3 11 13 14 -5 -3 2 3 6 2 3 7 9 12 2 3 3 5 10 2 3 5 7 11 3 4 -10 -7 -5 -10 1 3 -10 -4 -2 -10 0 2 5 10 -10 -6 -1 6 7 -10 4 7 9 11 -10 7 8 13 13 -10 7 14 16 19 -10 7 12 15 17 -10 6...
output:
3 1 4 5 2 4 5 1 2 3 1 2 3 1 4 5 3 2 3 2 1 1 3 4 5 1 2 3 4 1 5 2 1 4 2 5 3 2 3 4 5 1 2 3 1 1 1 3 4 5 2 1 3 2 4 5 5 2 1 3 4 1 3 4 5 2 2 1 3 4 5 1 4 2 3 5 4 1 2 3 1 4 2 3 1 1 3 4 5 2 1 3 4 2 1 2 3 1 1 2 3 4 5 2 1 3 4 5 1 2 1 2 3 5 4 1 2 3 2 5 1 3 4 1 2 4 3 5 1 2 1 3 4 5...
result:
ok 79 lines
Test #7:
score: 0
Accepted
time: 8ms
memory: 8824kb
input:
4 89 162 -98 -97 -94 -92 -91 -88 -82 -80 -77 -71 -70 -66 -64 -63 -62 -60 -56 -53 -48 -44 -43 -40 -37 -36 -34 -32 -31 -26 -25 -23 -21 -18 -17 -12 -11 -9 -8 -7 -4 -1 1 2 6 9 16 18 19 20 22 23 24 27 28 33 34 36 40 41 43 45 47 49 51 52 53 54 55 58 59 63 68 69 70 74 75 76 77 78 79 80 82 83 90 92 93 96 97...
output:
1 67 68 2 3 4 29 30 69 31 32 33 34 70 71 35 36 37 5 38 72 73 39 40 41 42 43 74 6 44 75 45 7 76 46 77 47 8 9 10 48 11 49 12 13 14 15 16 78 17 79 18 19 80 20 21 50 51 52 53 22 54 55 23 56 57 24 81 25 82 26 83 58 59 84 60 27 61 62 63 85 64 65 86 66 28 87 88 89 10 11 12 13 1 4 5 6 7 8 2 9 3 14 15 16 1...
result:
ok 4 lines
Test #8:
score: 0
Accepted
time: 6ms
memory: 10100kb
input:
6 97 148 -94 -93 -91 -90 -89 -85 -84 -82 -79 -76 -75 -73 -67 -65 -62 -61 -60 -59 -58 -57 -56 -54 -52 -50 -48 -47 -44 -42 -41 -36 -34 -31 -29 -28 -27 -25 -22 -21 -19 -18 -15 -14 -5 -3 3 4 7 8 9 10 11 12 16 17 18 19 29 33 34 35 37 39 41 44 45 47 51 52 55 56 59 61 62 63 64 65 66 68 69 70 71 74 76 77 79...
output:
44 45 1 46 2 3 4 5 47 6 48 49 7 50 8 51 9 52 53 54 10 55 11 12 13 56 57 58 59 60 14 61 62 63 15 64 65 16 66 17 18 67 19 20 68 21 69 70 71 22 72 73 23 24 74 75 76 77 78 25 26 27 28 29 79 30 31 32 33 34 35 80 36 81 82 37 83 84 85 86 38 87 88 39 89 90 91 40 92 93 94 41 95 42 96 43 97 45 46 47 1 2 48 4...
result:
ok 6 lines
Test #9:
score: 0
Accepted
time: 0ms
memory: 7792kb
input:
125 1 2 1 8 2 3 -1 2 8 11 9 12 3 5 -2 -1 0 -2 -1 0 -2 -1 0 -2 -1 0 -2 -1 0 3 4 -3 -1 0 -3 -1 6 -3 -1 1 -3 -1 5 3 5 0 1 3 0 1 9 0 1 8 0 1 7 0 1 12 3 5 -1 2 3 0 2 4 2 4 8 2 5 9 2 2 6 3 5 -1 1 3 -1 4 6 -1 5 7 -1 10 12 -1 8 10 3 4 -3 -1 3 -1 1 5 1 3 7 6 8 12 2 3 -1 1 -1 1 -1 1 3 4 1 2 3 2 3 6 2 3 7 2 3 ...
output:
1 1 2 1 2 3 1 2 3 1 2 3 2 1 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 2 3 1 1 2 3 1 2 1 2 1 2 3 1 2 3 2 1 1 2 3 1 2 3 1 2 1 1 2 3 1 1 2 1 2 1 1 2 3 3 1 2 1 3 2 1 2 1 2 1 2 1 2 1 2 3 3 1 2 1 2 3 1 1 1 2 3 2 3 1 3 1 2 1 2 1 3 2 3 1 2 3 1 1 2 3 1 1 2 3 1 2 3 1 3 2 ...
result:
ok 125 lines
Test #10:
score: 0
Accepted
time: 56ms
memory: 9740kb
input:
1 313 500 -4934898 -4922921 -4840685 -4775612 -4764872 -4727198 -4693308 -4666087 -4655510 -4584215 -4578427 -4567563 -4514324 -4459534 -4437224 -4420210 -4416294 -4400358 -4391202 -4374732 -4322821 -4301462 -4237369 -4209493 -4180742 -4131190 -4121838 -4112321 -4019936 -3988109 -3980007 -3979429 -3...
output:
226 273 146 206 237 44 313 1 272 86 231 248 199 242 271 175 130 76 70 115 124 267 200 283 102 147 164 46 279 205 100 10 227 233 134 41 137 6 282 32 110 212 112 72 280 51 119 198 23 264 284 19 269 302 129 163 256 58 230 3 195 2 247 101 179 128 107 126 224 125 150 94 82 289 62 87 186 64 59 204 16 303 ...
result:
ok single line: '226 273 146 206 237 44 313 1 2... 43 14 114 71 8 294 28 104 168 '
Test #11:
score: -100
Runtime Error
input:
1 469 500 -998 -996 -991 -988 -983 -978 -975 -972 -966 -955 -953 -951 -945 -941 -933 -932 -931 -929 -927 -926 -925 -923 -922 -920 -914 -910 -901 -893 -891 -890 -878 -877 -872 -845 -826 -821 -820 -814 -813 -809 -808 -803 -799 -795 -792 -791 -790 -789 -781 -775 -769 -766 -763 -760 -754 -752 -749 -748 ...