#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
struct fraction {
long long x, y;
long long integer() {
return x / y;
}
void simplify() {
long long d = __gcd( x, y );
x /= d;
y /= d;
}
};
fraction operator - ( long long a, fraction b ) {
fraction ans = { a * b.y - b.x, b.y };
ans.simplify();
return ans;
}
fraction operator * ( long long a, fraction b ) {
fraction ans = { a * b.x, b.y };
ans.simplify();
return ans;
}
bool operator < ( fraction a, fraction b ) {
return (long double)a.x / a.y < (long double)b.x / b.y;
}
bool operator > ( fraction a, fraction b ) {
return (long double)a.x / a.y > (long double)b.x / b.y;
}
fraction operator / ( fraction a, long long b ) {
fraction ans = { a.x, a.y * b };
ans.simplify();
return ans;
}
fraction operator + ( fraction a, long long b ) {
fraction ans = { a.x + a.y * b, a.y };
ans.simplify();
return ans;
}
fraction operator + ( long long a, fraction b ) {
return b + a;
}
fraction operator + ( fraction a, fraction b ) {
fraction ans = { a.x * b.y + b.x * a.y, a.y * b.y };
ans.simplify();
return ans;
}
fraction operator - ( fraction a, fraction b ) {
fraction ans = { a.x * b.y - b.x * a.y, a.y * b.y };
ans.simplify();
return ans;
}
const int MAX_N = 4000;
const int MAX_L = 4000;
bool active[MAX_N];
int happiness[MAX_N][MAX_L + 1], sumHappiness[MAX_N][MAX_L + 1], permutation[MAX_N];
fraction happy[MAX_N], timeFrac[MAX_N], timeFractions[MAX_N];
int main() {
int n, l;
cin >> n >> l;
for ( int i = 0; i < n; i++ ) {
happy[i] = { 0, n };
for ( int j = 1; j <= l; j++ ) {
cin >> happiness[i][j];
happy[i].x += happiness[i][j];
sumHappiness[i][j] = sumHappiness[i][j - 1] + happiness[i][j];
}
happy[i].simplify();
}
for ( int i = 0; i < n; i++ )
active[i] = true;
timeFractions[0] = { 0, 1 };
permutation[0] = 0;
for ( int pas = 1; pas <= n; pas++ ) {
for ( int i = 0; i < n; i++ ) {
if ( !active[i] ) {
timeFrac[i] = { INF + 1, 1 };
continue;
}
int j = timeFractions[pas - 1].integer();
fraction crtHappiness = happiness[i][j + 1] * (j + 1 - timeFractions[pas - 1]);
crtHappiness.simplify();
if ( crtHappiness > happy[i] ) {
timeFrac[i] = timeFractions[pas - 1] + happy[i] / happiness[i][j + 1];
continue;
}
int left = j + 1, right = l + 1;
while ( right - left > 1 ) {
int mid = (left + right) / 2;
if ( crtHappiness + (sumHappiness[i][mid] - sumHappiness[i][j + 1]) < happy[i] )
left = mid;
else
right = mid;
}
if ( left == l ) {
timeFrac[i] = { INF, 1 };
continue;
}
//printf( "(%d %d %d)\n", left, crtHappiness.x, crtHappiness.y );
crtHappiness = crtHappiness + (sumHappiness[i][left] - sumHappiness[i][j + 1]);
timeFrac[i] = left + (happy[i] - crtHappiness) / happiness[i][left + 1];
}
// printf( "\n" );
int p = 0;
for ( int i = 0; i < n; i++ ) {
if ( active[i] && timeFrac[i] < timeFrac[p] )
p = i;
// printf( "(%d %d) ", timeFrac[i].x, timeFrac[i].y );
}
//printf( "\n" );
timeFractions[pas] = timeFrac[p];
permutation[pas] = p;
active[p] = false;
}
for ( int i = 1; i < n; i++ )
cout << timeFractions[i].x << " " << timeFractions[i].y << "\n";
for ( int i = 1; i <= n; i++ )
cout << permutation[i] + 1 << " ";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
3 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 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 |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
2 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 |
340 KB |
Output is correct |
43 |
Runtime error |
182 ms |
13832 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |