Submission #123883

# Submission time Handle Problem Language Result Execution time Memory
123883 2019-07-02T08:32:00 Z 윤교준(#3030) Naan (JOI19_naan) C++14
100 / 100
3899 ms 69752 KB
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef long long lll;
//typedef __int128_t lll;
//typedef long double ld;
typedef double ld;
lll gcd(lll a, lll b) { for(;b;a%=b,swap(a,b)){} return a; }
static unsigned char str[55000055], *p=str;

const int MAXX = 10000000;

struct FRC {
	FRC(lll a, lll b) : a(a), b(b) {}
	FRC(lll a) : a(a), b(1) {}
	FRC() : a(0), b(1) {}
	ll a, b; // a / b

	void prt() const { printf("%lld %lld\n", ll(a), ll(b)); }
	FRC& norm() {
		if(!a) {
			b = 1;
			return *this;
		}
		if(b < 0) {
			a = -a;
			b = -b;
		}
		ll g = gcd(abs(a), b);
		if(g) {
			a /= g;
			b /= g;
		}
		return *this;
	}

	lll floor() const { return a/b; }

	FRC& snk() {
		if(MAXX < b) {
			ld t = ld(a) / b;
			b = MAXX;
			a = t*MAXX;
			a++;
			norm();
		}
		return *this;
	}
	FRC& rsnk() {
		if(MAXX < b) {
			ld t = ld(a) / b;
			b = MAXX;
			a = t*MAXX;
			norm();
		}
		return *this;
	}

	FRC operator + (const FRC &A) const {
		return FRC(a*A.b + b*A.a, b*A.b);
	}
	FRC operator - (const FRC &A) const {
		return FRC(a*A.b - b*A.a, b*A.b);
	}
	FRC operator * (const FRC &A) const {
		return FRC(a*A.a, b*A.b);
	}
	FRC operator / (const FRC &A) const {
		return FRC(a*A.b, b*A.a);
	}

	bool operator == (const FRC &A) const {
		return a == A.a && b == A.b;
	}
	bool operator < (const FRC &A) const {
		if(floor() != A.floor()) return floor() < A.floor();
		return (a-floor()*b)*A.b < b*(A.a-A.floor()*A.b);
	}
	bool operator <= (const FRC &A) const {
		return operator == (A) || operator < (A);
	}
	bool operator > (const FRC &A) const {
		return ! operator <= (A);
	}
	bool operator >= (const FRC &A) const {
		return ! operator < (A);
	}
};


const int MAXN = 2005;
const int MAXK = 2005;

ll AS[MAXN][MAXK];
int A[MAXN][MAXK];
int LS[MAXN];

FRC pos[MAXN], nd[MAXN];
int O[MAXN];
bitset<MAXN> chk;

int N, K;

FRC nxtF(int i, FRC pv, FRC nd) {
	pv.norm().snk(); nd.snk();
	if(nd <= 0) return pv;
	if(pv >= K) return K+1;
	int j = pv.floor();
	{
		FRC l = FRC(j+1) - pv;
		l.rsnk();
		if(nd <= (l * A[i][j+1]).rsnk())
			return (pv + (nd / A[i][j+1]).snk()).snk();
		nd = (nd - (l * A[i][j+1]).rsnk()).snk();
		pv = j+1;
	}
	nd.norm();
	j++;
	if(nd > AS[i][K]-AS[i][j]) return K+1;
	int s = max(j, LS[i]-1), e = K-1;
	for(int m; s < e;) {
		m = (s+e+1) >> 1;
		if(nd <= AS[i][m]-AS[i][j]) e = m-1;
		else s = m;
	}
	LS[i] = s;
	nd = (nd - (AS[i][s]-AS[i][j])).snk();
	nd.norm();
	pv = s;
	FRC l = (nd / A[i][s+1]).snk();
	return (pv + l).snk();
}

int main() {
	fread(str, 1, 55000055, stdin);

	rf(N) rf(K)
	for(int i = 1; i <= N; i++) {
		for(int j = 1; j <= K; j++) {
			rf(A[i][j])
			AS[i][j] = AS[i][j-1] + A[i][j];
		}
	}

	for(int i = 1; i <= N; i++) nd[i] = FRC(AS[i][K], N).norm();

	for(int cnt = 1; cnt <= N; cnt++) {
		if(cnt == N) {
			for(int i = 1; i <= N; i++) if(!chk[i]) O[N] = i;
			break;
		}
		FRC mnx(K+1);
		int mni = -1;
		for(int i = 1; i <= N; i++) if(!chk[i]) {
			FRC t = nxtF(i, pos[cnt-1], nd[i]).snk();
			if(t.b > MAXX/8) t.a += 3;
			t.norm().snk();
			if(t < mnx) {
				mnx = t;
				mni = i;
			}
		}
		if(mni < 0) {
			puts("FUK");
			exit(-1);
		}
		pos[cnt] = mnx;
		O[cnt] = mni;
		chk[mni] = true;
	}

	for(int i = 1; i < N; i++) pos[i].prt();
	for(int i = 1; i <= N; i++) printf("%d ", O[i]);
	puts("");
	return 0;
}

Compilation message

naan.cpp: In function 'int main()':
naan.cpp:137:7: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  fread(str, 1, 55000055, stdin);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Correct 2 ms 632 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 632 KB Output is correct
18 Correct 2 ms 632 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 632 KB Output is correct
22 Correct 2 ms 632 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 504 KB Output is correct
25 Correct 2 ms 504 KB Output is correct
26 Correct 2 ms 504 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 504 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 2 ms 504 KB Output is correct
24 Correct 2 ms 632 KB Output is correct
25 Correct 2 ms 632 KB Output is correct
26 Correct 2 ms 632 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
28 Correct 2 ms 504 KB Output is correct
29 Correct 2 ms 504 KB Output is correct
30 Correct 2 ms 504 KB Output is correct
31 Correct 2 ms 504 KB Output is correct
32 Correct 2 ms 632 KB Output is correct
33 Correct 2 ms 632 KB Output is correct
34 Correct 2 ms 504 KB Output is correct
35 Correct 2 ms 504 KB Output is correct
36 Correct 2 ms 632 KB Output is correct
37 Correct 2 ms 632 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 504 KB Output is correct
40 Correct 2 ms 504 KB Output is correct
41 Correct 2 ms 504 KB Output is correct
42 Correct 2 ms 504 KB Output is correct
43 Correct 299 ms 9556 KB Output is correct
44 Correct 1848 ms 40100 KB Output is correct
45 Correct 520 ms 24056 KB Output is correct
46 Correct 21 ms 4344 KB Output is correct
47 Correct 1014 ms 31352 KB Output is correct
48 Correct 3363 ms 27516 KB Output is correct
49 Correct 711 ms 14456 KB Output is correct
50 Correct 3899 ms 49276 KB Output is correct
51 Correct 1092 ms 28780 KB Output is correct
52 Correct 2924 ms 47864 KB Output is correct
53 Correct 2842 ms 43076 KB Output is correct
54 Correct 2 ms 504 KB Output is correct
55 Correct 1149 ms 13816 KB Output is correct
56 Correct 1103 ms 35192 KB Output is correct
57 Correct 1134 ms 31248 KB Output is correct
58 Correct 2194 ms 40056 KB Output is correct
59 Correct 1043 ms 31096 KB Output is correct
60 Correct 3636 ms 69456 KB Output is correct
61 Correct 3600 ms 69496 KB Output is correct
62 Correct 3801 ms 69368 KB Output is correct
63 Correct 3872 ms 69752 KB Output is correct
64 Correct 3668 ms 69628 KB Output is correct
65 Correct 2400 ms 56312 KB Output is correct
66 Correct 2362 ms 56568 KB Output is correct
67 Correct 2925 ms 56696 KB Output is correct
68 Correct 1605 ms 35956 KB Output is correct
69 Correct 1536 ms 37880 KB Output is correct
70 Correct 1934 ms 39800 KB Output is correct
71 Correct 2369 ms 49656 KB Output is correct