Programming

[CF] Educational Round 98 (Div. 2) _ 201119

minigb 2021. 4. 13. 22:09

(2020년 11월 20일에 작성한 글입니다.)

 

 

Dashboard - Educational Codeforces Round 98 (Rated for Div. 2) - Codeforces

 

codeforces.com

 

A.

일단 머물지 않고 최대로 간 다음에

그 뒤로는 한 칸 이동하고 머물고...를 반복하면 된다.

if(X == Y) ans = 2 * X;

else ans = min(X, Y) * 2 + (max(X, Y) - min(X * Y)) * 2 - 1인데

결국 ans = max(X, Y) * 2 - 1이구나

그러하다

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int T;
	int X, Y;
	int ans;
	int i;

	cin >> T;
	while (T--) {
		cin >> X >> Y;
		ans = 0;

		if (X == Y) {
			ans = X + Y;
		}
		else if(abs(X - Y) == 1) {
			ans = X + Y;
		}
		else {
			ans = min(X, Y) * 2 + abs(X - Y) * 2 - 1;
		}

		cout << ans << '\n';
	}
	return 0;
}

C.

B가 어려워 보였는데 C를 푼 사람이 더 많길래 이 문제부터 풀었다.

너무 쉬웠당

stack인데 굳이 stack을 이용할 필요는 없는.

그냥 cnt만 사용하면 되는 문제

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int T;
	string s;
	int len;
	int ans;
	int cnt1, cnt2;
	int i;

	cin >> T;
	while (T--) {
		cin >> s;
		len = s.length();
		cnt1 = cnt2 = 0;
		ans = 0;
		for (i = 0; i < len; i++) {
			if (s[i] == '(') {
				cnt1++;
			}
			else if (s[i] == '[') {
				cnt2++;
			}
			else if (s[i] == ')') {
				if (cnt1 > 0) {
					cnt1--;
					ans++;
				}
			}
			else {
				if (cnt2 > 0) {
					cnt2--;
					ans++;
				}
			}
		}

		cout << ans << '\n';
	}
	return 0;
}

B.

하... 세 번이나 틀렸다

일단 모든 수들의 합이 (n - 1)의 배수가 되야되니까

max값을 찾아서

max * (n - 1)이랑 sum을 비교한다.

max * (n - 1) >= sum이면 max * (n - 1) - sum을 출력하면 되고

max * (n - 1) < sum이면 sum 이상의 (n - 1)의 배수에서 sum을 빼면 된다.

근데 내가 이거 잘못해서 sum이 (n - 1)의 배수일 때 n - 1을 출력하게 했고

그래서 틀렸다...ㅠㅠㅠ

그리고 그걸 잘못했을 때 n == 2일때 0이 안 나와서

n == 2일때 0을 출력하도록 했는데

사실 그럴 필요가 없다... 제대로 풀면 그건 어차피 걸러지게 되어 있어서...

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int T;
	ll N;
	ll temp;
	ll sum, max_;
	ll i;

	cin >> T;
	while (T--) {
		cin >> N;
		sum = max_ = 0;
		for (i = 0; i < N; i++) {
			cin >> temp;
			max_ = max(max_, temp);
			sum += temp;
		}

		if (N == 2) {
			cout << 0 << '\n';
			continue;
		}

		if (max_ * (N - 1) >= sum) {
			cout << max_ * (N - 1) - sum << '\n';
		}
		else {
			if (sum % (N - 1)) {
				cout << (N - 1) - sum % (N - 1) << '\n';
			}
			else {
				cout << 0 << '\n';
			}
		}

	}
	return 0;
}