#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
using ll = long long;
bool operator<(pair<ll, ll> a, pair<ll, ll> b)
{
//x/y < z/k
//xk < zy
//maybe __int128
return (__int128)a.first * b.second < (__int128)a.second * b.first;
}
__int128 gcd(__int128 a, __int128 b)
{
if (a < b) swap(a, b);
while (b != 0)
{
a %= b;
swap(a, b);
}
return a;
}
pair<ll, ll> operator-(pair<ll, ll> _a, pair<ll, ll> _b)
{
pair<__int128, __int128> a(_a.first, _a.second);
pair<__int128, __int128> b(_b.first, _b.second);
b.first *= a.second;
a.first *= b.second;
pair<__int128, __int128> res{a.first - b.first, a.second * b.second};
__int128 gcded = gcd(res.first, res.second);
res.first /= gcded;
res.second /= gcded;
return res;
}
pair<ll, ll> operator+(pair<ll, ll> _a, pair<ll, ll> _b)
{
pair<__int128, __int128> a(_a.first, _a.second);
pair<__int128, __int128> b(_b.first, _b.second);
b.first *= a.second;
a.first *= b.second;
pair<__int128, __int128> res{a.first + b.first, a.second * b.second};
__int128 gcded = gcd(res.first, res.second);
res.first /= gcded;
res.second /= gcded;
return res;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, l;
cin >> n >> l;
vector<vector<ll>> v(n, vector<ll>(l));
vector<ll> sum;
for (auto &i : v)
{
for (ll &j : i) cin >> j;
sum.push_back(accumulate(i.begin(), i.end(), 0ll));
}
vector<int> p(n);
vector<pair<ll, ll>> last(n, {0, 1});
auto next = [&v, &sum, &n](int i, pair<ll, ll> last) -> pair<ll, ll>
{
pair<ll, ll> score{0, 1};
while (true)
{
ll s = v[i][last.first / last.second];
pair<ll, ll> d = pair{last.first / last.second + 1, 1} - last;
if (!(pair{d.first * s, d.second} + score < pair{sum[i], n})) break;
score = score + pair{d.first * s, d.second};
last = {last.first / last.second + 1, 1};
}
//(sum/l - score)/s = d
pair<ll, ll> d = pair{sum[i], n} - score;
ll s = v[i][last.first / last.second];
d.second *= s;
last = last + d;
return last;
};
for (int i = 0; i < n; ++i)
{
int ind = -1;
for (int j = 0; j < n; ++j)
{
if (last[j].second == 0) continue;
last[j] = next(j, last[j]);
if (ind == -1 || last[j] < last[ind]) ind = j;
}
p[i] = ind + 1;
if (i != n - 1) cout << last[ind].first << ' ' << last[ind].second << '\n';
last[ind] = {0, 0};
}
for (int i : p) cout << i << ' ';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
428 KB |
Output is correct |
18 |
Correct |
6 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
3 ms |
332 KB |
Output is correct |
31 |
Correct |
5 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
428 KB |
Output is correct |
33 |
Correct |
6 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
2 ms |
336 KB |
Output is correct |
43 |
Correct |
101 ms |
3932 KB |
Output is correct |
44 |
Correct |
667 ms |
23752 KB |
Output is correct |
45 |
Correct |
278 ms |
17968 KB |
Output is correct |
46 |
Correct |
30 ms |
3008 KB |
Output is correct |
47 |
Correct |
410 ms |
19512 KB |
Output is correct |
48 |
Correct |
728 ms |
9008 KB |
Output is correct |
49 |
Correct |
189 ms |
5468 KB |
Output is correct |
50 |
Correct |
970 ms |
26040 KB |
Output is correct |
51 |
Correct |
443 ms |
17540 KB |
Output is correct |
52 |
Correct |
856 ms |
32976 KB |
Output is correct |
53 |
Correct |
766 ms |
23072 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
364 ms |
1928 KB |
Output is correct |
56 |
Correct |
467 ms |
21812 KB |
Output is correct |
57 |
Correct |
470 ms |
16892 KB |
Output is correct |
58 |
Correct |
840 ms |
20004 KB |
Output is correct |
59 |
Correct |
587 ms |
21180 KB |
Output is correct |
60 |
Correct |
1116 ms |
53532 KB |
Output is correct |
61 |
Correct |
1171 ms |
53864 KB |
Output is correct |
62 |
Correct |
1280 ms |
53664 KB |
Output is correct |
63 |
Correct |
1285 ms |
54004 KB |
Output is correct |
64 |
Correct |
1092 ms |
53908 KB |
Output is correct |
65 |
Correct |
1214 ms |
40508 KB |
Output is correct |
66 |
Correct |
1204 ms |
40472 KB |
Output is correct |
67 |
Correct |
1142 ms |
40392 KB |
Output is correct |
68 |
Correct |
495 ms |
19236 KB |
Output is correct |
69 |
Correct |
496 ms |
25132 KB |
Output is correct |
70 |
Correct |
596 ms |
20848 KB |
Output is correct |
71 |
Correct |
739 ms |
35464 KB |
Output is correct |