Submission #118142

# Submission time Handle Problem Language Result Execution time Memory
118142 2019-06-18T08:51:04 Z scanhex Naan (JOI19_naan) C++17
100 / 100
1065 ms 127608 KB
#include <bits/stdc++.h>

using namespace std;
using nagai = __int128;
using ll=long long;
#define sz(x) int((x).size())

const int N=2020;
int n,m;
nagai matr[N][N];
nagai pref[N][N];
nagai gcd(nagai a,nagai b){
	while(b){
		 a%=b,swap(a,b);
	}
	return a;
}
struct frac{
	nagai a,b;
	frac(){}
	void check(){
		if(a==0)return;
		nagai g=gcd(a,b);
		a/=g;
		b/=g;
	}
	frac(nagai a,nagai b):a(a),b(b){
	}
	frac(nagai x):a(x),b(1){}
	frac operator *(frac o){
		return frac(a*o.a,b*o.b);
	}
	frac operator +(frac o){
		return frac(a*o.b+o.a*b,b*o.b);
	}
	frac operator -(frac o){
		return frac(a*o.b-o.a*b,b*o.b);
	}
	bool operator <=(frac o){
		return a*o.b<=b*o.a;
	}
	bool operator <(frac o){
		return a*o.b<b*o.a;
	}
};
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>m;
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j){
			ll x;
			cin>>x;
			matr[i][j]=x;
			pref[i][j+1]=pref[i][j]+matr[i][j];
		}
	frac cur(0);
	vector<frac>fr;
	vector<int>ord;
	vector<bool>used(n);
	vector<int>ptr(n);
	for(int i=0;i<n;++i){
		frac mn(1e9);
		int mni=-1;
		for(int j=0;j<n;++j){
			if(used[j])continue;
			while(true){
				int L=ptr[j];
				if(L<m&&frac(pref[j][L])<=frac(pref[j][m])*frac(i+1,n))
					++ptr[j];
				else
					break;
			}
			int L=ptr[j]-1;
			frac rest(0);
			rest=frac(pref[j][m])*frac(i+1,n)-frac(pref[j][L]);
			if(rest.a)
				rest=rest*frac(1,matr[j][L]);
			rest=rest+frac(L);
			rest.check();
			if(rest<mn)
				mn=rest,mni=j;
		}
		cur=mn;
//		cerr<<cur.a<<' '<<cur.b<<'\n';
		fr.push_back(cur);
		ord.push_back(mni);
		used[mni]=true;
	}
	for(int i=0;i<n-1;++i){
		cout<<(ll)fr[i].a<<' '<<(ll)fr[i].b<<'\n';
	}
	for(int i=0;i<n;++i)
		cout<<ord[i]+1<<' ';
	cout<<'\n';

	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 3 ms 640 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 768 KB Output is correct
15 Correct 2 ms 640 KB Output is correct
16 Correct 3 ms 768 KB Output is correct
17 Correct 3 ms 640 KB Output is correct
18 Correct 3 ms 768 KB Output is correct
19 Correct 3 ms 768 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 3 ms 768 KB Output is correct
22 Correct 3 ms 768 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 2 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 768 KB Output is correct
25 Correct 3 ms 768 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 768 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 3 ms 768 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 3 ms 768 KB Output is correct
34 Correct 3 ms 768 KB Output is correct
35 Correct 3 ms 640 KB Output is correct
36 Correct 3 ms 768 KB Output is correct
37 Correct 3 ms 768 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 3 ms 640 KB Output is correct
40 Correct 2 ms 640 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 3 ms 640 KB Output is correct
43 Correct 83 ms 14044 KB Output is correct
44 Correct 502 ms 65684 KB Output is correct
45 Correct 211 ms 44408 KB Output is correct
46 Correct 30 ms 7928 KB Output is correct
47 Correct 314 ms 52980 KB Output is correct
48 Correct 672 ms 42872 KB Output is correct
49 Correct 155 ms 23800 KB Output is correct
50 Correct 870 ms 96780 KB Output is correct
51 Correct 341 ms 63224 KB Output is correct
52 Correct 774 ms 109300 KB Output is correct
53 Correct 704 ms 85608 KB Output is correct
54 Correct 2 ms 640 KB Output is correct
55 Correct 327 ms 16964 KB Output is correct
56 Correct 457 ms 79868 KB Output is correct
57 Correct 378 ms 63252 KB Output is correct
58 Correct 622 ms 76384 KB Output is correct
59 Correct 377 ms 71580 KB Output is correct
60 Correct 861 ms 127096 KB Output is correct
61 Correct 853 ms 127016 KB Output is correct
62 Correct 1065 ms 126604 KB Output is correct
63 Correct 971 ms 127108 KB Output is correct
64 Correct 916 ms 127188 KB Output is correct
65 Correct 984 ms 127480 KB Output is correct
66 Correct 978 ms 127608 KB Output is correct
67 Correct 840 ms 127488 KB Output is correct
68 Correct 367 ms 63656 KB Output is correct
69 Correct 352 ms 78300 KB Output is correct
70 Correct 434 ms 69372 KB Output is correct
71 Correct 542 ms 101444 KB Output is correct