-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
6-dhlee777 #29
6-dhlee777 #29
Conversation
} | ||
} | ||
if (inf > abs(start - link)) //������ �ɷ�ġ���� �������� �ɷ�ġ ������ �۴ٸ� | ||
inf = abs(start - link); //inf�� �ּҰ� �ǵ��� �������ش�. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
소소한 팁인데 비교해서 갱신하는 코드는 아래와 같이 한 줄로 정리가 가능합니다!
// 1
inf = min(abs(start - link), inf);
// 2
inf = abs(start - link) > inf ? abs(start - link) : inf;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
재귀랑 DP는 많이 풀어서 어떤 식으로 사용하는지 감을 잡아야하는 것 같아요! 특히 둘 다 완성된 코드의 형태를 먼저 떠올리는게 도움이 되는 것 같아요 ㅎㅎ
재귀 함수는 특히 중간까지는 비슷하게 동작하고, 처음과 끝 부분만 다르게 동작하는 함수니까 처음과 끝이 어떤 식으로 생겼는지 먼저 생각해보고 나머지 중간 로직을 짜는 식으로 풀면 조금 도움이 될거예요 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
처음에는 ij
와 i
를 어떻게 구별해서 넣어줘야하지? 고민했는데 그냥 visited
에 true만 해놓으면 2중 for문으로 i
일때 j
일때 기준으로 ij
, ji
모두 처리해주는게 인상적이었습니다.
거기에 start
와 link
모두 visited
를 기준점으로 한번에 처리하는 것도 너무 좋았습니다.
점점 성장하고 있군뇨 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추가로 최대 20개의 요소들을 모두 비교해줄때 시간복잡도가 궁금하군요. 제출했을 당시에 얼마가 걸렸는지 알 수 있을까요?
20개의 요소 중 반만 비교를 해주기때문에 10(i)*10(j)*10(dfs)이 나올까요?
일단 for문으로 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
백트래킹은 언제 봐도 어렵네요. 절반을 돈 선수는 방문처리해주고 아니면 계속 확인하면서 재귀적으로 처리하는군요. 풀이 잘 보고 갑니다
🔗 문제 링크
https://www.acmicpc.net/problem/14889
✔️ 소요된 시간
하루
✨ 수도 코드
하루 동안 계속 생각해봤지만 도저히 아이디어가 떠오르지 않아서 답을 봐버렸다..
일단 이 문제는
총 n명의 선수(짝수)를 두팀(start와 link)로 나누는데
각 선수끼리 같은팀이 됐을때 능력치가 있다. 예를들어stats[2][3]
은2번선수와 3번선수가 같은팀이됐을때능력치
를 나타낸다.변수 start
는 start팀의능력치 합
link
는 link 팀의능력치 합
을 나타낸다. 결국 구하고자하는것은start와 link의 차가 최소가 되는 값 inf
이다.풀이과정(dfs)
1.
현재선수num
부터 제일마지막선수
까지 탐색을해준다.2.현재선수에
방문 표시
를 해주고카운트를 1증가
시키고다음선수로dfs
진행3.다른경우의 수를 위해 탐색이 끝난후
visited[i]=0으로 초기화
만약
탐색한 선수의 수
가전체선수의 절반
이 된다면 밑의 코드를 실행한다.(반만 탐색해서start팀
으로 할당해주면 나머지는 자동으로link팀
처리)이중for문
을 통해stats 배열
을 탐색해주며start
에 둘의 시너지를 더해준다.link
에 둘의 시너지를 더해준다.두팀의 총 능력치차
를 구해준다음 만약 기존의 차보다 더 적다면inf가 최소가되도록 갱신
해준다.전체코드
📚 새롭게 알게된 내용
https://hagisilecoding.tistory.com/86
백트래킹
은 직관적이지가 않아서 머릿속으로재귀
를 계속 돌려보고 아무리 생각해봐도 너무 이해하기 어렵다... 백트래킹 문제를계속풀면서 감을 익혀야겠다