문제링크 : https://www.acmicpc.net/problem/1107

아이디어

  • BFS로 채널을 누르고, 위아래로 누를때 원하는 채널로 이동하는 최소의 값
  • 버튼 누르는건 어떻게?
    • 1~6번째만, 해당 자리수의 숫자 큐에 넣음
    • 에시 : 2번쨰 누르는 경우엔, 10~99까지
    • 고장난 버튼 : 각 자리에서 고장난 버튼 확인
      • 이떄 BFS이므로 중복체크 해주기

시간복잡도 계산

  • 큐에 최대 들어갈수 있는수(노드) : 5e5
  • 큐에 들어가는 노드에서, 고장난 버튼 확인 : 10
  • 최대 움직일수 있는 수(간선) : 5e5
  • BFS 시간복잡도 : O(V*10+E) : 5e6 + 5e5 = 5.5e6

인트 계산

  • 누르는 횟수 : 최대 5e5

코드

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
 
using namespace std;
 
vector<int> f;
bool chk[1000010];
//bool chk[10000];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int target; cin >> target;
    
    // 중복체크 초기화
//    fill(chk, chk+500010, false);
    
    int M; cin >> M;
    for(int i=0; i<M; i++) {
        int a; cin >> a; f.push_back(a);
    }
    
    queue<int> q;
    q.push(100);
    chk[100= 1;
    int cnt=0;
    while(!q.empty()) {
        int qsize = q.size();
        while(qsize--) {
            int eq = q.front(); q.pop();
            
            if(eq == target) {
                cout << cnt << '\n';
                return 0;
            }
            
            // 더하고
            if(eq+1 <=500000) {
                if(chk[eq+1== false) {
                    chk[eq+1= 1;
                    q.push(eq+1);
                }
            }
            
            // 빼기
            if(eq-1 >=0) {
                if(chk[eq-1== false) {
                    chk[eq-1= 1;
                    q.push(eq-1);
                }
            }
            
            
            
        }
        
        if(cnt<6) {
            int st = pow(10,cnt);
            if(st==1) st = 0;
            int en = pow(10,cnt+1)-1;
            
            for(int i=st; i<=en; i++) {
                if(chk[i]) continue;
                bool sw = false;
                // 각 자리수에서 금지된 숫자 있는지 확인
                for(int j=0; j<=cnt; j++) {
                    // 각자리숫자
                    int num = (int)(i/pow(10,j)) % 10;
                    if(find(f.begin(), f.end(), num) != f.end()) {
                        sw = true;
                        break;
                    }
                    
                }
                
                // 금지된 숫자에 걸리는 것이 없다면 큐에 넣기
                if(sw == false){
                    chk[i] = 1;
                    q.push(i);
                }
            }
        }
        
        cnt++;
    }
    
    return 0;
}
 
cs

문제유형

  • BFS - 최소 횟수

비슷한 문제

문제링크 : https://www.acmicpc.net/problem/2143


아이디어

  • A,B의 각각 부배열을 구하는 방법
    • 애초에 들어올떄 해당 인덱스까지의 합을 받아옴
    • 각 인덱스 순회하면서 모든 경우의 수를 구함
    • O(N^2)
  • A,B 각각 더해서 값을 구할경우 : O(N^2 * N^2) = O(N^4) = 1e12 > 시간초과
  • A의 값을 고정한다음에, 더해서 0이되는 값을 B에서 이진탐색으로 찾음
  • B 배열에서 같은 값이 여러개일경우에??
    • 이진탐색을 lower bound, upper bound 같이 수행해서 차이만큼 빼기

시간복잡도 계산

  • B 정렬 : N^2lgN^2
  • A순회하면서 이진탐색 :N^2 lgN^2
  • O( N^2lgN^2 + N^2lgN^2) = O( N^2lgN^2) = 1e6 * lg(1e6) = 1e6 * 20 = 2e7

인트 계산

  • 더해서 최악의 캐이스 : N * 1e6 * 2 = 1e3 * 1e6 * 2 = 2e9 > INT 가능
    • 결과값 : 1e6개 * 1e6개 = 1e12개 >> ll 사용

코드

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <algorithm>
 
using namespace std;
typedef long long ll;
 
int sumA[1010];
int sumB[1010];
int partA[1000010];
int partB[1000010];
 
int binsearchlower(int st, int en, int target) {
    if(st==en) {
        if(partB[st] == target) return st;
        return -1;
    }
 
    int mid = (st+en)/2;
    if(partB[mid] < target) return binsearchlower(mid+1, en, target);
    else return binsearchlower(st, mid, target);
}
 
int binsearchupper(int st, int en, int target) {
    if(st==en) {
        if(partB[st] == target) return st;
        return -1;
    }
 
    int mid = (st+en+1)/2;
    if(partB[mid] <= target) return binsearchupper(mid, en, target);
    else return binsearchupper(st, mid-1, target);
}
 
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int T; cin >> T;
    int N; cin >> N;
    cin >> sumA[0];
    partA[0= sumA[0];
    for(int i=1; i<N; i++) {
        cin >> sumA[i];
        // 이전값이랑 합함
        sumA[i] += sumA[i-1];
    }
 
    int cntA=0;
    for(int j=0; j<N; j++) {
        partA[cntA] = sumA[j];
        cntA++;
        for(int i=0; i<j; i++) {
            partA[cntA] = sumA[j] - sumA[i];
            cntA++;
        }
    }
 
 
    int M; cin >> M;
    cin >> sumB[0];
    partB[0= sumA[0];
    for(int i=1; i<M; i++) {
        cin >> sumB[i];
        // 이전값이랑 합함
        sumB[i] += sumB[i-1];
    }
 
    int cntB=0;
    for(int j=0; j<M; j++) {
        partB[cntB] = sumB[j];
        cntB++;
        for(int i=0; i<j; i++) {
            partB[cntB] = sumB[j] - sumB[i];
            cntB++;
        }
    }
 
    sort(partB, partB+cntB);
 
    ll rs=0;
    for(int i=0; i<cntA; i++) {
        int tar = T - partA[i];
        // 같은 값을 나오는걸 어떻게 할까 >> lower, upper 둘다 구해서 인덱스 더하자
        int st = binsearchlower(0,cntB-1,tar);
        if(st != -1) {
            int en =binsearchupper(0,cntB-1,tar);
            rs += (en-st+1);
        }
    }
 
    cout << rs << '\n';
    
    return 0;
 
}
 
 
 
cs

문제유형

  • 이진탐색 - 모든케이스에서 이진탐색

비슷한 문제

문제링크 : https://www.acmicpc.net/problem/2565


문제 추상화

  • DP로 LCS를 구하는 문제
  • LCS인 이유
    • 전깃줄이 교차하지 않아야함
    • A이나 B중 하나를 정렬하면
    • 전깃줄이 교차하지 않으면 나머지 것이 순차적으로 증가와 같음

아이디어

  • DP로 LCS 풀기
    • DP[i] = i번째 위치에서 LCS 개수
    • 처음에 DP[i]를 1로 초기화
    • DP[i] = for(j=0~i-1) if(num[i] > num[j]) DP[i] = max(Dp[i], DP[j]+1)

시간복잡도 계산

  • O(N^2) : 1e4
  • 만약 숫자 클경우 이진탐색을 통해서 풀경우 O(NlgN)으로 가능

인트 계산

  • 전깃출 최대 100
  • 위치 최대 500
  • 모두 INT 사용 가능

코드

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
31
32
33
34
#include <iostream>
#include <algorithm>
 
using namespace std;
 
pair<intint> nums[110];
int dp[110];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    fill(dp, dp+1101);
    int N; cin >> N;
    for(int i=0; i<N; i++) {
        int a,b; cin >> a >> b;
        nums[i] = make_pair(a,b);
    }
    
    sort(nums, nums+N);
    int maxv= 1;
    for(int i=1; i<N; i++) {
        for(int j=0; j<i; j++) {
            if(nums[i].second > nums[j].second) {
                dp[i] = max(dp[i], dp[j]+1);
            }
        }
        maxv = max(maxv, dp[i]);
    }
    
    cout << N-maxv << '\n';
    
    return 0;
}
 
cs

문제유형

  • 끝에서부터 시작하는 DP
    • DP에서 순차적으로 오는것이 아닌, 여러 경로가 있을떄 사용
    • 특히 2차원에서 상하좌우를 모두 고려해줘야할 때

비슷한 문제

  • DP - LCS

+ Recent posts