회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
메모리 초과가 일어나는 이유를 모르겠습니다.
1005번 - ACM Craft
catking562
3년 전
0
어떤 부분이 메모리를 상당히 잡아먹고 있는지 모르겠습니다.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { public static ArrayList<Building> buildings = new ArrayList<>(); public static ArrayList<Integer> finalbuildings = new ArrayList<>(); public static ArrayList<Integer> canstartbuilding = new ArrayList<>(); public static ArrayList<Integer> simulatorlist = new ArrayList<>(); public static Integer finalbuild; public static int time = 0; public static class Building { public ArrayList<Integer> needbuilding = new ArrayList<>(); public ArrayList<Integer> nextbuilding = new ArrayList<>(); public int time; public Building(int time) { this.time = time; } } public static void getneedbuildings(ArrayList<Integer> list, Integer building) { list.add(building); for(Integer build : buildings.get(building).needbuilding) { getneedbuildings(list, build); } } public static void simulation() { while(!finalbuildings.contains(finalbuild)) { ArrayList<Integer> mintimebuildings = new ArrayList<>(); int mintime = Integer.MAX_VALUE; for(Integer building : simulatorlist) { Building b = buildings.get(building); if(b.time<mintime) { mintimebuildings.clear(); mintime = b.time; mintimebuildings.add(building); }else if(b.time==mintime) { mintimebuildings.add(building); } } finalbuildings.addAll(mintimebuildings); simulatorlist.removeAll(mintimebuildings); for(Integer building : simulatorlist) { buildings.get(building).time-=mintime; } time+=mintime; for(Integer building : mintimebuildings) { for(Integer building1 : buildings.get(building).nextbuilding) { if(finalbuildings.containsAll(buildings.get(building1).needbuilding)) { simulatorlist.add(building1); } } } } } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int c = Integer.parseInt(reader.readLine()); for(int i = 0; i<c; i++) { canstartbuilding.clear(); buildings.clear(); finalbuildings.clear(); simulatorlist.clear(); time = 0; String[] twoc = reader.readLine().split(" "); int cc = Integer.parseInt(twoc[0]); int rc = Integer.parseInt(twoc[1]); String[] timekey = reader.readLine().split(" "); for(int j = 0; j<cc; j++) { Building building = new Building(Integer.parseInt(timekey[j])); buildings.add(building); canstartbuilding.add(j); } for(int j = 0; j<rc; j++) { String[] twokey = reader.readLine().split(" "); int key1 = Integer.parseInt(twokey[0])-1; int key2 = Integer.parseInt(twokey[1])-1; buildings.get(key1).nextbuilding.add(key2); buildings.get(key2).needbuilding.add(key1); canstartbuilding.remove((Integer)key2); } finalbuild = Integer.parseInt(reader.readLine())-1; ArrayList<Integer> needbuildings = new ArrayList<>(); getneedbuildings(needbuildings, finalbuild); for(Integer bu : canstartbuilding) { if(needbuildings.contains(bu)) { simulatorlist.add(bu); } } simulation(); System.out.println(time); } }
jcy0011
2년 전
0
재귀함수 스택오버플로우
댓글을 작성하려면
로그인
해야 합니다.
catking562 3년 전
어떤 부분이 메모리를 상당히 잡아먹고 있는지 모르겠습니다.