Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
miro-ring
Algorithm
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Algorithm
/
2110.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (57 loc) · 1.92 KB
master
Breadcrumbs
Algorithm
/
2110.cpp
Copy path
Top
File metadata and controls
Code
Blame
65 lines (57 loc) · 1.92 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**************
parametric search라고도 부르는 이분 탐색을 통해 최솟값 혹은
최댓값을 구해내는 문제의 세번째 예제.
이 문제가 parametric 문제 예제 중에서 가장 보기 좋은 문제 인 듯.
어렵지만 이해하고 넘어갈 수 있는 부분이 많고 생각해야 할 부분이 많다.
*************/
#include <iostream>
#include <algorithm>
using namespace std;
int length[200000];
int n,c;
bool chk(int mid){
int count=1; //첫집에 공유기를 설치한다 가정.
int prev=length[0];
for(int i=1;i<n;i++) //첫 집에 이미 공유기가 설치되었기에 i는 1부터 시작한다.
{
if(length[i]-prev>=mid) //mid는 첫 x좌표와 마지막 x좌표의 중간
{
count++;
prev=length[i]; //비교할 때 계속해서 붙어있는 집 사이의 거리를 조회한다.
}
}
return count>=c;
} //이 bool함수를 만드는 것이 중요!
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>c;
for(int i=0;i<n;i++)
{
cin>>length[i];
}
sort(length,length+n);
int left = 1; //만약 left를 0으로 해버리면 mid가 0이되어 런타임 에러가 발생할 수 잇다.
int right = length[n-1]-length[0];
int ans = 0;
while(left<=right)
{
int mid=(left+right)/2;
if(chk(mid)){
if(mid>ans)
ans=mid;
left=mid+1; //count가 n보다 커지게 되어서 이 함수가 실행될 경우 자르는 길이가 더 켜져야하기 때문에 left를 mid+1로 설정한다.
}
else
right=mid-1;
}
cout<<ans;
}
/*********************
결국!!! mid를 잡아놓고 집 간의 거리가 mid를 넘는 경우의 총 횟수가 제시된 c를 넘을 경우
mid를 늘려나가는 것이고 집 간의 거리가 mid를 넘는 경우의 총 횟수가 제시된 c를 넘지 못한 경우
mid를 줄여 나가는 것이다.
잘 봐둬야 하는 문제이다.
********************/
You can’t perform that action at this time.