Codeforces 831 D Office Keys (DP)

Source: Internet
Author: User

Meaning

n individuals going to a place p and give everyone a one-dimensional coordinate a[i], go to this place need a key, a total of K key, given the coordinates of each key b[i], ask N people finally arrived at this place how much time, each person to walk a unit distance need a unit time.


Ideas for solving problems:


Looks very complicated question, has the destination coordinates and the key coordinates, actually thinks carefully, after takes the key, then also needs the time also to decide, therefore only needs to consider the person and the key relations can.

DP[I][J] can be defined as the maximum time for the former I person to take I to the destination in the former J key.

The transfer is dp[i][j]=min (Dp[i][j-1], Max (Dp[i-1][j-1], ABS (a[i]-b[i) +abs (b[i]-p));


Don't forget to sort, don't forget long long and you're done.


Code:

#include <bits/stdc++.h>
#define LL long-long 
using namespace std;
Const LL INF=1E16;
LL dp[1005][2005];
LL a[1005];
LL b[2006];
LL p;


int main ()
{
    int n, I, J, K;
    cin>>n>>k>>p;
    For (I=1 i<=n; i++) scanf ("%lld", &a[i));
    Sort (a+1, a+n+1);
    For (I=1 i<=k; i++) scanf ("%lld", &b[i));
    Sort (b+1, b+k+1);
    for (j=0; j<=k; j + +) dp[0][j]=0;
    For (I=1 i<=n; i++)
    {for
        (j=i; j<=k; j + +)
        {
            if (i==j)
            {
                Dp[i][j]=max (dp[i-1][ J-1],abs (A[i]-b[j]) +abs (b[j]-p));
                Continue;
            }
            Dp[i][j]=min (Dp[i][j-1], Max (Dp[i-1][j-1],abs (a[i]-b[j)) +abs (b[j]-p));
        }
    J=n;
    For (i=n i<=k; i++)
    {
        if (dp[n][i]<dp[n][j)) j=i;      cout<<dp[n][i]<<endl;
    }
    cout<<dp[n][j]<<endl;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.