μκ° μ ν | λ©λͺ¨λ¦¬ μ ν | μ λ΅ λΉμ¨ |
2 μ΄ | 512 MB | 50.576% |
λ°©λ² 1 : vector μ΄μ©
Brute Force λ‘ μ€ννΈνμ ꡬμ±μμ ꡬν΄κ³ , μ€ννΈνμ ꡬμ±μμ κΈ°λ°μΌλ‘ λ§ν¬νμ ꡬμ±μμ μμλΈλ€. κ·Έλ κ² νμ ꡬμ±μμ μμλΈ νμ κ° νμ λ₯λ ₯μΉλ₯Ό κ³μ°νμ¬ λΉκ΅νκ³ MIN κ°μ λ§€λ² κ°±μ μν¨λ€.
- ν¨μ μ€λͺ
μΈμ idxλ μΈλ±μ€ μμλ₯Ό λνλ΄κ³ , cnt λ μ€ννΈνμ ꡬμ±μμ΄ μλ μ¬λμ μ«μμ΄λ€. μ€ννΈνκ³Ό λ§ν¬νμ ꡬμ±μμ 무쑰건 n/2 λͺ μ΄λ―λ‘ μ€ννΈνμ ꡬμ±μμ΄ μλ μ¬λμ μ«μλ n/2λ₯Ό λμ΄κ° μ μλ€. λ°λΌμ cnt κ° n/2λ₯Ό λμ΄κ°λ©΄ return νλλ‘ νμλ€.
λ©λͺ¨λ¦¬ | μκ° |
1176 KB | 20 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
|
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
int n, arr[25][25], chk[25], MIN = 10000;
vector<int> team1, team2;
void go(int idx, int cnt) {
if (idx > n || cnt > n/2) return;
if (team1.size() == n / 2) {
// μμͺ½μ ν λ₯λ ₯μΉλ₯Ό λΉκ΅νμ¬ MIN κ°μ ꡬν κ±°μ.
int sumteam1 = 0, sumteam2 = 0;
for (int i = 0; i < team1.size(); i++) {
chk[team1[i]] = 1;
for (int j = i+1; j < team1.size(); j++) {
sumteam1 += arr[team1[i]][team1[j]] + arr[team1[j]][team1[i]];
}
}
for (int i = 1; i <= n; i++) {
if (!chk[i]) team2.push_back(i);
}
for (int i = 0; i < team2.size(); i++) {
for (int j = i + 1; j < team2.size(); j++) {
sumteam2 += arr[team2[i]][team2[j]] + arr[team2[j]][team2[i]];
}
}
MIN = min(MIN, abs(sumteam1 - sumteam2));
team2.clear(); memset(chk, 0, sizeof(chk));
return;
}
team1.push_back(idx);
go(idx + 1, cnt);
team1.pop_back();
go(idx + 1, cnt + 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &arr[i][j]);
}
}
go(1, 0);
printf("%d", MIN);
return 0;
}
|
cs |
λ§€λ² κΈ°μ λ¨κ³μμ λ°°μ΄ μ΄κΈ°νλ₯Ό μμΌμΌνκΈ° λλ¬Έμ λΆλ΄μ΄ ν° λ¨μ μ΄ μλ€. κ·Έλμ λ°©λ² 2λ₯Ό μ¬μ©ν΄ 보μλ€.
λ°©λ² 2 : vector μ΄μ© μκ° λ¨μΆ.
μ΄ λ°©λ²μ λ§€λ² n ν¬κΈ°μ λ°°μ΄μ λͺ¨λ νμν΄μΌνμ§λ§ λ°°μ΄μ μ΄κΈ°νν΄μΌνλ λΆλ΄μ΄ μ μ΄μ§κΈ° λλ¬Έμ μκ°μ΄ ν¬κ² μ μ½λμ΄ 'λ°©λ² 1'λ³΄λ€ ν¨μ¨μ μΈ μ½λκ° λλ€.
κ·Έλ¦¬κ³ 'λ°©λ² 1'μμ μ¬μ©νλ cntλ₯Ό λ°©λ² 2μμ μ¬μ©νλ©΄ n λͺ μ νμ λͺ¨λλ₯Ό λ§λ μ μμ΄ μμνλ κ²°κ³Όκ°κ³Ό λ€λ₯Έ κ°μ΄ λμ€κ² λμ΄ λΉΌμ£Όμ΄μΌ νλ€.
λ©λͺ¨λ¦¬ | μκ° |
1176 KB | 4 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
|
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
int n, arr[25][25], MIN = 10000;
vector<int> team1, team2;
void go(int idx) {
if (!MIN) return; // μ°¨μ΄κ° 0μ΄λΌλ©΄ λ μ΄μ λ³΄μ§ μμλ λ¨.
if (idx > n) {
// μμͺ½μ ν λ₯λ ₯μΉλ₯Ό λΉκ΅νμ¬ MIN κ°μ ꡬνλ€.
int sumteam1 = 0, sumteam2 = 0;
for (int i = 0; i < team1.size() - 1; i++) {
for (int j = i + 1; j < team1.size(); j++) {
sumteam1 += arr[team1[i]][team1[j]] + arr[team1[j]][team1[i]];
sumteam2 += arr[team2[i]][team2[j]] + arr[team2[j]][team2[i]];
}
}
MIN = min(MIN, abs(sumteam1 - sumteam2));
return;
}
if (team1.size() < n / 2) {
team1.push_back(idx);
go(idx + 1);
team1.pop_back();
}
if (team2.size() < n / 2) {
team2.push_back(idx);
go(idx + 1);
team2.pop_back();
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &arr[i][j]);
}
}
go(1);
printf("%d", MIN);
return 0;
}
|
cs |
λ°©λ² 3 : int λ°°μ΄ μ΄μ©
λ©λͺ¨λ¦¬λ, μκ°λ λͺ¨λ μ μ½λλ μ΅μ μ λ°©λ²μ΄λ€.
λ©λͺ¨λ¦¬ | μκ° |
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
|
#include<cstdio>
#include<math.h>
int n, a[20][20], t1[10], t2[10], an, bn, ans = 1e5;
void go(int x) {
if (!ans) return;
if (x == n) {
int s1 = 0, s2 = 0;
for (int i = 0; i < n / 2 - 1; i++)
for (int j = i + 1; j < n / 2; j++) {
s1 += a[t1[i]][t1[j]] + a[t1[j]][t1[i]];
s2 += a[t2[i]][t2[j]] + a[t2[j]][t2[i]];
}
s1 = abs(s1 - s2);
ans = ans < s1 ? ans : s1;
return;
}
if (an < n / 2) {
t1[an++] = x;
go(x + 1);
an--;
}
if (bn < n / 2) {
t2[bn++] = x;
go(x + 1);
bn--;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &a[i][j]);
go(0);
printf("%d", ans);
}
|
cs |
λ§λ¬΄λ¦¬
μ€ννΈμ λ§ν¬λ₯Ό νλ©΄μ vectorκ° μΌλ° int λ°°μ΄λ³΄λ€ λ©λͺ¨λ¦¬μ μκ°μ λ§μ΄ μ¬μ©νκ² λλ€λ μ μ ν¬κ² κΉ¨λ«κ² λμλ€. λ¬Όλ‘ λ©λͺ¨λ¦¬λΆλΆμ ν€λ?μ μν₯μ΄ μμ κ² κ°μ§λ§! νμ€ν μΈλ±μ€λ‘λ§ μμ§μ΄λ κ²κ³Ό ν¨μ(pop_back, push_back)μ μ°λ κ²μ ν° μ°¨μ΄κ΅¬λ μΆμλ€.
'π₯ PS(Problem Solving) π₯ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] #1010 λ€λ¦¬λκΈ° (0) | 2019.10.02 |
---|---|
[BOJ] #14891 ν±λλ°ν΄ (0) | 2019.10.02 |
[BOJ] #3190 λ± (0) | 2019.09.29 |
[BOJ] #16929 Two Dots (0) | 2019.09.29 |
[BOJ] #6359 λ§μ·¨ν μλ² (0) | 2019.09.19 |
λκΈ