#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;
 
long long A, B, V;
 
int main(void)
{
//    freopen("B2869_input.txt", "r", stdin);
 
    cin >> A >> B >> V;
    
    long long left = 1;
    long long right = V;
    long long Min = 1111111111;
    
    while(left <= right)
    {
        // 날짜 
        long long mid = (left + right) / 2;
 
        if(A * mid - B * (mid-1>= V)
        {
            
            if(mid < Min)
            {
                Min = mid;
            }
            
            right = mid-1;
        }
        else if(A * mid - B * (mid-1< V)
        {
            left = mid+1;
        }
    }
    
    cout << Min;
 
    return 0;
}
cs

map

map 은 기본적으로 레드블랙 트리(Red-Black Tree 이하 RB Tree) 기반으로 되어있습니다.

때문에 모든 데이터는 정렬되어 저장됩니다.

 

RB Tree는 검색속도가 빠른 Binary Search Tree(BST)에 Self-Balancing 기능을 추가한 것으로 AVL Tree만큼 엄격하진 않지만

O(logN)을 보장하는 수준으로 Balancing 되어지는 특징을 갖고 있습니다. 

 

이러한 특성 때문에 입력되는 키 값의 분포가 고르지 못할 경우 balancing(node rotation)에 대한 비용이 들기 때문에

Insert / Delete 성능이 저하될 수 있습니다. (물론 최악의 경우에도 O(logN)의 성능은 보장됩니다)

 


unordered_map

unordered_map은 hash_table 기반의 hash container입니다. 

 

hash_table을 통해 참조를 하기 때문에 각 node들을 정렬할 필요가 없습니다. 

따라서 충분한 hash_table의 크기만 주어진다면 데이터 양이 많아지더라도

Search / Insert / Delete 의 O(1) 성능을 꾸준하게 보장할 수 있습니다. 

 

 

 

 



출처: https://gracefulprograming.tistory.com/3 [Peter의 우아한 프로그래밍]

'STL > map' 카테고리의 다른 글

map 자주 쓰는 멤버함수 정리  (0) 2019.09.28
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;
 
int N, M;
unordered_map<intint> check;
 
int main(void)
{
//    freopen("B10816_input.txt", "r", stdin);
    
    ios_base :: sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);
 
    cin >> N;
    
    for(int i = 1; i <= N; i++)
    {
        int num;
        cin >> num;
        
        check[num] += 1;
    }
    
    cin >> M;
    
    while(M--)
    {
        int Find;
        cin >> Find;
    
        cout << check[Find] << " ";
    }
 
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
 
int main(void)
{
//    freopen("B2018_input.txt", "r", stdin);
    
    int N;
    int ans = 0;
        
    cin >> N;
    
    int left = 1;
    int right = 1;
    int sum = 0;
    
    while(left <= right && right <= N+1)
    {        
        if(sum > N)
        {
            sum -= left;
            left++;
        }
        else if(sum < N)
        {
            sum += right;
            right++;
        }
        else if(sum == N)
        {
            ans++;
            
            sum += right;
            right++;
        }
    }
    
    cout << ans;
 
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
 
int main(void)
{
//    freopen("B1484_input.txt", "r", stdin);
    
    long long G;
    vector<long long> result;
        
    cin >> G;
    
    long long left = 1;
    long long right = 1;
    
    while(left <= right && right <= G+1)
    {
        if(right*right - left*left > G)
        {
            left++;
        }
        else if(right*right - left*left < G)
        {
            right++;
        }
        else if(right*right - left*left == G)
        {
            result.push_back(right);
            
            right++;
        }
    }
    
    if(result.size() == 0)
    {
        cout << -1;
    }
    else
    {
        for(int i = 0; i < result.size(); i++)
        {
            cout << result[i] << endl;
        }
    }
 
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
 
int N, K;
int arr[1000010];
 
int main(void)
{
//    freopen("B15565_input.txt", "r", stdin);
    
    cin >> N >> K;
    
    for(int i = 1; i <= N; i++)
    {
        cin >> arr[i];
    }
    
    int left = 1;
    int right = 1;
    int lionCnt = 0;
    int Min = 99999999;
    
    while(left <= right && right <= N+1)
    {
        if(lionCnt < K)
        {
            if(arr[right] == 1)
            {
                lionCnt++;
                right++;
            }
            else
            {
                right++;
            }
        }
        else if(lionCnt > K)
        {
            if(arr[left] == 1)
            {
                lionCnt--;
                left++;
            }
            else
            {
                left++;
            }
        }
        else if(lionCnt == K)
        {
            if(Min > right-left)
            {
                Min = right-left;
            }
            
            if(arr[left] == 1)
            {
                lionCnt--;
                left++;
            }
            else
            {
                left++;
            }
        }
    }
    
    if(Min == 99999999)
    {
        cout << -1;
    }
    else
    {
        cout << Min;    
    }
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
 
int N, M;
int A[10010];
int ans;
 
int main(void)
{
//    freopen("B2003_input.txt", "r", stdin);
    
    cin >> N >> M;
    
    int left = 1;
    int right = 1;
    int sum = 0;
    
    for(int i = 1; i <= N; i++)
    {
        cin >> A[i];
    }
    
    while(left <= right && right <= N+1)
    {
        if(sum < M)
        {
            sum += A[right++];
        }
        else if(sum == M)
        {
            ans++;
            sum += A[right++];    
        }
        else
        {
            sum -= A[left++];
        }
    }
    
    cout << ans;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int N, treeNum, year;
vector<int> tree[15][15];
vector<int> die[15][15];
int    food[15][15];
int add_food[15][15];
int ans;
 
int dx[4= {-1100};
int dy[4= {00-11};
 
int safe(int x, int y)
{
    if(x >= 1 && y >= 1 && x <= N && y <= N)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
void spring()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(tree[i][j].size() != 0)
            {
                // 죽는 나무 인덱스 저장 
                int eraseIdx = -1;
                
                // 하나의 칸에 여러 개의 나무가 있다면, 나이가 어린 나무부터 양분을 먹는다.
                sort(tree[i][j].begin(), tree[i][j].end());
                
                for(int k = 0; k < tree[i][j].size(); k++)
                {
                    // 나이만큼 양분이 있어야 가능 
                    if(food[i][j] - tree[i][j][k] >= 0)
                    {
                        // 양분 먹음 
                        food[i][j] -= tree[i][j][k];
                        
                        // 나무가 자신의 나이만큼 양분을 먹고, 나이가 1 증가한다.
                        tree[i][j][k] += 1;     
                    }
                    else
                    {
                        eraseIdx = k;
                        break;
                    }
                }
                
                if(eraseIdx != -1)
                {
                    // 죽은 나무의 나이 저장 
                    for(int k = eraseIdx; k < tree[i][j].size(); k++)
                    {
                        die[i][j].push_back(tree[i][j][k]);
                    }
                    
                    tree[i][j].erase(tree[i][j].begin()+eraseIdx, tree[i][j].end());
                }
            }
        }
    }
}
 
void summer()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(die[i][j].size() != 0)
            {
                // 각각의 죽은 나무마다 나이를 2로 나눈 값이 나무가 있던 칸에 양분으로 추가된다. 
                for(int k = 0; k < die[i][j].size(); k++)
                {
                    food[i][j] += die[i][j][k] / 2;
                }
                
                die[i][j].clear();
            }
        }
    }
}
 
void fall()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(tree[i][j].size() != 0)
            {
                // 번식하는 나무는 나이가 5의 배수이어야 하며, 인접한 8개의 칸에 나이가 1인 나무가 생긴다. 
                for(int k = 0; k < tree[i][j].size(); k++)
                {
                    if(tree[i][j][k] % 5 == 0)
                    {
                        for(int m = i-1; m <= i+1; m++)
                        {
                            for(int n = j-1; n <= j+1; n++)
                            {
                                if(m == i && n == j)
                                {
                                    continue;
                                }
                                
                                if(safe(m, n) == 1)
                                {
                                    tree[m][n].push_back(1);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
 
void winter()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            food[i][j] += add_food[i][j];
        }
    }
}
 
int main(void)
{
//    freopen("B15685_input.txt", "r", stdin);
    
    ios_base :: sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);
 
    cin >> N >> treeNum >> year;
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            food[i][j] = 5;
            cin >> add_food[i][j];    
        }
    }
    
    for(int i = 1; i <= treeNum; i++)
    {
        int x, y, age;
        cin >> x >> y >> age;
    
        tree[x][y].push_back(age);
    }
    
    while(year--)
    {
        spring();
        summer();
        fall();
        winter();
    }
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            ans += tree[i][j].size();
        }
    }
    
    cout << ans;
    
    return 0;
}
cs

+ Recent posts