Processing math: 100%

ABOUT ME

Today
Yesterday
Total
  • 368-B, *1100
    코드포스 2021. 7. 23. 16:24
     

    Problem - 368B - Codeforces

     

    codeforces.com

    문제

    • 배열 a=a1a2an, (1n105)에 대해
    • alal+1an 중 서로 다른 수가 몇 개인지 m(1m105)번 출력
    • O(nlog n) 정도의 알고리즘은 통과할 수 있다

     

    O(log n!)

    가장 큰 l부터 답을 구하여 배열 arr[l]에 기록한다
    an을 set s 에 삽입한다. 그러면 O(log s.size)
    ans[n]=s.size
    an1을 set s 에 삽입한다. 그러면 O(log s.size)
    ans[n1]=s.size


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #include <bits/stdc++.h>
    #define endl "\n"
    using namespace std;
     
    int arr[100001];
    int ans[100001];
    set<int> s;
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int n, m;
        cin >> n >> m;
        for(int i = 1; i <= n; i++cin >> arr[i];
     
        for(int i = n; i >= 1; i--){
            s.insert(arr[i]);
            ans[i] = s.size();
        }
     
        int tmp;
        while(m--){
            cin >> tmp;
            cout << ans[tmp] << endl;
        }
     
        return 0;

     

    '코드포스' 카테고리의 다른 글

    1327-A, *1100  (0) 2021.07.24
    1335-C, *1100  (0) 2021.07.23
    313-B, *1100  (0) 2021.07.23
    456-A, *1100  (0) 2021.07.23
    519-B, *1100  (0) 2021.07.23

    댓글

Designed by Tistory.