모든 점들 사이에 대해서 이진탐색을 통해 균형점을 찾아주면 된다.



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
#include <stdio.h>
 
struct Node{
    int x;
    int w;
};
 
int t, n;
Node a[11];
 
double abs(double a){
    return (a > 0) ? a : -a;
}
 
int main(){
    scanf("%d"&t);
 
    for (int tc = 1; tc <= t; tc++){
        scanf("%d"&n);
 
        for (int i = 1; i <= n; i++){
            scanf("%d"&a[i].x);
        }
 
        for (int i = 1; i <= n; i++){
            scanf("%d"&a[i].w);
        }
        
        printf("#%d ", tc);
        for (int i = 1; i < n; i++){
            double x;
            double left = a[i].x;
            double right = a[i + 1].x;
 
            int cnt = 0;
            while (true){
                x = (left + right) / 2.0;
                double left_force = 0;
                double right_force = 0;
                for (int j = 1; j <= i; j++){
                    left_force += (a[j].w) / ((a[j].x - x)*(a[j].x - x));
                }
                for (int j = i + 1; j <= n; j++){
                    right_force += (a[j].w) / ((a[j].x - x)*(a[j].x - x));
                }
 
                if (cnt++ == 100) {
                    printf("%.10f ", x);
                    break;
                }
 
                if (left_force > right_force){
                    left = x;
                }
                else {
                    right = x;
                }
 
            }
        }
        printf("\n");
    }
}
cs


'SWEA::문제풀이' 카테고리의 다른 글

5644 무선 충전  (0) 2018.10.08
공통조상  (0) 2018.09.06
최대 상금  (0) 2018.09.06
4676 늘어지는 소리 만들기  (0) 2018.07.14
2117 홈 방범 서비스  (0) 2018.04.14

+ Recent posts