51nod 1287 Cannon two points

Source: Internet
Author: User
title: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1287 Test Instructions:

An array of positive integers of length m that represents the height of the terrain from left to right. Test a cannon, the shells parallel to the ground from left to right to fly, height is h, if the height of a terrain is greater than the height of the projectile flying H (A[i] >= h), the shells will be blocked and fall in I-1 place, then a[i-1] + 1. If H <= a[0], then this shell is invalid, if H > All A[i], this shell is not valid. Now the array B of the given n integers represents the height of the shells and calculates the final topography.
For example: terrain height a = {1, 2, 0, 4, 3, 2, 1, 5, 7}, Shell height B = {2, 8, 0, 7, 6, 5, 3, 4, 5, 6, 5}, the resulting terrain height is: {2, 2, 2, 4, 3, 3, 5, 6, 7}.
Input

Line 1th: 2 number m, n is separated by a space, respectively, for the length of the array A and B (1 <= m, n <= 50000)
2nd to M + 1 lines: 1 numbers per line representing the corresponding terrain height (0 <= a[i] <= 1000000).
The M + 2 to n + M + 1 lines, 1 numbers per line, indicate the height of the projectile (0 <= b[i] <= 1000000).

Output

Outputs a total of M rows, one number per line, corresponding to the final terrain height. Analysis:

As long as it is clear that the shells will only be the first one more than his >= to intercept, if the shells than AI high, then I behind the AI short can not stop. And then the problem soon falls out of the water ~

I am the second point, O (MLONGN). Look at the discussion of pre-treatment heights, and then you can do O (n). In fact, the meaning is the same. Code:

 #include <bits/stdc++.h> using namespace std; const int N = 5e4+4; int n,m,x,a[n],b[n];
    int main () {//freopen ("In.txt", "R", stdin);
    scanf ("%d%d", &n,&m);
        for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);
    B[i]=max (A[i],b[i-1]);
        } for (int i=0;i<m;i++) {scanf ("%d", &x);
        int K=lower_bound (b+1,b+1+n,x)-B; if (k>n| |
        K<=1) continue;
        a[k-1]++;
    if (B[k-1]<a[k-1]) b[k-1]=a[k-1];
    } for (int i=1;i<=n;i++) printf ("%d\n", A[i]);
return 0; }

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.