#undef KEV
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 2005, inf = 1e9;
using opl = long long;
int n, L;
opl v[MAX_N][MAX_N];
using lll = __int128;
#define ll lll
opl gcd(lll a, lll b) { return gcd(opl(a), opl(b)); }
struct frac {
ll up, dn;
void ref() { ll g = gcd(up, dn); up/=g, dn/=g; }
frac(ll a, ll b) :
up(a), dn(b) {}
frac(ll v) :
up(v), dn(1) {}
void sca(ll v) { up *= v, dn *= v; }
frac& chk() {
if (dn > inf) {
ll num = up / dn, lft = up % dn;
up = (lll)inf * lft / dn + num * inf, dn = inf;
}
return *this;
}
};
frac operator * (frac a, frac b) { a.up *= b.up, a.dn *= b.dn, a.ref(); return a.chk(); }
frac operator * (frac a, ll v) { a.up *= v; return a.chk(); }
frac operator / (frac a, ll v) { a.dn *= v; return a.chk(); }
frac operator + (frac a, frac b) {
ll da = a.dn, db = b.dn, nd = da / gcd(da, db) * db;
a.sca(nd/da), b.sca(nd/db);
a.up += b.up;
return a.chk();
}
frac operator - (frac a, frac b) {
ll da = a.dn, db = b.dn, nd = da / gcd(da, db) * db;
a.sca(nd/da), b.sca(nd/db);
a.up -= b.up;
return a.chk();
}
frac& operator += (frac &a, frac b) { return a = a + b; }
frac& operator -= (frac &a, frac b) { return a = a - b; }
bool operator < (frac a, frac b) { return (lll)a.up * b.dn < (lll)b.up * a.dn; }
bool operator > (frac a, frac b) { return (lll)a.up * b.dn > (lll)b.up * a.dn; }
ostream& operator << (ostream& out, frac a) { return out << (opl)a.up << ' ' << (opl)a.dn; }
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> L;
for (int i = 0;i < n;++i)
for (int j = 0;j < L;++j)
cin >> v[i][j];
vector<int> vis(n);
bool ok = true;
vector<frac> sep {{0, 1}};
vector<int> od;
for (int i = 0;i < n;++i) {
frac nl = frac(inf, 1);
int nid = -1;
for (int x = 0;x < n;++x) if (!vis[x]) {
int sum = 0;
for (int j = 0;j < L;++j)
sum += v[x][j];
auto [up, dn] = sep.back();
int atid = up / dn, lft = up - atid * dn;
frac need{sum, n};
if (frac(lft, dn) * v[x][atid] > need) {
frac len = need / v[x][atid];
if (chmin(nl, len + sep.back()))
nid = x;
continue;
}
need -= frac(lft, dn) * v[x][atid];
++atid;
while (atid < L) {
if (need < v[x][atid]) break;
need -= v[x][atid++];
}
if (atid == L && need > frac(0)) {
continue;
}
if (chmin(nl, frac(atid) + need / v[x][atid]))
nid = x;
}
if (nid == -1) {
assert(i+1 == n);
for (int j = 0;j < n;++j) if (!vis[j])
nid = j;
}
od.pb(nid);
sep.pb(nl);
vis[nid] = true;
}
for (int i = 1;i < n;++i)
cout << sep[i] << '\n';
for (int i = 0;i < n;++i)
cout << od[i]+1 << " \n"[i+1==n];
return 0;
}
Compilation message
naan.cpp: In function 'int32_t main()':
naan.cpp:82:7: warning: unused variable 'ok' [-Wunused-variable]
82 | bool ok = true;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
94 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
93 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
48 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
94 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
93 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
2 ms |
492 KB |
Output is correct |
34 |
Correct |
2 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Runtime error |
118 ms |
9580 KB |
Execution killed with signal 6 |
44 |
Halted |
0 ms |
0 KB |
- |