Algorithm/코드포스

Good Bye 2021: 2022 is NEAR 1687 -> 1599

Edyy 2022. 1. 1. 22:33

Dashboard - Good Bye 2021: 2022 is NEAR - Codeforces

 

Dashboard - Good Bye 2021: 2022 is NEAR - Codeforces

 

codeforces.com

 

!!!! 모든 답은 제 풀이일 뿐 정해가 아닐 수 있습니다 !!!!

 

A. Integer Diversity

Problem - A - Codeforces

문제

 

n개의 수가 주어진다. 원하는 원소를골라 x -> -x를 바꾸는 연산을 원하는 만큼 할 수 있을때 서로 다른 숫자의 종류의 갯수 최대 값을 출력하는 문제

 

풀이

 

정답에 각 수에 절대 값을 씌워 min(cnt[원소],2)를 더해주고 0은 min(cnt[0],1)을 더해주면 된다.

 

코드

 

#include <bits/stdc++.h>
using namespace std;
int cnt[210];
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int T;
    cin >> T;
    while (T--) {
        for (int i = 0; i < 110; i++)cnt[i] = 0;
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            int t;
            cin >> t;
            if (t < 0)cnt[-t]++;
            else cnt[t]++;
        }
        int ans = min(cnt[0],1);
        for (int i = 1; i < 101; i++) {
            if (cnt[i]) {
                ans += min(cnt[i], 2);
           }
        }
        cout << ans << '\n';
    }
}

 

 

B. Mirror in the String

Problem - B - Codeforces

 

문제

 

string s가 주어진다. 원하는 인덱스 k (1<=k<=n)을 골라 s1s2s3...sksksk-1sk-2...s1으로 스트링을 만들 때 만들 수 있는 모든 문자열 중 사전순으로 가장 작은 문자열을 출력하는 문제

 

풀이

 

s[0]==s[1] 이라면 그대로 s[0]s[1]을 출력한다. , 아니라면 s[0]<s[1]일땐 s[0][s[0]출력,  s[0] > s[1]일때는 이후 s[i-1] >= s[i] 인 구간이 아닐때까지 계속 i를 증가시킨 후 i를 k로 잡고 출력한다.

 

코드

 

#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n >> s;
        string temp = "";
        temp += s[0];
        if (n == 1) {
            cout << temp <<temp<< '\n';
            continue;
        }
        if (s[0] == s[1]) {
            cout << s[0] << s[1] << '\n';
            continue;
        }
        int k = -1;
        for (int i = 1; i < n; i++) {
            if (s[i-1] >= s[i]) {
                temp += s[i];
            }
            else break;
        }
        cout << temp;
        reverse(temp.begin(), temp.end());
        cout << temp << '\n';
    }
}

 

 

자세한 설명에 중점을 두기보다는 대회 기록에 중점을 둔 글입니다 !

틀린 부분은 감사히 지적받겠습니다.