Submission #67876443


Source Code Expand

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define sz(a) ((int) (a).size())
#define vi vector < int > 
#define pb emplace_back
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
using ll = long long;
#if defined(LOCAL)
#include "debug.cpp"
#else
#define debug(x...) 0
#endif // LOCAL
using namespace std;

const int N = 2e5 + 5;
int n, a[N];

void Main() {
	cin >> n;
	L(i, 0, n - 1) cin >> a[i];
	map<array<int, 2>, int> now = {{{-1, -1}, 0}}, next;
	auto update = [&](array<int, 2> ind, int cost){
		next[ind] = min(next.count(ind) ? next[ind] : int(1e9), cost);
	};
	L(i, 0, n - 1){
		for(auto [cur, cost]:now){
			//debug(cur, cost);
			if(
				i < 2 or 
				cur == array<int, 2>{a[i-2], a[i-1]} or
				cur == array<int, 2>{a[i-3], a[i-1]} or
				cur == array<int, 2>{a[i-1], a[i-2]}
			){
				update({cur[1], a[i]}, cost + (cur[1] != a[i]));
				update({a[i], cur[1]}, cost - (cur[0] != cur[1]) + (cur[0] != a[i]) + (a[i] != cur[1]) + 1);
			}
		}
		swap(now, next);
		next.clear();
	}
	int ans = 1e9;
	for(auto [_, cost]:now) ans = min(ans, cost);
	cout << ans << endl;
}

signed main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t; while(t--) Main();
	return 0;
};

Submission Info

Submission Time
Task D - Swap and Erase
User n1k
Language C++ 20 (gcc 12.2)
Score 700
Code Size 1382 Byte
Status AC
Exec Time 134 ms
Memory 4340 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 1
AC × 60
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 02_tiny_01.txt, 02_tiny_02.txt, 02_tiny_03.txt, 02_tiny_04.txt, 02_tiny_05.txt, 02_tiny_06.txt, 02_tiny_07.txt, 02_tiny_08.txt, 02_tiny_09.txt, 02_tiny_10.txt, 03_small_01.txt, 03_small_02.txt, 03_small_03.txt, 03_small_04.txt, 03_small_05.txt, 04_medium_01.txt, 04_medium_02.txt, 04_medium_03.txt, 04_medium_04.txt, 04_medium_05.txt, 05_large_01.txt, 05_large_02.txt, 05_large_03.txt, 05_large_04.txt, 05_large_05.txt, 06_max_01.txt, 06_max_02.txt, 06_max_03.txt, 06_max_04.txt, 06_max_05.txt, 07_small_few_types_01.txt, 07_small_few_types_02.txt, 07_small_few_types_03.txt, 07_small_few_types_04.txt, 07_small_few_types_05.txt, 08_medium_few_types_01.txt, 08_medium_few_types_02.txt, 08_medium_few_types_03.txt, 08_medium_few_types_04.txt, 08_medium_few_types_05.txt, 09_large_few_types_01.txt, 09_large_few_types_02.txt, 09_large_few_types_03.txt, 09_large_few_types_04.txt, 09_large_few_types_05.txt, 10_max_few_types_01.txt, 10_max_few_types_02.txt, 10_max_few_types_03.txt, 10_max_few_types_04.txt, 10_max_few_types_05.txt, 10_max_few_types_06.txt, 10_max_few_types_07.txt, 10_max_few_types_08.txt, 10_max_few_types_09.txt, 10_max_few_types_10.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 1 ms 3396 KiB
01_handmade_01.txt AC 1 ms 3380 KiB
01_handmade_02.txt AC 134 ms 3260 KiB
01_handmade_03.txt AC 15 ms 4340 KiB
01_handmade_04.txt AC 38 ms 4172 KiB
02_tiny_01.txt AC 55 ms 3448 KiB
02_tiny_02.txt AC 54 ms 3344 KiB
02_tiny_03.txt AC 55 ms 3376 KiB
02_tiny_04.txt AC 54 ms 3452 KiB
02_tiny_05.txt AC 56 ms 3396 KiB
02_tiny_06.txt AC 55 ms 3380 KiB
02_tiny_07.txt AC 55 ms 3344 KiB
02_tiny_08.txt AC 54 ms 3456 KiB
02_tiny_09.txt AC 54 ms 3428 KiB
02_tiny_10.txt AC 55 ms 3356 KiB
03_small_01.txt AC 44 ms 3340 KiB
03_small_02.txt AC 43 ms 3340 KiB
03_small_03.txt AC 44 ms 3336 KiB
03_small_04.txt AC 43 ms 3324 KiB
03_small_05.txt AC 43 ms 3568 KiB
04_medium_01.txt AC 42 ms 3348 KiB
04_medium_02.txt AC 42 ms 3568 KiB
04_medium_03.txt AC 42 ms 3436 KiB
04_medium_04.txt AC 42 ms 3404 KiB
04_medium_05.txt AC 42 ms 3388 KiB
05_large_01.txt AC 13 ms 3720 KiB
05_large_02.txt AC 42 ms 3928 KiB
05_large_03.txt AC 41 ms 3976 KiB
05_large_04.txt AC 43 ms 4100 KiB
05_large_05.txt AC 18 ms 3696 KiB
06_max_01.txt AC 45 ms 4160 KiB
06_max_02.txt AC 44 ms 4120 KiB
06_max_03.txt AC 44 ms 4228 KiB
06_max_04.txt AC 44 ms 4224 KiB
06_max_05.txt AC 44 ms 4168 KiB
07_small_few_types_01.txt AC 40 ms 3260 KiB
07_small_few_types_02.txt AC 40 ms 3568 KiB
07_small_few_types_03.txt AC 40 ms 3348 KiB
07_small_few_types_04.txt AC 40 ms 3444 KiB
07_small_few_types_05.txt AC 40 ms 3416 KiB
08_medium_few_types_01.txt AC 36 ms 3364 KiB
08_medium_few_types_02.txt AC 36 ms 3332 KiB
08_medium_few_types_03.txt AC 36 ms 3268 KiB
08_medium_few_types_04.txt AC 36 ms 3356 KiB
08_medium_few_types_05.txt AC 35 ms 3388 KiB
09_large_few_types_01.txt AC 29 ms 3640 KiB
09_large_few_types_02.txt AC 33 ms 3668 KiB
09_large_few_types_03.txt AC 35 ms 4092 KiB
09_large_few_types_04.txt AC 39 ms 3824 KiB
09_large_few_types_05.txt AC 19 ms 3804 KiB
10_max_few_types_01.txt AC 25 ms 4336 KiB
10_max_few_types_02.txt AC 31 ms 4168 KiB
10_max_few_types_03.txt AC 33 ms 4200 KiB
10_max_few_types_04.txt AC 35 ms 4120 KiB
10_max_few_types_05.txt AC 35 ms 4116 KiB
10_max_few_types_06.txt AC 36 ms 4128 KiB
10_max_few_types_07.txt AC 37 ms 4096 KiB
10_max_few_types_08.txt AC 37 ms 4120 KiB
10_max_few_types_09.txt AC 38 ms 4168 KiB
10_max_few_types_10.txt AC 38 ms 4100 KiB