알고리즘/SW Expert Academy

1244 최대 상금

고나영 2018. 9. 20. 14:27

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15Khn6AN0CFAYD&categoryId=AV15Khn6AN0CFAYD&categoryType=CODE


DFS썼는데 N이 커지면 안될듯


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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
 
typedef struct _st
{
    int num;
    int order;
}ST;
 
#define MAX (6+6)
#define SWAP(x,y) { ST temp; temp = x; x = y; y = temp; }
int T, tc;
char num_s[MAX];
//int num[MAX];
//int order[MAX];
ST a[MAX];
int count[MAX];
int count_flag;
int digit = 6;
int N;
int ans;
 
int sum_array(void)
{
    int i;
    int S;
 
    S = a[1].num;
    for (i = 2; i <= digit; i++)
    {
        S *= 10;
        S += a[i].num;
    }
    return S;
}
 
void init(void)
{
    int i;
 
    ans = 0;
    count_flag = 0;
    for (i = 1; i <= digit; i++)
    {
        a[i].order = 1;
    }
    for (i = 0; i <= 9; i++)
    {
        count[i] = 0;
    }
}
 
void input(void)
{
    int i, j;
 
    //값 받기
    scanf("%s"&num_s[1]);
    for (i = 1; num_s[i] != 0; i++)
    {
        a[i].num = num_s[i] - '0';
        count[a[i].num]++;
    }
    digit = i - 1;
 
    //count_flag설정
    for (i = 0; i <= 9; i++)
    {
        if (count[i] > 1)
        {
            count_flag = 1;
            break;
        }
    }
 
    //order정하기
    for (i = 1; i <= digit; i++)
    {
        for (j = 1; j <= i; j++)
        {
            if (a[i].num < a[j].num)
            {
                a[i].order++;
            }
            else if (a[i].num > a[j].num)
            {
                a[j].order++;
            }
        }
    }
 
    //times 조절
    scanf("%d"&N);
    if (N > digit)
    {
        if ((digit + N) & 1) N = digit - 1;
        else N = digit; //둘다 홀인경우 or 짝인경우
    }
}
 
void DFS(int L, int C, int S)
{
    int i, j;
    int flag = 0;
 
    if (C >= N)
    {
        if (S > ans) ans = S;
        return;
    }
    if (L >= digit)
    {
        if (C < N)
        {
            //N-C 값 비교해서 짝 홀/ 같은거 있으면 리턴 없으면 (N-C)&1
            if (count_flag == 1)
            {
                if (S > ans) ans = S;
            }
            else
            {
                if ((N - C) & 1// N-C가 홀수면
                {
                    if (ans < S - 9 * a[digit - 1].num + 9 * \
                    a[digit].num) ans = S - 9 * a[digit - 1].num + 9 * a[digit].num;
                }
                else
                {
                    if (S > ans) ans = S;
                }
            }           
        }
        return;
    }
 
    //L번째 자리에서 order가 L인 것만 움직인다.
    if (a[L].order == L)
    {
        if (count[a[L].num] > 1)
        {
            flag = 1;
            count[a[L].num]--;
            for (i = L + 1; i <= digit; i++)
            {
                if (a[i].order == L) a[i].order++;
            }
        }
        DFS(L + 1, C, sum_array());
        if (flag == 1)
        {
            flag = 0;
            count[a[L].num]++;
            for (i = L + 1; i <= digit; i++)
            {
                if (a[i].order == L + 1) a[i].order--;
            }
        }
    }
    else
    {
        for (i = L + 1; i <= digit; i++)
        {
            if (a[i].order == L)
            {
                //바꾸고 같은 L이 있으면 order내에서 더해주고...
                if (count[a[i].num] > 1)
                {
                    flag = 1;
                    count[a[i].num]--;
                    for (j = L + 1; j <= digit; j++)
                    {
                        if (j == i) continue;
                        if (a[j].order == L) a[j].order++;
                    }
                }
                SWAP(a[L], a[i]);
                DFS(L + 1, C + 1, sum_array());
                if (flag == 1)
                {
                    flag = 0;
                    count[a[L].num]++;
                    for (j = L + 1; j <= digit; j++)
                    {
                        if (j == i) continue;
                        if (a[j].order == L + 1) a[j].order--;
                    }
                }
                SWAP(a[L], a[i]);
            }
        }
    }
}
 
void output(void)
{
    printf("#%d %d\n", tc, ans);
}
 
int main(void)
{
    scanf("%d"&T);
    for (tc = 1; tc <= T; tc++)
    {
        init();
        input();
        DFS(100);
        output();
    }
    return 0;
}
 
cs