QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#552342 | #8940. Piggy Sort | Max_s_xaM | RE | 0ms | 0kb | C++17 | 4.9kb | 2024-09-07 22:09:11 | 2024-09-07 22:09:11 |
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++)
{
ll det = (f[i][j] - f[1][_]) * dx[_] - tim[i] * dy[_];
if (det == 0) { vis[i][j] = _; continue; }
}
int cnt = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
cnt += (vis[i][j] == _);
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: 0
Runtime Error
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