μκ° μ ν | λ©λͺ¨λ¦¬ μ ν | μ λ΅ λΉμ¨ |
2 μ΄ | 512 MB | 46.685 % |
Nκ°μ μμ N-1κ°μ μ°μ°μκ° μ£Όμ΄μ‘μ λ, λ§λ€ μ μλ μμ κ²°κ³Όκ° μ΅λμΈ κ²κ³Ό μ΅μμΈ κ²μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
μ°μ°μ λ°°μ΄λ‘ dfs λ₯Ό ν΄μΌνλ€.
λ©λͺ¨λ¦¬ | μκ° |
1988 KB | 0 ms |
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
|
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long int ll;
ll arr[12]; int calc[4]; int n;
ll Max = -1e9; ll Min = 1e9;
void dfs(int idx, ll sum, int op) {
if (op == -1) sum = arr[idx];
if (op == 0) sum += arr[idx];
if (op == 1) sum -= arr[idx];
if (op == 2) sum *= arr[idx];
if (op == 3) {
if (sum < 0) {
sum = abs(sum);
sum /= arr[idx];
sum = -sum;
}
else
sum /= arr[idx];
}
if (idx == n - 1) {
if (Max < sum) Max = sum;
if (Min > sum) Min = sum;
return;
}
for (int i = 0; i < 4; i++) {
if (calc[i] != 0) {
calc[i]--;
dfs(idx + 1, sum, i);
calc[i]++;
}
}
return;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (int i = 0; i < 4; i++)
scanf("%d", &calc[i]);
dfs(0, 0, -1);
printf("%lld\n%lld", Max, Min);
return 0;
}
|
cs |
λ λ€λ₯Έ λ°©λ²
λ©λͺ¨λ¦¬ | μκ° |
1116 KB | 0 ms |
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
|
#include <stdio.h>
typedef long long int ll;
int n, operation[4];
ll num[11], max = -1e9, min = 1e9;
void dfs(int idx, int sum)
{
if (idx == n)
{
min = min < sum ? min : sum;
max = max > sum ? max : sum;
return;
}
if (operation[0])
{
--operation[0];
dfs(idx + 1, sum + num[idx]);
++operation[0];
}
if (operation[1])
{
--operation[1];
dfs(idx + 1, sum - num[idx]);
++operation[1];
}
if (operation[2])
{
--operation[2];
dfs(idx + 1, sum * num[idx]);
++operation[2];
}
if (operation[3])
{
--operation[3];
dfs(idx + 1, sum / num[idx]);
++operation[3];
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
}
for (int i = 0; i < 4; i++) {
scanf("%d", &operation[i]);
}
dfs(1, num[0]);
printf("%lld\n%lld\n", max, min);
}
|
cs |
'π₯ PS(Problem Solving) π₯ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] #14501 ν΄μ¬ (0) | 2019.09.19 |
---|---|
[BOJ] #2309 μΌκ³± λμμ΄ (0) | 2019.09.19 |
[BOJ] #1182 λΆλΆμμ΄μ ν© (0) | 2019.09.04 |
[BOJ] #14582 μ€λλ μ‘λ€ (0) | 2019.09.04 |
[BOJ] #1946 μ μ μ¬μ (0) | 2019.09.04 |
λκΈ