Codeforces 831D Office Keys[Dp/2점]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n,k andp (1 ≤ n ≤ 1 000,n ≤ k ≤ 2 000,1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integersa1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integersb1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50
20 100
60 10 40 80
Output
50
Input
1 2 10
11
15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point40 and go with it to the office located at point50. He spends 30 seconds. The person located at point100 can take the key located at point80 and go to the office with it. He spends 50 seconds. Thus, after50 seconds everybody is in office with keys.
제목 대의:
N 명 드릴게요. K 열쇠가 있어요. 종점은 P예요.
모든 사람이 열쇠를 가지고 결승점까지 얼마나 걸릴지 물어보면 모든 사람이 할 수 있다.
아이디어:
Dp[i][j]를 설정하면 전 i개인이 전 j가 열쇠에서 모두 열쇠를 받았고 종점까지의 최우선, 최장 소요 시간을 나타낸다.
그러면 다음과 같습니다.
if(i=j)Dp[i][j]=max(Dp[i-1][j-1], 제i개인이 열쇠 j에 도착하고 결승점에 도착하는 비용);
else Dp[i][j]=min(Dp[i][j-1],max(Dp[i-1][j-1],제 i 개인이 열쇠 j에 도착하고 결승점에 도착하는 비용);
Ac 코드:
#include
#include
#include
#include
using namespace std;
int a[150000];
int b[150000];
int dp[1005][1005];
int main()
{
int n,k,p;
while(~scanf("%d%d%d",&n,&k,&p))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=k;i++)scanf("%d",&b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+k);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=k;j++)
{
if(i==j)
{
dp[i][j]=max(dp[i-1][j-1],abs(p-b[j])+abs(a[i]-b[j]));
continue;
}
dp[i][j]=min(dp[i][j-1],max(dp[i-1][j-1],abs(p-b[j])+abs(a[i]-b[j])));
}
}
int output=INT_MAX;
for(int i=n;i<=k;i++)
{
output=min(output,dp[n][i]);
}
printf("%d
",output);
}
}
이분 사고방식:
2분의 시간, 욕심 체크.
사람마다 가능한 한 왼쪽 열쇠를 들면 된다.
Ac 코드:
#include
#include
#include
#include
using namespace std;
#define ll __int64
ll a[150000];
ll b[150000];
ll n,k,p;
ll Slove(ll mid)
{
ll output=0;
ll now=1;
for(ll i=1;i<=n;i++)
{
while(now<=k&&abs(b[now]-a[i])+abs(b[now]-p)>mid)
{
now++;
}
if(now<=k)
{
now++;
output++;
}
else break;
}
if(output>=n)return 1;
else return 0;
}
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&k,&p))
{
for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]);
for(ll i=1;i<=k;i++)scanf("%I64d",&b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+k);
ll ans=-1;
ll l=0;
ll r=2000000000+50;
while(r>=l)
{
ll mid=(l+r)/2;
if(Slove(mid)==1)
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%I64d
",ans);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
codeforce 991E(조합수 반복 검색)제목 링크: 링크 열기 클릭 샤오밍이 헷갈릴 때 본 자동차 번호판 숫자를 실제 숫자와 비교해 보자. ① 실제로 나온 숫자는 샤오밍이 다 봤다 ② 샤오밍은 같은 숫자만 보고 적을 수는 없다 ③ 차량 번호는 전도 제로가...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.