답안 #524652

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524652 2022-02-09T18:12:45 Z fatemetmhr Naan (JOI19_naan) C++17
100 / 100
2891 ms 194064 KB
//  ~Be name khoda~  //
 
#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define pb       push_back
#define mp       make_pair
#define fi       first
#define se       second
#define cl       clear
 
const int maxn  =  1e6   + 10;
const int maxn5 =  2e3   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  2e18;
 
struct kasr{
	__int128 s, m;
	kasr(__int128 a, __int128 b){
		__int128 g = __gcd(a, b);
		s = a / g;
		m = b / g;
	}
	kasr(){
		s = 0;
		m = 1;
	}
	
	inline ll ciel(){
		__int128 f = s / m;
		if(s * f != m)
			f++;
		return f;
	}
	
};
 
 
 
inline kasr operator -(ll a, kasr b){
	a *= b.m;
	return kasr(a - b.s, b.m);
}
inline kasr operator -(kasr a, kasr b){
	__int128 mm = a.m * b.m / __gcd(a.m, b.m);
	return kasr(a.s * (mm / a.m) - b.s * (mm / b.m), mm);
}
inline kasr operator +(kasr a, kasr b){
	__int128 mm = a.m * b.m / __gcd(a.m, b.m);
	return kasr(a.s * (mm / a.m) + b.s * (mm / b.m), mm);
}
inline bool operator <=(kasr a, kasr b){
	return a.s * b.m <= b.s * a.m;
}
inline bool operator >=(kasr a, kasr b){
	return a.s * b.m >= b.s * a.m;
}
inline bool operator <(kasr a, kasr b){
	return a.s * b.m < b.s * a.m;
}
inline bool operator >(kasr a, kasr b){
	return a.s * b.m > b.s * a.m;
}
 
 
 
int n, l, per[maxn5];
ll a[maxn5][maxn5], ps[maxn5][maxn5];
kasr out[maxn5], have[maxn5][maxn5];
bool mark[maxn5];
 
 
 
inline ll get_val(int l, int r, int i){
	if(l >= r)
		return 0;
	return ps[i][r] - ps[i][l];
}
 
inline kasr cont(int i, kasr last, kasr ask){
	//cout << "starting " << i << ' ' << last.s << ' ' << last.m << '\n';
	kasr ret (0, 1);
	int id = last.ciel();
	if(kasr(id, 1) > last){
		kasr rem = id - last;
		kasr sup = kasr(ask.s, ask.m * a[i][id]);
		if(sup >= rem){
			last = kasr(id, 1);
			ask = ask - kasr(rem.s * a[i][id], rem.m);
		}
		else{
			return last + sup;
		}
	}
	int lo = id, hi = l + 1;
	while(hi - lo > 1){
		int mid = (lo + hi) >> 1;
		if(kasr(get_val(id, mid, i), 1) <= ask)
			lo = mid;
		else
			hi = mid;
	}
	//cout << "ok " << lo << endl;
	last = kasr(lo, 1);
	ask = ask - kasr(get_val(id, lo, i), 1);
	if(ask.s == 0)
		return last;
	kasr sup = kasr(ask.s, ask.m * a[i][lo + 1]);
	//cout << ask.s << ' ' << ask.m << endl;
	return last + sup;
}
 
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 
	cin >> n >> l;
	
	for(int i = 0; i < n; i++){
		ll sum = 0;
		for(int j = 1; j <= l; j++){
			cin >> a[i][j];
			ps[i][j] = a[i][j] + ps[i][j - 1];
			sum += a[i][j];
		}
		
		kasr last(0, 1);
		for(int j = 0; j < n; j++){
			//cout << i << ' ' << j << ' ' << last.s << ' ' << last.m << ' ' << sum << '\n';
			last = have[i][j] = cont(i, last, kasr(sum, n));
		}
	}
	
	//cout << have[1][0].s << ' ' << have[1][0].m << endl;
	
	for(int i = 0; i < n - 1; i++){
		out[i] = kasr(l + 5, 1);
		per[i] = -1;
		for(int j = 0; j < n; j++) 
			if(!mark[j] && have[j][i] < out[i]){
				out[i] = have[j][i];
				//cout << i << ' ' << j << ' ' << out[i].s << ' ' << out[i].m << endl;
				if(per[i] != -1)
					mark[per[i]] = false;
				per[i] = j;
				mark[j] = true;
			}
		cout << ll(out[i].s) << ' ' << ll(out[i].m) << '\n';
	}
	for(int i = 0; i < n; i++) if(!mark[i])
		per[n - 1] = i;
	for(int i = 0; i < n; i++){
		cout << per[i] + 1 << ' ';
	}
	cout << endl;
}
















# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 126772 KB Output is correct
2 Correct 47 ms 126876 KB Output is correct
3 Correct 47 ms 126940 KB Output is correct
4 Correct 47 ms 126896 KB Output is correct
5 Correct 47 ms 126916 KB Output is correct
6 Correct 52 ms 126872 KB Output is correct
7 Correct 51 ms 127004 KB Output is correct
8 Correct 47 ms 126832 KB Output is correct
9 Correct 47 ms 126876 KB Output is correct
10 Correct 47 ms 126916 KB Output is correct
11 Correct 48 ms 126924 KB Output is correct
12 Correct 48 ms 126820 KB Output is correct
13 Correct 64 ms 126828 KB Output is correct
14 Correct 49 ms 126908 KB Output is correct
15 Correct 48 ms 126928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 126900 KB Output is correct
2 Correct 48 ms 126976 KB Output is correct
3 Correct 48 ms 126936 KB Output is correct
4 Correct 49 ms 126980 KB Output is correct
5 Correct 48 ms 126956 KB Output is correct
6 Correct 52 ms 126900 KB Output is correct
7 Correct 49 ms 126936 KB Output is correct
8 Correct 49 ms 126888 KB Output is correct
9 Correct 60 ms 126960 KB Output is correct
10 Correct 55 ms 127020 KB Output is correct
11 Correct 49 ms 126908 KB Output is correct
12 Correct 48 ms 126876 KB Output is correct
13 Correct 48 ms 126916 KB Output is correct
14 Correct 49 ms 127040 KB Output is correct
15 Correct 51 ms 127044 KB Output is correct
16 Correct 49 ms 127036 KB Output is correct
17 Correct 49 ms 126988 KB Output is correct
18 Correct 50 ms 126976 KB Output is correct
19 Correct 56 ms 127048 KB Output is correct
20 Correct 49 ms 127012 KB Output is correct
21 Correct 54 ms 126960 KB Output is correct
22 Correct 51 ms 126972 KB Output is correct
23 Correct 50 ms 126796 KB Output is correct
24 Correct 53 ms 126916 KB Output is correct
25 Correct 53 ms 126900 KB Output is correct
26 Correct 49 ms 126900 KB Output is correct
27 Correct 49 ms 126916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 126772 KB Output is correct
2 Correct 47 ms 126876 KB Output is correct
3 Correct 47 ms 126940 KB Output is correct
4 Correct 47 ms 126896 KB Output is correct
5 Correct 47 ms 126916 KB Output is correct
6 Correct 52 ms 126872 KB Output is correct
7 Correct 51 ms 127004 KB Output is correct
8 Correct 47 ms 126832 KB Output is correct
9 Correct 47 ms 126876 KB Output is correct
10 Correct 47 ms 126916 KB Output is correct
11 Correct 48 ms 126924 KB Output is correct
12 Correct 48 ms 126820 KB Output is correct
13 Correct 64 ms 126828 KB Output is correct
14 Correct 49 ms 126908 KB Output is correct
15 Correct 48 ms 126928 KB Output is correct
16 Correct 48 ms 126900 KB Output is correct
17 Correct 48 ms 126976 KB Output is correct
18 Correct 48 ms 126936 KB Output is correct
19 Correct 49 ms 126980 KB Output is correct
20 Correct 48 ms 126956 KB Output is correct
21 Correct 52 ms 126900 KB Output is correct
22 Correct 49 ms 126936 KB Output is correct
23 Correct 49 ms 126888 KB Output is correct
24 Correct 60 ms 126960 KB Output is correct
25 Correct 55 ms 127020 KB Output is correct
26 Correct 49 ms 126908 KB Output is correct
27 Correct 48 ms 126876 KB Output is correct
28 Correct 48 ms 126916 KB Output is correct
29 Correct 49 ms 127040 KB Output is correct
30 Correct 51 ms 127044 KB Output is correct
31 Correct 49 ms 127036 KB Output is correct
32 Correct 49 ms 126988 KB Output is correct
33 Correct 50 ms 126976 KB Output is correct
34 Correct 56 ms 127048 KB Output is correct
35 Correct 49 ms 127012 KB Output is correct
36 Correct 54 ms 126960 KB Output is correct
37 Correct 51 ms 126972 KB Output is correct
38 Correct 50 ms 126796 KB Output is correct
39 Correct 53 ms 126916 KB Output is correct
40 Correct 53 ms 126900 KB Output is correct
41 Correct 49 ms 126900 KB Output is correct
42 Correct 49 ms 126916 KB Output is correct
43 Correct 275 ms 135500 KB Output is correct
44 Correct 1408 ms 164900 KB Output is correct
45 Correct 487 ms 152964 KB Output is correct
46 Correct 76 ms 131744 KB Output is correct
47 Correct 912 ms 161360 KB Output is correct
48 Correct 1586 ms 157300 KB Output is correct
49 Correct 363 ms 142876 KB Output is correct
50 Correct 1841 ms 187120 KB Output is correct
51 Correct 612 ms 163364 KB Output is correct
52 Correct 1477 ms 185096 KB Output is correct
53 Correct 1382 ms 180140 KB Output is correct
54 Correct 49 ms 126908 KB Output is correct
55 Correct 791 ms 140888 KB Output is correct
56 Correct 880 ms 171996 KB Output is correct
57 Correct 740 ms 166216 KB Output is correct
58 Correct 1292 ms 175256 KB Output is correct
59 Correct 677 ms 166168 KB Output is correct
60 Correct 2453 ms 192596 KB Output is correct
61 Correct 2495 ms 192568 KB Output is correct
62 Correct 2891 ms 192492 KB Output is correct
63 Correct 2750 ms 192576 KB Output is correct
64 Correct 2485 ms 192620 KB Output is correct
65 Correct 1957 ms 193820 KB Output is correct
66 Correct 1851 ms 193956 KB Output is correct
67 Correct 1788 ms 194064 KB Output is correct
68 Correct 1114 ms 168072 KB Output is correct
69 Correct 1067 ms 170312 KB Output is correct
70 Correct 1380 ms 171852 KB Output is correct
71 Correct 1721 ms 180784 KB Output is correct