Submission #128796

# Submission time Handle Problem Language Result Execution time Memory
128796 2019-07-11T09:34:47 Z imyujin None (JOI16_ho_t4) C++14
100 / 100
308 ms 56268 KB
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

#define MAXN 100005
#define fi first
#define se second

typedef long long lint;
typedef pair<lint, lint> pll;

const lint LINF = 1ll << 50;
const int mm[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
char S[MAXN];
vector<pll> dot;
vector<pll> ty, ty2;
vector<lint> ds[MAXN];
vector<pll> cell, cs[4 * MAXN];
vector<lint> done[4 * MAXN];
lint A, B, ABs;

pll f(pll &a) {
	return make_pair(B * a.fi - A * a.se, ((A * a.fi + B * a.se) % ABs + ABs) % ABs);
}

lint apbdis(pll &a, pll &b) {	//a < b
	return (A * (b.fi - a.fi) + B * (b.se - a.se)) / ABs;
}

int main() {
	int N, K;
	lint ans = 0ll;

	scanf("%d%d%s", &N, &K, S);

	dot.push_back(make_pair(0ll, 0ll));
	for(int i = 0; i < N; i++) {
		dot.push_back(dot.back());
		if(S[i] == 'W') dot[i + 1].fi--;
		else if(S[i] == 'E') dot[i + 1].fi++;
		else if(S[i] == 'N') dot[i + 1].se++;
		else dot[i + 1].se--;
	}
	for(int i = 0; i <= N; i++) for(int j = 0; j < 4; j++) 
		cell.push_back(make_pair(dot[i].fi - mm[j][0], dot[i].se - mm[j][1]));

	A = dot[N].fi;
	B = dot[N].se;
	ABs = A * A + B * B;
	if(A == 0 && B == 0) {
		sort(dot.begin(), dot.end());
		dot.resize(unique(dot.begin(), dot.end()) - dot.begin());
		sort(cell.begin(), cell.end());
		cell.resize(unique(cell.begin(), cell.end()) - cell.begin());

		int ans = 0;
		for(auto &a : cell) {
			bool b = true;
			for(int j = 0; j < 4; j++) b &= binary_search(dot.begin(), dot.end(), make_pair(a.fi + mm[j][0], a.se + mm[j][1]));
			if(b) ans++;
		}
		printf("%d", ans);
		return 0;
	}

	for(auto &a : dot) ty.push_back(f(a));
	sort(ty.begin(), ty.end());
	ty.resize(unique(ty.begin(), ty.end()) - ty.begin());
	for(auto &a : dot) {
		int l = lower_bound(ty.begin(), ty.end(), f(a)) - ty.begin();
		ds[l].push_back(A * a.fi + B * a.se);
	}
	for(int i = 0; i < ty.size(); i++) {
		sort(ds[i].begin(), ds[i].end());
		ds[i].resize(unique(ds[i].begin(), ds[i].end()) - ds[i].begin());
	}

	for(auto &a : cell) ty2.push_back(f(a));
	sort(ty2.begin(), ty2.end());
	ty2.resize(unique(ty2.begin(), ty2.end()) - ty2.begin());
	for(auto &a : cell) {
		int l = lower_bound(ty2.begin(), ty2.end(), f(a)) - ty2.begin();
		cs[l].push_back(a);
	}
	for(int i = 0; i < ty2.size(); i++) {
		sort(cs[i].begin(), cs[i].end(), [&](pll a, pll b) { return A * a.fi + B * a.se < A * b.fi + B * b.se; } );
		cs[i].resize(unique(cs[i].begin(), cs[i].end()) - cs[i].begin());
	}

	for(int i = 0; i < ty2.size(); i++) for(int j = 0; j < cs[i].size(); j++) {
		done[i].push_back(0ll);
		for(int k = 0; k < 4; k++) {
			lint x = cs[i][j].fi + mm[k][0], y = cs[i][j].se + mm[k][1];
			pll nd = make_pair(x, y);
			int l = lower_bound(ty.begin(), ty.end(), f(nd)) - ty.begin();
			if(l == ty.size() || ty[l] != f(nd)) {
				done[i][j] = K;
				break;
			}
			lint ApB = A * x + B * y;
			int ll = upper_bound(ds[l].begin(), ds[l].end(), ApB) - ds[l].begin();
			if(ll == 0) {
				done[i][j] = K;
				break;
			}
			done[i][j] = max(done[i][j], (ApB - ds[l][ll - 1]) / ABs);
		}
		if(j > 0) done[i][j] = min(done[i][j - 1] + apbdis(cs[i][j - 1], cs[i][j]), done[i][j]);
		done[i][j] = min(done[i][j], (lint) K);
		ans += (j == 0 ? K : min(done[i][j - 1] + apbdis(cs[i][j - 1], cs[i][j]), (lint) K)) - done[i][j];
	}

	printf("%lld", ans);

	return 0;
}

Compilation message

2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ty.size(); i++) {
                 ~~^~~~~~~~~~~
2016_ho_t4.cpp:87:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ty2.size(); i++) {
                 ~~^~~~~~~~~~~~
2016_ho_t4.cpp:92:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ty2.size(); i++) for(int j = 0; j < cs[i].size(); j++) {
                 ~~^~~~~~~~~~~~
2016_ho_t4.cpp:92:55: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ty2.size(); i++) for(int j = 0; j < cs[i].size(); j++) {
                                                     ~~^~~~~~~~~~~~~~
2016_ho_t4.cpp:98:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if(l == ty.size() || ty[l] != f(nd)) {
       ~~^~~~~~~~~~~~
2016_ho_t4.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%s", &N, &K, S);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 21368 KB Output is correct
2 Correct 21 ms 21496 KB Output is correct
3 Correct 21 ms 21496 KB Output is correct
4 Correct 22 ms 21496 KB Output is correct
5 Correct 21 ms 21368 KB Output is correct
6 Correct 21 ms 21496 KB Output is correct
7 Correct 22 ms 21432 KB Output is correct
8 Correct 21 ms 21496 KB Output is correct
9 Correct 21 ms 21496 KB Output is correct
10 Correct 21 ms 21496 KB Output is correct
11 Correct 21 ms 21496 KB Output is correct
12 Correct 21 ms 21496 KB Output is correct
13 Correct 21 ms 21496 KB Output is correct
14 Correct 21 ms 21496 KB Output is correct
15 Correct 21 ms 21496 KB Output is correct
16 Correct 21 ms 21500 KB Output is correct
17 Correct 22 ms 21496 KB Output is correct
18 Correct 21 ms 21496 KB Output is correct
19 Correct 21 ms 21496 KB Output is correct
20 Correct 21 ms 21496 KB Output is correct
21 Correct 21 ms 21496 KB Output is correct
22 Correct 21 ms 21544 KB Output is correct
23 Correct 22 ms 21496 KB Output is correct
24 Correct 21 ms 21480 KB Output is correct
25 Correct 21 ms 21496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 21368 KB Output is correct
2 Correct 21 ms 21496 KB Output is correct
3 Correct 21 ms 21496 KB Output is correct
4 Correct 22 ms 21496 KB Output is correct
5 Correct 21 ms 21368 KB Output is correct
6 Correct 21 ms 21496 KB Output is correct
7 Correct 22 ms 21432 KB Output is correct
8 Correct 21 ms 21496 KB Output is correct
9 Correct 21 ms 21496 KB Output is correct
10 Correct 21 ms 21496 KB Output is correct
11 Correct 21 ms 21496 KB Output is correct
12 Correct 21 ms 21496 KB Output is correct
13 Correct 21 ms 21496 KB Output is correct
14 Correct 21 ms 21496 KB Output is correct
15 Correct 21 ms 21496 KB Output is correct
16 Correct 21 ms 21500 KB Output is correct
17 Correct 22 ms 21496 KB Output is correct
18 Correct 21 ms 21496 KB Output is correct
19 Correct 21 ms 21496 KB Output is correct
20 Correct 21 ms 21496 KB Output is correct
21 Correct 21 ms 21496 KB Output is correct
22 Correct 21 ms 21544 KB Output is correct
23 Correct 22 ms 21496 KB Output is correct
24 Correct 21 ms 21480 KB Output is correct
25 Correct 21 ms 21496 KB Output is correct
26 Correct 22 ms 21496 KB Output is correct
27 Correct 29 ms 22928 KB Output is correct
28 Correct 30 ms 22896 KB Output is correct
29 Correct 115 ms 35340 KB Output is correct
30 Correct 155 ms 41936 KB Output is correct
31 Correct 254 ms 54340 KB Output is correct
32 Correct 85 ms 31580 KB Output is correct
33 Correct 212 ms 48944 KB Output is correct
34 Correct 211 ms 49076 KB Output is correct
35 Correct 210 ms 48728 KB Output is correct
36 Correct 199 ms 48216 KB Output is correct
37 Correct 95 ms 31580 KB Output is correct
38 Correct 206 ms 48664 KB Output is correct
39 Correct 199 ms 48848 KB Output is correct
40 Correct 86 ms 31580 KB Output is correct
41 Correct 182 ms 48080 KB Output is correct
42 Correct 185 ms 48176 KB Output is correct
43 Correct 213 ms 48736 KB Output is correct
44 Correct 207 ms 48820 KB Output is correct
45 Correct 201 ms 48592 KB Output is correct
46 Correct 213 ms 47312 KB Output is correct
47 Correct 231 ms 47568 KB Output is correct
48 Correct 201 ms 49720 KB Output is correct
49 Correct 263 ms 56268 KB Output is correct
50 Correct 259 ms 56120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 21368 KB Output is correct
2 Correct 21 ms 21496 KB Output is correct
3 Correct 21 ms 21496 KB Output is correct
4 Correct 22 ms 21496 KB Output is correct
5 Correct 21 ms 21368 KB Output is correct
6 Correct 21 ms 21496 KB Output is correct
7 Correct 22 ms 21432 KB Output is correct
8 Correct 21 ms 21496 KB Output is correct
9 Correct 21 ms 21496 KB Output is correct
10 Correct 21 ms 21496 KB Output is correct
11 Correct 21 ms 21496 KB Output is correct
12 Correct 21 ms 21496 KB Output is correct
13 Correct 21 ms 21496 KB Output is correct
14 Correct 21 ms 21496 KB Output is correct
15 Correct 21 ms 21496 KB Output is correct
16 Correct 21 ms 21500 KB Output is correct
17 Correct 22 ms 21496 KB Output is correct
18 Correct 21 ms 21496 KB Output is correct
19 Correct 21 ms 21496 KB Output is correct
20 Correct 21 ms 21496 KB Output is correct
21 Correct 21 ms 21496 KB Output is correct
22 Correct 21 ms 21544 KB Output is correct
23 Correct 22 ms 21496 KB Output is correct
24 Correct 21 ms 21480 KB Output is correct
25 Correct 21 ms 21496 KB Output is correct
26 Correct 25 ms 21496 KB Output is correct
27 Correct 23 ms 21496 KB Output is correct
28 Correct 25 ms 21512 KB Output is correct
29 Correct 21 ms 21416 KB Output is correct
30 Correct 21 ms 21496 KB Output is correct
31 Correct 22 ms 21496 KB Output is correct
32 Correct 21 ms 21496 KB Output is correct
33 Correct 22 ms 21496 KB Output is correct
34 Correct 25 ms 21496 KB Output is correct
35 Correct 22 ms 21496 KB Output is correct
36 Correct 21 ms 21496 KB Output is correct
37 Correct 21 ms 21524 KB Output is correct
38 Correct 21 ms 21496 KB Output is correct
39 Correct 26 ms 21496 KB Output is correct
40 Correct 20 ms 21496 KB Output is correct
41 Correct 21 ms 21496 KB Output is correct
42 Correct 21 ms 21496 KB Output is correct
43 Correct 21 ms 21496 KB Output is correct
44 Correct 21 ms 21500 KB Output is correct
45 Correct 21 ms 21496 KB Output is correct
46 Correct 21 ms 21468 KB Output is correct
47 Correct 22 ms 21496 KB Output is correct
48 Correct 26 ms 21496 KB Output is correct
49 Correct 21 ms 21496 KB Output is correct
50 Correct 23 ms 21496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 21368 KB Output is correct
2 Correct 21 ms 21496 KB Output is correct
3 Correct 21 ms 21496 KB Output is correct
4 Correct 22 ms 21496 KB Output is correct
5 Correct 21 ms 21368 KB Output is correct
6 Correct 21 ms 21496 KB Output is correct
7 Correct 22 ms 21432 KB Output is correct
8 Correct 21 ms 21496 KB Output is correct
9 Correct 21 ms 21496 KB Output is correct
10 Correct 21 ms 21496 KB Output is correct
11 Correct 21 ms 21496 KB Output is correct
12 Correct 21 ms 21496 KB Output is correct
13 Correct 21 ms 21496 KB Output is correct
14 Correct 21 ms 21496 KB Output is correct
15 Correct 21 ms 21496 KB Output is correct
16 Correct 21 ms 21500 KB Output is correct
17 Correct 22 ms 21496 KB Output is correct
18 Correct 21 ms 21496 KB Output is correct
19 Correct 21 ms 21496 KB Output is correct
20 Correct 21 ms 21496 KB Output is correct
21 Correct 21 ms 21496 KB Output is correct
22 Correct 21 ms 21544 KB Output is correct
23 Correct 22 ms 21496 KB Output is correct
24 Correct 21 ms 21480 KB Output is correct
25 Correct 21 ms 21496 KB Output is correct
26 Correct 22 ms 21496 KB Output is correct
27 Correct 29 ms 22928 KB Output is correct
28 Correct 30 ms 22896 KB Output is correct
29 Correct 115 ms 35340 KB Output is correct
30 Correct 155 ms 41936 KB Output is correct
31 Correct 254 ms 54340 KB Output is correct
32 Correct 85 ms 31580 KB Output is correct
33 Correct 212 ms 48944 KB Output is correct
34 Correct 211 ms 49076 KB Output is correct
35 Correct 210 ms 48728 KB Output is correct
36 Correct 199 ms 48216 KB Output is correct
37 Correct 95 ms 31580 KB Output is correct
38 Correct 206 ms 48664 KB Output is correct
39 Correct 199 ms 48848 KB Output is correct
40 Correct 86 ms 31580 KB Output is correct
41 Correct 182 ms 48080 KB Output is correct
42 Correct 185 ms 48176 KB Output is correct
43 Correct 213 ms 48736 KB Output is correct
44 Correct 207 ms 48820 KB Output is correct
45 Correct 201 ms 48592 KB Output is correct
46 Correct 213 ms 47312 KB Output is correct
47 Correct 231 ms 47568 KB Output is correct
48 Correct 201 ms 49720 KB Output is correct
49 Correct 263 ms 56268 KB Output is correct
50 Correct 259 ms 56120 KB Output is correct
51 Correct 25 ms 21496 KB Output is correct
52 Correct 23 ms 21496 KB Output is correct
53 Correct 25 ms 21512 KB Output is correct
54 Correct 21 ms 21416 KB Output is correct
55 Correct 21 ms 21496 KB Output is correct
56 Correct 22 ms 21496 KB Output is correct
57 Correct 21 ms 21496 KB Output is correct
58 Correct 22 ms 21496 KB Output is correct
59 Correct 25 ms 21496 KB Output is correct
60 Correct 22 ms 21496 KB Output is correct
61 Correct 21 ms 21496 KB Output is correct
62 Correct 21 ms 21524 KB Output is correct
63 Correct 21 ms 21496 KB Output is correct
64 Correct 26 ms 21496 KB Output is correct
65 Correct 20 ms 21496 KB Output is correct
66 Correct 21 ms 21496 KB Output is correct
67 Correct 21 ms 21496 KB Output is correct
68 Correct 21 ms 21496 KB Output is correct
69 Correct 21 ms 21500 KB Output is correct
70 Correct 21 ms 21496 KB Output is correct
71 Correct 21 ms 21468 KB Output is correct
72 Correct 22 ms 21496 KB Output is correct
73 Correct 26 ms 21496 KB Output is correct
74 Correct 21 ms 21496 KB Output is correct
75 Correct 23 ms 21496 KB Output is correct
76 Correct 34 ms 23552 KB Output is correct
77 Correct 25 ms 22260 KB Output is correct
78 Correct 67 ms 27192 KB Output is correct
79 Correct 149 ms 39892 KB Output is correct
80 Correct 157 ms 43600 KB Output is correct
81 Correct 88 ms 31588 KB Output is correct
82 Correct 212 ms 48704 KB Output is correct
83 Correct 180 ms 48220 KB Output is correct
84 Correct 214 ms 48728 KB Output is correct
85 Correct 207 ms 48812 KB Output is correct
86 Correct 272 ms 48692 KB Output is correct
87 Correct 211 ms 49104 KB Output is correct
88 Correct 201 ms 48656 KB Output is correct
89 Correct 205 ms 48592 KB Output is correct
90 Correct 87 ms 31580 KB Output is correct
91 Correct 205 ms 48908 KB Output is correct
92 Correct 212 ms 48976 KB Output is correct
93 Correct 217 ms 48880 KB Output is correct
94 Correct 191 ms 48340 KB Output is correct
95 Correct 193 ms 48268 KB Output is correct
96 Correct 212 ms 47132 KB Output is correct
97 Correct 232 ms 47620 KB Output is correct
98 Correct 198 ms 49668 KB Output is correct
99 Correct 270 ms 56188 KB Output is correct
100 Correct 308 ms 51520 KB Output is correct