51nod 1287 cannon (good exercise thinking)

Source: Internet
Author: User

Title: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1287

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, the length of the array A and B (1 <= m, n <= 50000) 2nd to M + 1 lines: 1 numbers per line, indicating 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).
Outputs a total of M rows, one number per line, corresponding to the final terrain height.
Input example
9 1112043215728076534565
Output example

Problem Solving Ideas:

The height of the shells is within 10^6, so we can find where it hit each height, and it's easy to think of the location of each height attack.

Must be non-diminishing, so it can be found in linear time, and then, for each projectile, it will only increase the height of the terrain in front of it by 1, which is

At most it is possible to change the position of a high attack, and it is better to judge the update.


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=50000+1000;int a[maxn];int b[maxn];int c[maxn*20];int Main () {    int m,n;    scanf ("%d%d", &n,&m);    for (int i=0;i<n;i++)    scanf ("%d", &a[i]);    for (int j=0;j<m;j++)    scanf ("%d", &b[j]);    int h=0;    for (int i=0;i<m;i++)    H=max (H,b[i]);    int j=0;    for (int. i=0;i<=h;i++)    {        while (a[j]<i&& (J<n))        j + +;        c[i]=j;    }    for (int i=0;i<m;i++)    {        int te=c[b[i]];        if (te>0&&te<n)        {            a[te-1]++;            if (c[a[te-1]]>=te)            c[a[te-1]]=te-1;        }    }    for (int i=0;i<n;i++)    {        printf ("%d\n", A[i]);    }    return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

51nod 1287 cannon (good exercise thinking)

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.