#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <limits.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int n;
int ans[3];
int tb2[3001][3001];
bool same(int x, int y, int k) {
       for (int i = x; i<x + k; ++i) {
              for (int j = y; j<y + k; ++j) {
                     if (tb2[i][j] != tb2[x][y])
                     {
                           return false;
                     }
              }
       }
       return true;
}
void go(int x, int y, int k) {
       if (same(x, y, k)) {
              ans[tb2[x][y] + 1] += 1;
              return;
       }
       for (int ii = 0; ii<3; ++ii) {
              for (int jj = 0; jj<3; ++jj) {
                     go(x + ii * (k / 3), y + jj * (k / 3), k / 3);
              }
       }
}
int main(void) {
       cin >> n;
       for (int i = 0; i<n; ++i) {
              for (int j = 0; j<n; ++j) {
                     cin >> tb2[i][j];
              }
       }
       go(0, 0, n);
       for (int i = 0; i<3; ++i) {
              cout << ans[i] << endl;
       }
       return 0;
}


'알고리즘' 카테고리의 다른 글

[DC] 백준 6236 용돈관리  (0) 2018.04.29
[DC] 백준 11728 배열 합치기  (1) 2018.04.29
[BS] 백준 2512 예산  (0) 2018.04.29
[BS] 백준 1939 중량제한  (0) 2018.04.29
[BS] 백준 1561 놀이공원  (0) 2018.04.29

이해가 썩 잘 되진 않는다. 주석을 달아봐야겠다
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <limits.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int n;
int tb1[100001];
int tb2[1001][1001];
int che2[1001][1001];
ull che1[100001];

ull go(int idx, int sum){
    
}

int main(void){
    //input
    cin >> n;
    for(int i=0;i<n;++i){
        cin >> tb1[i];
    }
    int total;
    cin >> total;


    //no exceed case
    int tmp = total;
    int max_money = 0;

    //cal maxmoney
    for(int i=0;i<n;++i){
        tmp -= tb1[i]; // 예산이 충분한지
        max_money = tb1[i] > max_money ? tb1[i] : max_money;
    }

    if(tmp >= 0){ // 예산이 충분하면 가장 큰 돈을 출력
        return cout << max_money, 0;
    }



    //BS phase
    //적당한 예산을 구하는 과정이다. 우선 절반값으로 둔다.
    int left = total/n;
    int right = max_money;

    while(left<=right){
        int mid = (left + right) / 2;
        tmp = total;

        for(int i=0;i<n;++i){
            if(mid>=tb1[i]) tmp -= tb1[i];
            else tmp -= mid;
        }
        if(tmp>=0) left = mid + 1;
        else right = mid - 1;
    }
    cout << right;

    return 0;
}


// while(left<=right){
//         int mid = (left+right) / 2; // 새로 설정한 예산한도
//         tmp = total;

//         for(int i=0;i<n;++i){
//             if(mid >= tb1[i]) tmp -= tb1[i]; // 예산한도 아래면 그대로
//             else tmp -= mid; // 초과면 예산한도 만큼
//         }
//         if(tmp >= 0) left = mid+1; // 예산초과하지 않았다.
//         else right = mid - 1; // 초과했다.
//     }
//     cout << right;


'알고리즘' 카테고리의 다른 글

[DC] 백준 11728 배열 합치기  (1) 2018.04.29
[DC] 백준 1780 종이의 개수  (0) 2018.04.29
[BS] 백준 1939 중량제한  (0) 2018.04.29
[BS] 백준 1561 놀이공원  (0) 2018.04.29
[BS] 백준 3020 개똥벌레  (0) 2018.04.29

백준 강의 코드.

이분탐색 쉬운 것 같다. 
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <limits.h>
#include <map>
#include <stdlib.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int n,m,st,endd;
int tb1[100001];
int tb2[1001][1001];
int che2[1001][1001];
ull che1[100001];
string s[100001];

vector< pair<int,int> > a[10001];
bool visited[10001];

bool dfs(int idx, int limit){
    if(visited[idx]){
        return false;
    }
    visited[idx] = true;

    if(idx == endd) {
        return true;
    }

    for(int i=0;i<a[idx].size();++i){
        int next = a[idx][i].first;
        int cost = a[idx][i].second;
        if(cost >= limit){
            if(dfs(next, limit)){
                return true;
            }
        }
    }
    return false;
}

int main(void){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    for(int i=0;i<m;++i){
        int x,y,z;
        cin >> x >> y >> z;
        a[x].push_back(make_pair(y,z));
        a[y].push_back(make_pair(x,z));
    }
    cin >> st >> endd;
    int left, right;
    left = 1;
    right = 1000000000;
    int ans = 0;

    while(left <= right){
        // cout << "left : " << left << endl;
        // cout << "rt : " << right << endl;
        int mid = left + (right - left) / 2;
        memset(visited, false, sizeof(visited));
        if(dfs(st,mid)){    
            ans = mid;
            left = mid + 1;
        }
        else{
            right = mid - 1;
        }

    }
    cout << ans << endl;
    return 0;
}



'알고리즘' 카테고리의 다른 글

[DC] 백준 1780 종이의 개수  (0) 2018.04.29
[BS] 백준 2512 예산  (0) 2018.04.29
[BS] 백준 1561 놀이공원  (0) 2018.04.29
[BS] 백준 3020 개똥벌레  (0) 2018.04.29
[BS] 백준 1300 k번째 수  (0) 2018.04.29

+ Recent posts