Codeforeces 344 E Read Time

Source: Internet
Author: User

Test instructions

n heads to read the m track, giving the head and the initial position of the disk (increment given), asking at least how long all tracks are to be read.


Ideas:

The answer is still very easy to think of, the difficulty is probably greedy.

Consider after determining the time t:

Because there are multiple heads, all the leftmost tracks must be read by the leftmost head, but it is not right to have the head read from the left when it is not once. The leftmost track is naturally read, but under the greedy mind, we should let the current head go farther to the right as it reads the leftmost track, because it allows more tracks to be read within the time t.

Of course there is also the case that all the tracks are on the right side of the current leftmost track, so go straight to the right.

If there is on the left, then the current head has two choices, one is to the right and then to the left, then to the right up (T (h[i]-p[j)))/2 distance, the second is to the left and then to the right, up to the right t-2* (H[i]-p[j]), this go to the maximum selection of the row, in turn this run h[i ]。


It is also important to note that the continue can be done if the left-most head is not reached within the time t.


Code:

#include <bits/stdc++.h> #define LL Long long using namespace std;
const int maxn=1e5+5;
LL H[MAXN];
LL H[MAXN];
LL O[MAXN];

BOOL BOOK[MAXN];
    int main () {int n, m;
    cin>>n>>m;
    int I, J;
    for (i=0; i<n; i++) {scanf ("%lld", &h[i]);
    } for (i=0; i<m; i++) scanf ("%lld", &o[i]);
        if (n==m) {for (i=0; i<m; i++) if (h[i]!=o[i]) break;
    if (i==m) return 0*printf ("0\n");
    } Long long r=1e15+5, l=1;
    Long long mid;
    Long Long ans=0;
    int s=0;
    Long long X;
        for (int e=0; e<150; e++) {mid= (l+r) >>1;
        s=0;
            for (i=0; i<n; i++) {x=mid;
            H[i]=h[i];

            if (ABS (H[i]-o[s]) >mid) continue;    
            if (O[s]>=h[i]) {for (j=s; j<m; J + +) {if (o[j]-h[i]>mid) break;
            } s=j;
                } else {Long Long e=h[i]-o[s];
                X=max (X-2*e, (X-E)/2);
                for (J=s; j<m; J + +) {if ((o[j]-h[i)) >x) break;
            } s=j;
        } if (s==m) break;
            } if (s==m) {r=mid-1;
        Ans=mid;
        } else {l=mid+1;

}} cout<<ans<<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.