/*input
7 1
1
2
3
4
5
6
7
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
// credit: https://codeforces.com/blog/entry/66022?#comment-500539
struct Frac
{
uint64_t nume, demo;
Frac(uint64_t _nume = 0, uint64_t _demo = 1): nume(_nume), demo(_demo) {}
bool operator<(const Frac fr) const
{
if (nume / demo != fr.nume / fr.demo) return nume / demo < fr.nume / fr.demo;
return (nume - nume / demo * demo) * fr.demo < (fr.nume - fr.nume / fr.demo * fr.demo) * demo;
}
bool operator<=(const Frac fr) const
{
if (nume / demo != fr.nume / fr.demo) return nume / demo < fr.nume / fr.demo;
return (nume - nume / demo * demo) * fr.demo <= (fr.nume - fr.nume / fr.demo * fr.demo) * demo;
}
Frac operator+(const uint64_t x) const
{
return {nume + demo * x, demo};
}
Frac operator-(const uint64_t x) const
{
return {nume - demo * x, demo};
}
Frac operator/ (const uint64_t x) const
{
return {nume, demo * x};
}
friend ostream& operator<< (ostream &os, const Frac fr)
{
return os << fr.nume << ' ' << fr.demo;
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
uint32_t N, L;
cin >> N >> L;
vector<vector<uint32_t>> V(N, vector<uint32_t>(L));
for (auto &vec : V) for (auto &i : vec) cin >> i;
vector<vector<Frac>> joints(N, vector<Frac>(N, {0, 1}));
for (uint32_t person = 0; person < N; ++person) {
uint64_t total = accumulate(V[person].begin(), V[person].end(), (uint32_t) 0);
uint32_t flavor = 0, happiness = 0;
for (uint32_t split = 1; split < N; ++split) {
Frac target(total * split, N);
while (Frac(happiness + V[person][flavor], 1) <= target) {
happiness += V[person][flavor];
++flavor;
}
joints[person][split] = ((target - happiness) / V[person][flavor]) + flavor;
}
}
vector<uint32_t> P(N);
vector<bool> used(N, false);
for (uint32_t joint = 1; joint < N; ++joint) {
uint32_t candidate = N;
for (uint32_t person = 0; person < N; ++person) {
if ((not used[person]) and (candidate == N or joints[person][joint] < joints[candidate][joint])) {
candidate = person;
}
}
P[joint - 1] = candidate;
used[candidate] = true;
cout << joints[candidate][joint] << '\n';
}
for (uint32_t person = 0; person < N; ++person) {
if (not used[person]) {
P[N - 1] = person;
break;
}
}
for (auto person : P) {
cout << person + 1 << ' ';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
10 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
380 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
248 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
1 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
256 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
10 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
380 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
248 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
1 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
376 KB |
Output is correct |
33 |
Correct |
3 ms |
376 KB |
Output is correct |
34 |
Correct |
3 ms |
376 KB |
Output is correct |
35 |
Correct |
3 ms |
376 KB |
Output is correct |
36 |
Correct |
3 ms |
376 KB |
Output is correct |
37 |
Correct |
3 ms |
376 KB |
Output is correct |
38 |
Correct |
2 ms |
256 KB |
Output is correct |
39 |
Correct |
3 ms |
376 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
3 ms |
376 KB |
Output is correct |
43 |
Correct |
45 ms |
7928 KB |
Output is correct |
44 |
Correct |
277 ms |
41136 KB |
Output is correct |
45 |
Correct |
155 ms |
20328 KB |
Output is correct |
46 |
Correct |
22 ms |
2552 KB |
Output is correct |
47 |
Correct |
198 ms |
29396 KB |
Output is correct |
48 |
Correct |
244 ms |
60920 KB |
Output is correct |
49 |
Correct |
74 ms |
15224 KB |
Output is correct |
50 |
Correct |
356 ms |
76896 KB |
Output is correct |
51 |
Correct |
167 ms |
27868 KB |
Output is correct |
52 |
Correct |
380 ms |
66216 KB |
Output is correct |
53 |
Correct |
296 ms |
58780 KB |
Output is correct |
54 |
Correct |
3 ms |
376 KB |
Output is correct |
55 |
Correct |
114 ms |
32248 KB |
Output is correct |
56 |
Correct |
221 ms |
39800 KB |
Output is correct |
57 |
Correct |
179 ms |
33144 KB |
Output is correct |
58 |
Correct |
266 ms |
57600 KB |
Output is correct |
59 |
Correct |
193 ms |
32636 KB |
Output is correct |
60 |
Correct |
593 ms |
100704 KB |
Output is correct |
61 |
Correct |
649 ms |
100872 KB |
Output is correct |
62 |
Correct |
686 ms |
100676 KB |
Output is correct |
63 |
Correct |
621 ms |
101092 KB |
Output is correct |
64 |
Correct |
598 ms |
100888 KB |
Output is correct |
65 |
Correct |
453 ms |
87600 KB |
Output is correct |
66 |
Correct |
474 ms |
87676 KB |
Output is correct |
67 |
Correct |
473 ms |
87672 KB |
Output is correct |
68 |
Correct |
230 ms |
43004 KB |
Output is correct |
69 |
Correct |
263 ms |
40696 KB |
Output is correct |
70 |
Correct |
272 ms |
52844 KB |
Output is correct |
71 |
Correct |
373 ms |
63380 KB |
Output is correct |