CSES - HIIT Open 2024 - Results
Submission details
Task:Forgotten measurements
Sender:Aalto CS-A1140 Team 2
Submission time:2024-11-16 14:50:11 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#30.00 sdetails
#40.00 sdetails
#50.00 sdetails
#60.03 sdetails
#70.03 sdetails
#80.03 sdetails
#90.03 sdetails
#100.03 sdetails
#110.03 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
	string s;
	cin >> s;
	vector<int> ys, xs;
	for (char c : s) {
		if (c == 'U' || c == 'D') ys.push_back(c == 'U');
		if (c == 'L' || c == 'R') xs.push_back(c == 'L');
	}
	auto solve = [&](vector<int> x) {
		int n = (int)x.size();
		{
			vi y = x;
			int i = 0;
			while (x[i] == x[(i + 1) % n]) i = (i + 1) % n;
			i = (i + 1) % n;
			for (int j = 0; j < n; ++j) {
				y[j] = x[(j + i) % n];
			}
			x = y;
		}
		int mx = 0;
		for (int i = 0, j = 0; i < n; i = j) {
			while (j < n && x[i] == x[j]) ++j;
			mx = max(mx, j - i);
		}
		return n - mx;
	};
	cout << solve(xs) + solve(ys) << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
ULDRUR

correct output
2

user output
2

Test 2

Verdict: ACCEPTED

input
RULD

correct output
2

user output
2

Test 3

Verdict:

input
RULDRURDRURDLULD

correct output
7

user output
11

Test 4

Verdict:

input
LDLDRDLULULURURURULDRDLDRDRURD...

correct output
19

user output
31

Test 5

Verdict:

input
RURULDLULDRURDLULDRULURULURDLD...

correct output
470

user output
980

Test 6

Verdict:

input
DRULDLULDLULDLURDRULDRURDLURDL...

correct output
499773

user output
999955

Test 7

Verdict:

input
DLURDLULDLULDRULURULURURDLDLDR...

correct output
499383

user output
999954

Test 8

Verdict:

input
RULDLDLDRULDLDLULURULULDLURDLU...

correct output
499622

user output
999961

Test 9

Verdict:

input
RDRDRURDLDLURDRULURDLDRULDLULU...

correct output
499788

user output
999967

Test 10

Verdict:

input
LDLURDRURDRULDLULDLDRURDLULDRU...

correct output
499368

user output
999962

Test 11

Verdict:

input
DRULULDRDLDLDLURULDRDLURULULDR...

correct output
499790

user output
999954