티스토리 뷰
"이분 탐색 과 double.. 100~200 번만 돌리자."
이분 탐색을 통해 문제를 풀어야 한다. 근데 제한 없이 돌리다 보니 stackoverflow... 아래와 같은 게시물을 봄... 결론만 말하자면 double형을 가지고 이분 탐색을 할 경우 100~200 번 까지만 돌리도록 한다는 것이다. (https://www.acmicpc.net/blog/view/37)
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Solution { static int T; static int N; static int[] X; static int[] M; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); T = Integer.parseInt(br.readLine()); int t = 1; while(T-- > 0) { N = Integer.parseInt(br.readLine()); X = new int[N]; M = new int[N]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < N; i++) { X[i] = Integer.parseInt(st.nextToken()); } for (int i = 0; i < N; i++) { M[i] = Integer.parseInt(st.nextToken()); } System.out.printf("#%d",t++); for (int i = 0; i < N-1; i++) { System.out.printf(" %.10f",solve(i,0,(X[i]+X[i+1])/2,X[i],X[i+1])); } System.out.println(); } } static double solve(int idx, int depth, double cur, double left, double right) { if( depth == 100) return cur; double f = 0.0; double val = 0.0; for (int i = 0; i <= idx; i++) { f += (double)M[i] / Math.pow(cur - X[i], 2.0); } for (int i = idx+1; i < N; i++) { f -= (double)M[i] / Math.pow(X[i] - cur, 2.0); } if(f < 0) { val = solve(idx,depth+1, (left + cur)/2.0, left, cur); } else if ( f > 0) { val = solve(idx,depth+1, (cur + right)/2.0, cur, right); } else { val = cur; } return val; } } | cs |
'Study > 알고리즘 문제풀이' 카테고리의 다른 글
백준 15683. 감시 :: 돼지개발자 (0) | 2019.01.30 |
---|---|
SWEA 3752. 가능한 시험 점수 :: 돼지개발자 (0) | 2019.01.29 |
백준 4920. 테트리스 게임 :: 돼지개발자 (0) | 2019.01.28 |
백준 3019. 테트리스 :: 돼지개발자 (0) | 2019.01.28 |
2018 카카오 1차 코딩테스트 오픈채팅방 :: 돼지개발자 (0) | 2019.01.23 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday