3015번 - 오아시스 재결합
어디가 틀렸는지 모르겠네요... import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { System.<i>setIn</i>(new FileInputStream("src/CT/input.txt")); BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.<i>out</i>)); int n = Integer.<i>parseInt</i>(br.readLine()); Stack<Long> stack = new Stack<>(); Map<Long,Integer> m = new HashMap<>(); int count = 0; for(int i = 0; i < n; i++){ long p = Long.<i>parseLong</i>(br.readLine()); if(!m.containsKey(p)) m.put(p,0); while(!stack.isEmpty()){ if(stack.peek() > p){ count++; break; }else if(stack.peek() < p){ count++; long del = stack.pop(); if(m.get(del) != 0){ count += m.get(del); m.put(del, 0); } }else{ count += m.get(p) + 1; m.put(p,m.get(p) + 1); stack.pop(); } } stack.push(p); } bw.write(count + "\n"); bw.flush(); bw.close(); br.close(); } }
count는 충분히 int의 범위를 넘을 수 있습니다.
정말 간단한거였네요 하..... 감사합니다
댓글을 작성하려면 로그인해야 합니다.
maxol2558 2년 전
어디가 틀렸는지 모르겠네요... import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { System.<i>setIn</i>(new FileInputStream("src/CT/input.txt")); BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.<i>out</i>)); int n = Integer.<i>parseInt</i>(br.readLine()); Stack<Long> stack = new Stack<>(); Map<Long,Integer> m = new HashMap<>(); int count = 0; for(int i = 0; i < n; i++){ long p = Long.<i>parseLong</i>(br.readLine()); if(!m.containsKey(p)) m.put(p,0); while(!stack.isEmpty()){ if(stack.peek() > p){ count++; break; }else if(stack.peek() < p){ count++; long del = stack.pop(); if(m.get(del) != 0){ count += m.get(del); m.put(del, 0); } }else{ count += m.get(p) + 1; m.put(p,m.get(p) + 1); stack.pop(); } } stack.push(p); } bw.write(count + "\n"); bw.flush(); bw.close(); br.close(); } }