티스토리 뷰

 

입력받는 숫자들을 보면 엄청 작다

완탐으로 정말 다 찾아보면 된다

 

답이 여러개라면 숫자가 많은것 중에 사전순으로 앞에오는 것을 출력해야한다

따라서 각 자리마자 0~x까지 넣어보면서 햄스터 수를 만족하고

현재 답보다 합이 크다면 계속 바꿔주면 된다!

 

소스코드

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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int t,n,x,m;
pair<pair<int,int>,int> arr[11];
int maxcnt;
int ans[11];
void solve(int pos,int sum,vector<int>& vt){
    if(pos==n){
        if(sum>maxcnt){
            for(int i=0;i<m;i++){
                int l = arr[i].first.first;
                int r = arr[i].first.second;
                int cnt = arr[i].second;
 
                int tempcnt=0;
                for(int j=l;j<=r;j++) tempcnt+=vt[j];
                if(tempcnt!=cnt) return;
            }
            for(int i=0;i<n;i++) ans[i]=vt[i];
            maxcnt=sum;
        }
        return;
    }
    for(int i=0;i<=x;i++){
        vt[pos]=i;
        solve(pos+1,sum+i,vt);
    }
}
int main(){
    scanf(" %d",&t);
    for(int tc=1;tc<=t;tc++){
        maxcnt=-1;
        scanf("%d %d %d",&n,&x,&m);
        for(int i=0;i<m;i++){
            int l,r,q; scanf(" %d %d %d",&l,&r,&q);
            arr[i]= {{l-1,r-1},q};
        }
        vector<int>vt(n);
        solve(0,0,vt);
        
        printf("#%d ",tc); 
        if(maxcnt==-1printf("-1");
        else for(int i=0;i<n;i++printf("%d ",ans[i]);
        puts("");
    }
}
 
 

'알고리즘 > SW Expert Academy' 카테고리의 다른 글

[SWEA] 5684 운동  (0) 2020.03.05
[SWEA] 7988 내전 경기  (0) 2020.03.04
[SWEA] 1907 모래성 쌓기  (0) 2020.03.03
[SWEA] 8382 방향전환  (0) 2020.03.03
[SWEA] 4534 트리 흑백 색칠  (0) 2020.03.02
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/04   »
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
글 보관함