• toc {:toc}

문제 ν™•μΈν•˜κΈ°

풀이

문제 이해가 쉽사리 λ˜μ§€ μ•Šμ§€λ§Œ μ°¨κ·Όμ°¨κ·Ό 읽어보면 두 μ‹œν—˜ 성적 λ“±μˆ˜κ°€ μ£Όμ–΄μ§€κ³ , μ–΄λ–€ μ§€μ›μž A, B λ₯Ό λΉ„κ΅ν–ˆμ„ λ•Œ A 의 λ“±μˆ˜κ°€ 1, 4 이고 B 의 λ“±μˆ˜κ°€ 2, 5 인 경우 B λŠ” 합격할 수 μ—†λ‹€. μž…λ ₯은 μ„œλ₯˜ 심사 λ“±μˆ˜, λ©΄μ ‘μ‹œν—˜ λ“±μˆ˜ 순으둜 λ“€μ–΄μ˜¨λ‹€.

  • 두 μ‹œν—˜ 성적을 λͺ¨λ‘ 비ꡐ해야 ν•œλ‹€. β‡’ λ³΅μž‘ν•˜λ‹€.
    • ν•˜λ‚˜λ₯Ό κ³ μ •μ‹œν‚€μž. β‡’ μ„œλ₯˜ 심사 λ“±μˆ˜λ₯Ό μ •λ ¬ν•œλ‹€.
  • λ©΄μ ‘μ‹œν—˜μ˜ λ“±μˆ˜λ₯Ό λΉ„κ΅ν•œλ‹€.
    • 이쀑 for 문을 μ‚¬μš©ν•΄ μ„œλ₯˜ 심사 λ“±μˆ˜ 1 λ“±λΆ€ν„° N λ“±κΉŒμ§€ λΉ„κ΅ν•œλ‹€.

λ‹€μŒμ˜ 아이디어λ₯Ό κ°€μ§„ 채 μ½”λ“œλ₯Ό κ΅¬ν˜„ν–ˆλ‹€.

#include <bits/stdc++.h>
using namespace std;
 
bool compare(const pair<int, int>& a, const pair<int, int>& b){
    return a.first < b.first;
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
 
    int T, N, is_pass = 1;
    pair<int, int> rank[100005];
 
    cin >> T;
 
    while(T--){
        int res = 1;
        cin >> N;
        for(int i = 0; i < N; i++){
            cin >> rank[i].first >> rank[i].second;
        }
        sort(rank, rank+N, compare);
 
        for(int i = 1; i < N; i++){
            is_pass = 1;
            for(int j = 0; j < i; j++){
                if(rank[i].second > rank[j].second){
                    is_pass = 0;
                    break;
                }
            }
            if (!is_pass) continue;
            else res++;
        }
        cout << res << '\n';
    }
 
    return 0;
}

ν•˜μ§€λ§Œ, μ‹œκ°„ μ΄ˆκ³Όκ°€ λ–΄λ‹€. N 이 100,000 개 이기 λ•Œλ¬Έμ— 이쀑 for 문을 돌릴 경우 μ΅œμ•…μ˜ 상황에

You can't use 'macro parameter character #' in math modeO(N^2) $$이 될 수 μžˆλ‹€. j의 λ²”μœ„λ₯Ό iλ₯Ό μ΄μš©ν•΄ μ œν•œν•΄μ€¬μ§€λ§Œ 정렬도 μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— μ‹œκ°„ μ΄ˆκ³Όκ°€ λ°œμƒν•˜λŠ” 것이 μ•„λ‹Œκ°€ μ‹Άλ‹€. λ•Œλ¬Έμ— 이쀑 for문이 μ•„λ‹Œ λ‹€λ₯Έ 방법을 μ‚¬μš©ν•΄μ•Ό ν•œλ‹€. 정렬을 ν•œ μƒνƒœμ—μ„œ λ‹€μ‹œ μƒκ°ν•΄λ³΄μž. | 예제 1 | 예제 2 | | ------ | ------ | | 1, 4 | 1, 4 | | 2, 3 | 2, 5 | | 3, 2 | 3, 6 | | 4, 1 | 4, 2 | | 5, 5 | 5, 7 | | | 6, 1 | | | 7, 3 | μ˜ˆμ œμ— λ‚˜μ™€μžˆλŠ” μž…λ ₯을 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ— 따라 λ‚˜λˆ  μ •λ ¬ν–ˆλ‹€. μœ„μ—μ„œ μ•„λž˜λ‘œ 순차적으둜 ν™•μΈν•œλ‹€κ³  μƒκ°ν•΄λ³΄μž. - 예제 1을 ν™•μΈν•΄λ³΄μž. 1. 두 번째 값은 3이고, 이전 κ°’ 4와 λΉ„κ΅ν–ˆμ„ λ•Œ 더 μž‘κΈ° λ•Œλ¬Έμ— 합격할 수 μžˆλ‹€. 2. μ„Έ 번째 값은 두 번째 κ°’κ³Ό λΉ„κ΅ν–ˆμ„ λ•Œ 더 μž‘κΈ°μ— λ§ˆμ°¬κ°€μ§€λ‘œ 합격이닀. - 예제 2λ₯Ό ν™•μΈν•΄λ³΄μž. 1. 두 번째 값은 5이고 첫 번째 값은 4이닀. 두 번째 값이 더 크기 λ•Œλ¬Έμ— ν•©κ²©ν•˜μ§€ λͺ»ν•œλ‹€. 2. μ„Έ 번째 값을 비ꡐ해야 ν•œλ‹€. μ΄λ•Œ, 두 번째 값이 ν†΅κ³Όν•˜μ§€ λͺ»ν•œ 경우 비ꡐ해야 ν•  λŒ€μƒμ€ μ—¬μ „νžˆ 첫번째 값이닀. μ˜ˆμ‹œλ₯Ό 톡해 ν™•μΈν•œ 것은, μ„œλ₯˜ 심사 λ“±μˆ˜κ°€ 높은 μ‚¬λžŒμ˜ λ©΄μ ‘ λ“±μˆ˜μ™€ λΉ„κ΅ν•œλ‹€λŠ” 것, λΉ„κ΅ν–ˆμ„ λ•Œ ν•©κ²©ν•œλ‹€λ©΄ 비ꡐ λŒ€μƒμ΄ ν•©κ²©ν•œ μ‚¬λžŒμœΌλ‘œ λ³€κ²½λœλ‹€λŠ” 것이닀. λ•Œλ¬Έμ— 이에 따라 인덱슀λ₯Ό μ„€μ •ν•΄ 비ꡐ λŒ€μƒμ„ λ³€κ²½ν•΄μ£Όλ©΄ λœλ‹€. ```cpp #include <bits/stdc++.h> using namespace std; bool compare(const pair<int, int>& a, const pair<int, int>& b){ return a.first < b.first; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int T, N, is_pass = 1; pair<int, int> rank[100005]; cin >> T; while(T--){ int res = 1; cin >> N; for(int i = 0; i < N; i++){ cin >> rank[i].first >> rank[i].second; } sort(rank, rank+N, compare); int idx = 0; for(int i = 1; i < N; i++){ if(rank[i].second < rank[idx].second){ res++; idx = i; } } cout << res << '\n'; } return 0; } ```