Submission #497559

# Submission time Handle Problem Language Result Execution time Memory
497559 2021-12-23T09:36:03 Z Ziel Coin Collecting (JOI19_ho_t4) C++17
100 / 100
86 ms 14076 KB
/**
 * LES GREATEABLES BRO TEAM
**/
 
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
 
string to_string(const string s) {
	return '"' + s + '"';
}
string to_string(const char c) {
 	return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
 	return to_string(string(s));
}
string to_string(bool f) {
	return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
	return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
	return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
	return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
	string res = "{"; bool f = true;
	for (auto x: v)
		res += (f ? to_string(x) : ", " + to_string(x)), f = false;
	return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
	cerr << to_string(x);
	if (sizeof... (y))
	cerr << ", ";
	debug_args(y...);
}
 
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
 
#define int ll

const int N = 1e6 + 12;
 
int a[N], cnt[N][5];
 
void solve() {
    int n;
    cin >> n;
    ll ans = 0;
    for (int i = 0; i < n + n; i++) {
    	int x, y;
    	cin >> x >> y;

    	if (1 <= x && x <= n && 1 <= y && y <= 2) {
    		cnt[x][y]++;
    		a[i] = 0;
    	} else if (1 <= x && x <= n) {
    		if (y < 1) {
    			cnt[x][1]++;
    			a[i] = abs(y - 1);
    		} else {
    			cnt[x][2]++;
    			a[i] = abs(y - 2);
    		}
    	} else if (1 <= y && y <= 2) {
    		if (x < 1) {
    			cnt[1][y]++;
    			a[i] = abs(x - 1);
    		} else {
    			cnt[n][y]++;
    			a[i] = abs(x - n);
    		}
    	} else {
    		if (x < 1 && y < 1) {
    			cnt[1][1]++;
    			a[i] = abs(x - 1) + abs(y - 1);
    		} else if (x < 1 && y > 2) {
    			cnt[1][2]++;
    			a[i] = abs(x - 1) + abs(y - 2);
    		} else if (x > n && y < 1) {
    			cnt[n][1]++;
    			a[i] = abs(x - n) + abs(y - 1);
    		} else {
    			cnt[n][2]++;
    			a[i] = abs(x - n) + abs(y - 2);
    		}
    	}

    	ans += a[i];
    }

    set<int> l1, l2;
	for (int i = 1; i <= n; i++) {
		while (sz(l1) && cnt[i][1] > 1) {
			ans += abs((*l1.begin()) - i);
			cnt[i][1]--;
			l1.erase(l1.begin());
		}
		while (sz(l2) && cnt[i][2] > 1) {
			ans += abs((*l2.begin()) - i);
			cnt[i][2]--;
			l2.erase(l2.begin());
		}


		while (sz(l2) && cnt[i][1] > 1) {
			ans += abs((*l2.begin()) - i) + 1;
			cnt[i][1]--;
			l2.erase(l2.begin());
		}
		while (sz(l1) && cnt[i][2] > 1) {
			ans += abs((*l1.begin()) - i) + 1;
			cnt[i][2]--;
			l1.erase(l1.begin());
		}

		if (cnt[i][1] > 1 && cnt[i][2] == 0) {
			ans++;
			cnt[i][2]++;
			cnt[i][1]--;
		}
		if (cnt[i][2] > 1 && cnt[i][1] == 0) {
			ans++;
			cnt[i][1]++;
			cnt[i][2]--;
		}


		if (cnt[i][1] > 1) {
			ans += (cnt[i][1] - 1);
			cnt[i + 1][1] += (cnt[i][1] - 1);
			cnt[i][1] = 1;
		}
		if (cnt[i][2] > 1) {
			ans += (cnt[i][2] - 1);
			cnt[i + 1][2] += (cnt[i][2] - 1);
			cnt[i][2] = 1;
		}

		if (cnt[i][1] == 0)
			l1.insert(i);
		if (cnt[i][2] == 0)
			l2.insert(i);
	}
	
	cout << ans;
}
 
signed main() {
    setIO();
    
    int tt = 1;
    if (FLAG) {
    	cin >> tt;
    }
    while (tt--) {
    	solve();
    }
    
    return 0;
}
 
void setIO(const string &f) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen((f + ".in").c_str(), "r")) {
        freopen((f + ".in").c_str(), "r", stdin);
        freopen((f + ".out").c_str(), "w", stdout);
    }
}

Compilation message

joi2019_ho_t4.cpp: In function 'void setIO(const string&)':
joi2019_ho_t4.cpp:187:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t4.cpp:188:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  188 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 316 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 324 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 0 ms 320 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 316 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 316 KB Output is correct
33 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 316 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 324 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 0 ms 320 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 316 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 316 KB Output is correct
33 Correct 1 ms 316 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 460 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 1 ms 372 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 2 ms 332 KB Output is correct
49 Correct 2 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 392 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 400 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 1 ms 328 KB Output is correct
57 Correct 1 ms 460 KB Output is correct
58 Correct 1 ms 328 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 1 ms 456 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 460 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 316 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 324 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 0 ms 320 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 316 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 316 KB Output is correct
33 Correct 1 ms 316 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 460 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 1 ms 372 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 2 ms 332 KB Output is correct
49 Correct 2 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 392 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 400 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 1 ms 328 KB Output is correct
57 Correct 1 ms 460 KB Output is correct
58 Correct 1 ms 328 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 1 ms 456 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 460 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 28 ms 7236 KB Output is correct
66 Correct 55 ms 10352 KB Output is correct
67 Correct 50 ms 8860 KB Output is correct
68 Correct 58 ms 8864 KB Output is correct
69 Correct 59 ms 8900 KB Output is correct
70 Correct 61 ms 8968 KB Output is correct
71 Correct 51 ms 8872 KB Output is correct
72 Correct 53 ms 10812 KB Output is correct
73 Correct 86 ms 14076 KB Output is correct
74 Correct 59 ms 9608 KB Output is correct
75 Correct 45 ms 9636 KB Output is correct
76 Correct 49 ms 9884 KB Output is correct
77 Correct 70 ms 10444 KB Output is correct
78 Correct 46 ms 9544 KB Output is correct
79 Correct 63 ms 9256 KB Output is correct
80 Correct 75 ms 9392 KB Output is correct
81 Correct 47 ms 9628 KB Output is correct
82 Correct 56 ms 8248 KB Output is correct
83 Correct 67 ms 8372 KB Output is correct
84 Correct 45 ms 8540 KB Output is correct
85 Correct 44 ms 8796 KB Output is correct
86 Correct 48 ms 8740 KB Output is correct
87 Correct 57 ms 8200 KB Output is correct
88 Correct 52 ms 8892 KB Output is correct
89 Correct 60 ms 9032 KB Output is correct
90 Correct 68 ms 8996 KB Output is correct
91 Correct 60 ms 8896 KB Output is correct
92 Correct 71 ms 8992 KB Output is correct
93 Correct 66 ms 9028 KB Output is correct
94 Correct 52 ms 8812 KB Output is correct
95 Correct 50 ms 8772 KB Output is correct
96 Correct 73 ms 13812 KB Output is correct
97 Correct 71 ms 13816 KB Output is correct
98 Correct 45 ms 9412 KB Output is correct
99 Correct 65 ms 11448 KB Output is correct
100 Correct 55 ms 10548 KB Output is correct
101 Correct 63 ms 13000 KB Output is correct
102 Correct 45 ms 9564 KB Output is correct
103 Correct 48 ms 10072 KB Output is correct