51nod 1287 cannon (dichotomy/Line tree)

Source: Internet
Author: User
Tags time limit

1287 Cannon Title Source: codility Base time limit: 1 seconds space limit: 131072 KB Score: 40 Difficulty: 4-level algorithm topic collection focuses on a positive integer array of length m, which 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, the length of the array A and B respectively (1 <= m, n <= 50000)
2nd to M + 1 rows: 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.
Input example
9
1
2
0
4
3
2
1
5
7
2
8
0
7
6
5
3
4
5
6
5
Output example
2
2
2
4
3
3
5
6
7

Build another array Conno, which represents the ascending sequence of h[], using two points to find the high ground of the cannon, place its previous land height at +1 and then update the Highland height. See Code

const int maxn=5e4+10; int n,m; int H[MAXN],CONNO[MAXN]; int main () {Ios::sync_with_stdio (false);
    cin>>n>>m;
    int maxh=-1;
    memset (conno,0,sizeof (Conno));
        for (int i=1;i<=n;i++) {cin>>h[i];
        Maxh=max (Maxh,h[i]);
    Conno[i]=maxh;
    } int gun,pos;
        for (int i=1;i<=m;i++) {cin>>gun;
        if (Gun<=conno[0] | | gun>conno[n]) continue;
        Pos=lower_bound (Conno+1,conno+1+n,gun)-conno;
        h[pos-1]++;
    Conno[pos-1]=max (Conno[pos-1],h[pos-1]);
    } for (int i=1;i<=n;i++) cout<

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.