Codeforces 831D Office Keys[Dp/2점]

4723 단어 dp사유
D. Office Keys
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); } }

좋은 웹페이지 즐겨찾기