Submission #375643

# Submission time Handle Problem Language Result Execution time Memory
375643 2021-03-09T15:59:10 Z casperwang Naan (JOI19_naan) C++14
100 / 100
1409 ms 130156 KB
#include <bits/stdc++.h>
#define int long long
#define ll __int128
#define pii pair<int,int>
#define ppi pair<pii,int>
#define ff first
#define ss second
using namespace std;
#define debug(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 pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }

const int MAXN = 2000;
const int INF = 1e9;
int n, L;
int v[MAXN+1][MAXN+1];
int pre[MAXN+1][MAXN+1];
int sum[MAXN+1];
pii arr[MAXN+1][MAXN+1];
bool vis[MAXN+1];
pii ans[MAXN];
int p[MAXN+1];

pii operator - (pii a, pii b) {
	int k = a.ss / __gcd(a.ss, b.ss) * b.ss;
	int c = __gcd(a.ff * (k / a.ss) - b.ff * (k / b.ss), k);
	return pii((a.ff * (k / a.ss) - b.ff * (k / b.ss)) / c, k / c);
}

pii operator + (pii a, pii b) {
	int k = a.ss / __gcd(a.ss, b.ss) * b.ss;
	int c = __gcd(a.ff * (k / a.ss) + b.ff * (k / b.ss), k);
	return pii((a.ff * (k / a.ss) + b.ff * (k / b.ss)) / c, k / c);
}

pii operator * (pii a, int b) {
	int k = __gcd(a.ss, b);
	return pii(a.ff * b / k, a.ss / k);
}

pii operator / (pii a, int b) {
	int k = __gcd(a.ff, b);
	return pii(a.ff / k, a.ss * b / k);
}

bool operator >= (pii a, pii b) {
	return (ll)a.ff * b.ss >= (ll)b.ff * a.ss;
}

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> L;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= L; j++) {
			cin >> v[i][j];
			pre[i][j] = pre[i][j-1] + v[i][j];
			sum[i] += v[i][j];
		}
	}
	for (int i = 1; i <= n; i++) {
		int id = 1;
		for (int j = 1; j <= n; j++) {
			pii k(sum[i] * j, n);
			while (true) {
				if (pii(pre[i][id], 1) >= k) {
					arr[i][j] = (k - pii(pre[i][id-1], 1)) / v[i][id];
					arr[i][j] = arr[i][j] + pii(id-1, 1);
					break;
				} else {
					id++;
				}
			}
		}
	}
	ans[0] = pii(0, 1);
	for (int i = 1; i <= n; i++) {
		ppi mmin(pii(INF, 0), 0);
		for (int j = 1; j <= n; j++) {
			if (vis[j]) continue;
			assert(arr[j][i] >= ans[i-1]);
			if (mmin.ff >= arr[j][i])
				mmin = ppi(arr[j][i], j);
		}
		vis[mmin.ss] = true;
		p[i] = mmin.ss;
		if (i < n)
			ans[i] = mmin.ff;
	}
	for (int i = 1; i < n; i++)
		cout << ans[i].ff << ' ' << ans[i].ss << '\n';
	for (int i = 1; i <= n; i++)
		cout << p[i] << " \n"[i==n];
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 1 ms 492 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 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 1 ms 620 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 1 ms 492 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 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 620 KB Output is correct
25 Correct 1 ms 620 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 620 KB Output is correct
30 Correct 1 ms 620 KB Output is correct
31 Correct 1 ms 620 KB Output is correct
32 Correct 1 ms 620 KB Output is correct
33 Correct 1 ms 620 KB Output is correct
34 Correct 2 ms 620 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 2 ms 620 KB Output is correct
37 Correct 1 ms 620 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 492 KB Output is correct
40 Correct 1 ms 492 KB Output is correct
41 Correct 2 ms 492 KB Output is correct
42 Correct 1 ms 492 KB Output is correct
43 Correct 121 ms 16236 KB Output is correct
44 Correct 721 ms 73108 KB Output is correct
45 Correct 265 ms 36288 KB Output is correct
46 Correct 24 ms 5996 KB Output is correct
47 Correct 423 ms 53516 KB Output is correct
48 Correct 1098 ms 89580 KB Output is correct
49 Correct 245 ms 31596 KB Output is correct
50 Correct 1319 ms 121944 KB Output is correct
51 Correct 435 ms 58092 KB Output is correct
52 Correct 1088 ms 110828 KB Output is correct
53 Correct 1032 ms 104684 KB Output is correct
54 Correct 1 ms 492 KB Output is correct
55 Correct 541 ms 51076 KB Output is correct
56 Correct 617 ms 76412 KB Output is correct
57 Correct 500 ms 67436 KB Output is correct
58 Correct 916 ms 101100 KB Output is correct
59 Correct 492 ms 63852 KB Output is correct
60 Correct 1190 ms 125932 KB Output is correct
61 Correct 1209 ms 125932 KB Output is correct
62 Correct 1365 ms 125676 KB Output is correct
63 Correct 1297 ms 126188 KB Output is correct
64 Correct 1224 ms 125900 KB Output is correct
65 Correct 1388 ms 129940 KB Output is correct
66 Correct 1409 ms 130156 KB Output is correct
67 Correct 1219 ms 129848 KB Output is correct
68 Correct 503 ms 76836 KB Output is correct
69 Correct 473 ms 72300 KB Output is correct
70 Correct 651 ms 90036 KB Output is correct
71 Correct 755 ms 99900 KB Output is correct