Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • Kubernetes
  • 다이나믹 프로그래밍
  • docker
  • BFS
  • boj
  • 프로그래머스
  • DFS
  • Network
  • django
  • 백트래킹

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/BOJ

[백준 3980] 선발 명단 (C++)

2021. 9. 15. 16:16
 

3980번: 선발 명단

각각의 테스트 케이스에 대해서, 모든 포지션의 선수를 채웠을 때, 능력치의 합의 최댓값을 한 줄에 하나씩 출력한다. 항상 하나 이상의 올바른 라인업을 만들 수 있다.

www.acmicpc.net

백트래킹

 

인원수가 최대 11명이고 자신이 원하는 포지션 최대 5개 중 하나에 들어갈 수 있으므로 O(5^11 - @)의 시간복잡도가 가능하다고 생각한다. 포지션은 11개인데 11명의 선수가 원하는 포지션을 5개씩 가지고 있다면 중복되는 경우가 아주 많아서 백트래킹에서 걸러지게된다. 따라서 절대 O(5^11) 시간은 나올 수 없고 그보다 훨씬 적은 시간이 들 것으로 예상했다.

 

백트킹을 수행하면서 현재 선수가 원하는 포지션 중 하나에 들어갈 수 있다면 현재 선수의 스탯을 더하고 다음 선수로 넘어간다.

모든 선수를 포지션에 배치한다면 cnt==11이 될 텐데 이때 가장 큰 점수만 저장하도록 하면 답을 구할 수 있다.

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<int, int>
 
typedef long long ll;
// typedef pair<int, int> pii;
 
using namespace std;
 
int ans;
// {능력, 포지션}
vector<pii > player[11];
bool visited[11];
 
void input(){
    for(int i = 0; i < 11; i++){
        player[i].clear();
        visited[i] = false;
    }
    ans = 0;
    
    int a;
    for(int i = 0; i < 11; i++){
        for(int j = 0; j < 11; j++){
            cin >> a;
            if(!a) continue;
            player[i].push_back({a, j});
        }
    }
}
 
void dfs(int cnt, int sum){
    if(cnt == 11){
        ans = max(ans, sum);
        
        return;
    }
    
    for(int i = 0; i < player[cnt].size(); i++){
        int score = player[cnt][i].first;
        int position = player[cnt][i].second;
        
        if(visited[position]) continue;
        
        visited[position] = true;
        
        dfs(cnt+1, sum + score);
        
        visited[position] = false;
        
    }
}
 
void solve(){
    dfs(0, 0);
    
    cout << ans << "\n";
}
 
int main(){
    int T;
    cin >> T;
    while(T--){
        input();
        solve();
    }
    
    return 0;
}
Colored by Color Scripter
cs

 

 

'Algorithm > BOJ' 카테고리의 다른 글

[백준 16437] 양 구출 작전 (C++)  (0) 2021.09.28
[백준 15591] MooTube(Silver) (C++)  (0) 2021.09.28
[백준 1339] 단어수학 (C++)  (0) 2021.09.12
[백준 2529] 부등호 (C++)  (0) 2021.09.12
[백준 1525] 퍼즐 (C++)  (0) 2021.08.30
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 16437] 양 구출 작전 (C++)
    • [백준 15591] MooTube(Silver) (C++)
    • [백준 1339] 단어수학 (C++)
    • [백준 2529] 부등호 (C++)

    티스토리툴바