회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
시간초과.. 로직 설계 시점에서 어떻게 고려해야할까요
21758번 - 꿀 따기
kimmgsa
1년 전
0
아래코드는 시간초과가 발생하는 코드인데요
설계시점에서 이를 회피하기 위해 보통 어떻게 접근하시나요..
#include <iostream> #include <vector> typedef std::pair<int, int> status; std::vector<int> place; int findHoney(status bee, int honey, std::vector<int> &block_pos) { int result = 0; while(bee.first != honey) { bee.first += bee.second; if(block_pos[0] ==bee.first || block_pos[1] == bee.first) continue; result += place[bee.first]; //std::cout<<"bee,"<<bee.first << "honey"<<place[bee.first]<<std::endl; } return result; } int main() { int N; int value; std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> N; for(int i =0; i<N; i++) { std::cin >> value; place.push_back(value); } //0~N 범위에 값이 있고 꿀통을 임의로 배치 //꿀벌의 상태(위치), 꿀통의 위치 //N이 100,000까지.. // 꿀통 위치 - 꿀벌 위치 => 방향 정하기 int bee_pos = 0; //벌 위치 경우의수 std::vector<std::vector<status>> moving_bees; for(int i=0; i<N; i++) { for(int j=0; j<i; j++) { if(i == j) continue; moving_bees.push_back({{i,0},{j,0}}); //std::cout<<i << ',' << j <<std::endl; } } int result = 0; //수집된 벌 위치 하나씩 검증 while(!moving_bees.empty()) { std::vector<status> moving_bee = moving_bees.back(); moving_bees.pop_back(); // 빈공간 조사 std::vector<int> empty_pos; std::vector<int> block_pos; int honey_pos = 0; block_pos.push_back(moving_bee[0].first); block_pos.push_back(moving_bee[1].first); for(int i= 0; i< N; i++) { if(moving_bee[0].first == i || moving_bee[1].first == i) continue; empty_pos.push_back(i); } while(!empty_pos.empty()) { //꿀통 설정 honey_pos = empty_pos.back(); empty_pos.pop_back(); //이동방향 설정 if(honey_pos - moving_bee[0].first > 0 ) moving_bee[0].second = 1; else if(honey_pos - moving_bee[0].first < 0) moving_bee[0].second = -1; if(honey_pos - moving_bee[1].first > 0 ) moving_bee[1].second = 1; else if(honey_pos - moving_bee[1].first < 0) moving_bee[1].second = -1; //std::cout << moving_bee[0].second << ',' << moving_bee[1].second << std::endl; //꿀 찾기 int honey1 = findHoney(moving_bee[0],honey_pos,block_pos); int honey2 = findHoney(moving_bee[1],honey_pos,block_pos); result = std::max(result,honey1+honey2); } } std::cout <<result<<std::endl; }
댓글을 작성하려면
로그인
해야 합니다.
kimmgsa 1년 전
아래코드는 시간초과가 발생하는 코드인데요
설계시점에서 이를 회피하기 위해 보통 어떻게 접근하시나요..