Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
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.