답안 #412477

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412477 2021-05-27T00:51:06 Z rqi Naan (JOI19_naan) C++14
100 / 100
1372 ms 106812 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef vector<int> vi;
typedef long long ll;
typedef long double db;
 
#define pb push_back
 
const int MOD = 1e9+7;
const int mx = 2005;
 
 
ll GCD(ll a, ll b){
	a = abs(a); b = abs(b);
	if(a == 0) return b;
	if(b == 0) return a;
	return GCD(b%a, a);
}
 
struct frac{
	ll n, d;
	frac(){
		n = 0;
		d = 1;
	}
	frac(ll _n, ll _d, bool mode){
		n = _n;
		d = _d;
	}
	frac(ll _n, ll _d){
		if(_d < 0){
			_n*=-1;
			_d*=-1;
		}
		assert(_d >= 1);
		ll g = GCD(_n, _d);
		n = _n/g;
		d = _d/g;
	}
	frac(ll _n){
		n = _n;
		d = 1;
	}
};
 
frac operator+(frac a, frac b){
	return frac(a.n*b.d+b.n*a.d, a.d*b.d);
}
 
frac& operator+=(frac& a, frac b){
	a = a+b;
	return a;
}
 
frac operator-(frac a, frac b){
	b.n*=-1;
	return a+b;
}
 
frac operator/(frac a, frac b){
	ll g_n = GCD(a.n, b.n);
	a.n/=g_n;
	b.n/=g_n;
	ll g_d = GCD(a.d, b.d);
	a.d/=g_d;
	b.d/=g_d;
	return frac(a.n*b.d, a.d*b.n);
}
 
void print(frac a){
	cout << a.n << "/" << a.d << " ";
}
 
bool isEqual(frac a, frac b){
	ll g_n = GCD(a.n, b.n);
	a.n/=g_n;
	b.n/=g_n;
	ll g_d = GCD(a.d, b.d);
	a.d/=g_d;
	b.d/=g_d;
	return a.n*b.d == b.n*a.d;
}
 
bool isLess(frac a, frac b){
	ll g_n = GCD(a.n, b.n);
	a.n/=g_n;
	b.n/=g_n;
	ll g_d = GCD(a.d, b.d);
	a.d/=g_d;
	b.d/=g_d;
	return a.n*b.d < b.n*a.d;
}
 
int N, L;
ll V[mx][mx];
frac poses[mx][mx]; //person i, j/N fraction
bool used[mx];
 
int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> N >> L;
	// N = 2000;
	// L = 2000;
	mt19937 rng(127);
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= L; j++){
			cin >> V[i][j];
			//V[i][j] = 1 + rng() % 100000;
		}
	}

	clock_t BEG = clock();
 
	for(int i = 1; i <= N; i++){
		//cout << "i: " << i << "\n";
		ll weight_sum = 0;
		for(int j = 1; j <= L; j++){
			weight_sum+=V[i][j];
		}
		int cur_eaten = 0; //current prefix eaten
		ll cur_happy = 0;
		//frac cur_happy = frac(0);
		for(int j = 1; j <= N; j++){
			//cout << "j: " << j << "\n";
			while(cur_eaten+1 <= L){
 
				if(cur_happy+V[i][cur_eaten+1]*ll(N) < ll(j)*weight_sum){
					cur_happy+=V[i][cur_eaten+1]*ll(N);
					cur_eaten++;
					// cout << "cur_eaten: " << cur_eaten << "\n";
					// print(cur_happy);
					// cout << "\n";
				}
				else{
					break;
				}
			}
			assert(cur_eaten < L);
			poses[i][j] = frac(cur_eaten*V[i][cur_eaten+1]*ll(N)+ll(j)*weight_sum-cur_happy, V[i][cur_eaten+1]*ll(N));
			//poses[i][j] = frac(cur_eaten)+(ll(j)*weight_sum-cur_happy)/(frac(V[i][cur_eaten+1]*ll(N)));
		}
	}

	//cout << db(clock()-BEG)/db(CLOCKS_PER_SEC) << "\n";
 
	// for(int i = 1; i <= N; i++){
	// 	for(int j = 1; j <= N; j++){
	// 		cout << i << " " << j << " ";
	// 		print(poses[i][j]);
	// 		cout << "\n";
	// 	}
	// }
	vi perm;
	for(int i = 1; i <= N; i++){
		frac earliest = frac(MOD, 1);
		for(int j = 1; j <= N; j++){
			if(used[j]) continue; 
			if(isLess(poses[j][i], earliest)){
				earliest = poses[j][i];
			}
		}
		if(i <= N-1){
			cout << earliest.n << " " << earliest.d << "\n";
		}
		
		for(int j = 1; j <= N; j++){
			if(used[j]) continue; 
			if(isEqual(poses[j][i], earliest)){
				perm.pb(j);
				used[j] = 1;
				break;
			}
		}
	}

	//cout << db(clock()-BEG)/db(CLOCKS_PER_SEC) << "\n";
 
	for(auto u: perm){
		cout << u << " ";
	}
	cout << "\n";
}

Compilation message

naan.cpp: In function 'int main()':
naan.cpp:113:10: warning: unused variable 'BEG' [-Wunused-variable]
  113 |  clock_t BEG = clock();
      |          ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 63180 KB Output is correct
2 Correct 32 ms 63280 KB Output is correct
3 Correct 31 ms 63232 KB Output is correct
4 Correct 30 ms 63284 KB Output is correct
5 Correct 29 ms 63188 KB Output is correct
6 Correct 29 ms 63180 KB Output is correct
7 Correct 30 ms 63180 KB Output is correct
8 Correct 37 ms 63268 KB Output is correct
9 Correct 30 ms 63180 KB Output is correct
10 Correct 29 ms 63236 KB Output is correct
11 Correct 30 ms 63276 KB Output is correct
12 Correct 29 ms 63232 KB Output is correct
13 Correct 29 ms 63196 KB Output is correct
14 Correct 29 ms 63180 KB Output is correct
15 Correct 30 ms 63268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 63228 KB Output is correct
2 Correct 34 ms 63300 KB Output is correct
3 Correct 30 ms 63260 KB Output is correct
4 Correct 30 ms 63300 KB Output is correct
5 Correct 29 ms 63364 KB Output is correct
6 Correct 28 ms 63308 KB Output is correct
7 Correct 29 ms 63252 KB Output is correct
8 Correct 29 ms 63232 KB Output is correct
9 Correct 31 ms 63280 KB Output is correct
10 Correct 30 ms 63308 KB Output is correct
11 Correct 30 ms 63336 KB Output is correct
12 Correct 34 ms 63180 KB Output is correct
13 Correct 34 ms 63296 KB Output is correct
14 Correct 31 ms 63256 KB Output is correct
15 Correct 31 ms 63328 KB Output is correct
16 Correct 31 ms 63308 KB Output is correct
17 Correct 31 ms 63288 KB Output is correct
18 Correct 31 ms 63316 KB Output is correct
19 Correct 33 ms 63344 KB Output is correct
20 Correct 30 ms 63248 KB Output is correct
21 Correct 36 ms 63260 KB Output is correct
22 Correct 30 ms 63300 KB Output is correct
23 Correct 35 ms 63168 KB Output is correct
24 Correct 30 ms 63308 KB Output is correct
25 Correct 30 ms 63312 KB Output is correct
26 Correct 30 ms 63300 KB Output is correct
27 Correct 30 ms 63220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 63180 KB Output is correct
2 Correct 32 ms 63280 KB Output is correct
3 Correct 31 ms 63232 KB Output is correct
4 Correct 30 ms 63284 KB Output is correct
5 Correct 29 ms 63188 KB Output is correct
6 Correct 29 ms 63180 KB Output is correct
7 Correct 30 ms 63180 KB Output is correct
8 Correct 37 ms 63268 KB Output is correct
9 Correct 30 ms 63180 KB Output is correct
10 Correct 29 ms 63236 KB Output is correct
11 Correct 30 ms 63276 KB Output is correct
12 Correct 29 ms 63232 KB Output is correct
13 Correct 29 ms 63196 KB Output is correct
14 Correct 29 ms 63180 KB Output is correct
15 Correct 30 ms 63268 KB Output is correct
16 Correct 35 ms 63228 KB Output is correct
17 Correct 34 ms 63300 KB Output is correct
18 Correct 30 ms 63260 KB Output is correct
19 Correct 30 ms 63300 KB Output is correct
20 Correct 29 ms 63364 KB Output is correct
21 Correct 28 ms 63308 KB Output is correct
22 Correct 29 ms 63252 KB Output is correct
23 Correct 29 ms 63232 KB Output is correct
24 Correct 31 ms 63280 KB Output is correct
25 Correct 30 ms 63308 KB Output is correct
26 Correct 30 ms 63336 KB Output is correct
27 Correct 34 ms 63180 KB Output is correct
28 Correct 34 ms 63296 KB Output is correct
29 Correct 31 ms 63256 KB Output is correct
30 Correct 31 ms 63328 KB Output is correct
31 Correct 31 ms 63308 KB Output is correct
32 Correct 31 ms 63288 KB Output is correct
33 Correct 31 ms 63316 KB Output is correct
34 Correct 33 ms 63344 KB Output is correct
35 Correct 30 ms 63248 KB Output is correct
36 Correct 36 ms 63260 KB Output is correct
37 Correct 30 ms 63300 KB Output is correct
38 Correct 35 ms 63168 KB Output is correct
39 Correct 30 ms 63308 KB Output is correct
40 Correct 30 ms 63312 KB Output is correct
41 Correct 30 ms 63300 KB Output is correct
42 Correct 30 ms 63220 KB Output is correct
43 Correct 144 ms 67576 KB Output is correct
44 Correct 761 ms 82332 KB Output is correct
45 Correct 289 ms 74220 KB Output is correct
46 Correct 56 ms 65136 KB Output is correct
47 Correct 448 ms 78236 KB Output is correct
48 Correct 1109 ms 77496 KB Output is correct
49 Correct 270 ms 70720 KB Output is correct
50 Correct 1372 ms 91200 KB Output is correct
51 Correct 485 ms 83312 KB Output is correct
52 Correct 1142 ms 97280 KB Output is correct
53 Correct 1044 ms 92560 KB Output is correct
54 Correct 30 ms 63300 KB Output is correct
55 Correct 557 ms 70484 KB Output is correct
56 Correct 641 ms 87892 KB Output is correct
57 Correct 544 ms 84692 KB Output is correct
58 Correct 950 ms 89436 KB Output is correct
59 Correct 519 ms 84972 KB Output is correct
60 Correct 841 ms 106812 KB Output is correct
61 Correct 876 ms 106120 KB Output is correct
62 Correct 1241 ms 106800 KB Output is correct
63 Correct 1059 ms 105408 KB Output is correct
64 Correct 940 ms 105860 KB Output is correct
65 Correct 890 ms 103388 KB Output is correct
66 Correct 893 ms 103452 KB Output is correct
67 Correct 850 ms 103384 KB Output is correct
68 Correct 357 ms 87640 KB Output is correct
69 Correct 378 ms 90424 KB Output is correct
70 Correct 438 ms 89836 KB Output is correct
71 Correct 538 ms 99232 KB Output is correct