CSES - HIIT Open 2024 - Results
Submission details
Task:Forgotten measurements
Sender:Aalto CS-A1140 Team 2
Submission time:2024-11-16 14:07:35 +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.02 sdetails
#70.02 sdetails
#80.02 sdetails
#90.02 sdetails
#100.02 sdetails
#110.02 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();
		int popcnt = 0;
		for (int i = 0; i < n; ++i) popcnt += x[i];
		int i = 0;
		while (x[i] == x[(i + 1) % n]) i = (i + 1) % n;
		i = (i + 1) % n;
		int c = 0;
		int z = x[i];
		while (x[i] == z) {
			i = (i + 1) % n;
			c++;
		}
		if (c == popcnt || n - c == popcnt) {
			return min(popcnt, n - popcnt);
		}
		return n - 1;
	};
	cout << solve(ys) + solve(xs) << 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
14

Test 4

Verdict:

input
LDLDRDLULULURURURULDRDLDRDRURD...

correct output
19

user output
38

Test 5

Verdict:

input
RURULDLULDRURDLULDRULURULURDLD...

correct output
470

user output
994

Test 6

Verdict:

input
DRULDLULDLULDLURDRULDRURDLURDL...

correct output
499773

user output
999982

Test 7

Verdict:

input
DLURDLULDLULDRULURULURURDLDLDR...

correct output
499383

user output
999982

Test 8

Verdict:

input
RULDLDLDRULDLDLULURULULDLURDLU...

correct output
499622

user output
999986

Test 9

Verdict:

input
RDRDRURDLDLURDRULURDLDRULDLULU...

correct output
499788

user output
999994

Test 10

Verdict:

input
LDLURDRURDRULDLULDLDRURDLULDRU...

correct output
499368

user output
999986

Test 11

Verdict:

input
DRULULDRDLDLDLURULDRDLURULULDR...

correct output
499790

user output
999986