Hihocode #1299 Discount Tickets

Source: Internet
Author: User

Test instructions very simple is to give you two number n and m,n to indicate that there are n tickets, m for M query, the next n lines, two numbers per line, respectively, the time and price of the flight departure, the next M-line, each line of two number represents the most expensive flight of the two hours of the price. If no ticket is required, the output is "None". This question is a typical RMQ problem, which is the interval maximum value query problem. There are two solutions available.

1. The segment tree can be solved and is a bare topic of a line segment tree.

Segment Tree#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int N =    100005;int result;struct tree{int left;    int right; int value;}    Tree[n*4];void Build (int l, int r, int p) {tree[p].left = l;    Tree[p].right = R;    int mid = (l+r)/2;        if (L = = r) {tree[p].value = 0;    return;    } build (L, Mid, p*2);    Build (Mid+1, R, P*2+1); Tree[p].value = Max (Tree[p*2].value, tree[p*2+1].value);}    void update (int t, int value, int p) {int mid = (tree[p].left + tree[p].right)/2;        if (Tree[p].left = = tree[p].right) {tree[p].value = max (tree[p].value, value);    return;    } if (t<=mid) update (t, value, p*2);    else Update (t, value, p*2+1); Tree[p].value = Max (Tree[p*2].value, tree[p*2+1].value);}    void query (int l, int r, int p) {int mid = (tree[p].left + tree[p].right)/2; if (Tree[p].left = = L && tree[p].right = = r) {result = max (result, tree[p]. value);    return;    } if (r <= mid) query (L, R, P*2);    else if (L > Mid) query (L, R, P*2+1);        else {query (L, Mid, p*2);    Query (mid+1, R, P*2+1);    }}int Main () {int n, m;    scanf ("%d%d", &n, &m);    Build (1, N, 1);        for (int i=0; i<n; i++) {int T, V;        scanf ("%d%d", &t, &v);    Update (t, V, 1);        } for (int i=0; i<m; i++) {int L, R;        result = 0;        scanf ("%d%d", &l, &r);        Query (L, r, 1);        if (result = = 0) cout<< "None" <<endl;    else cout<<result<<endl; } return 0;}
2. The ST algorithm can also be solved.

Algorithm analysis: The preprocessing time complexity is O (N*log (n)), but the complexity of the query is O (1), preprocessing is to use a two-dimensional array data[a][b] to represent the maximum value from a to 2^b, and then use a DP to update all values, and finally query the output results.

RMQ Resolving Interval-Maximum problems # include <iostream> #include <cstring> #include <cstdio> #include <cmath>using namespace Std;const int N = 100005;int input[n];int price[n][40];void rmq (int num) {int tmp = INT (log (num*1.0)/log (2).    0));    for (int i=1; i<=num; i++) price[i][0] = Input[i];                for (int j=1; j<=tmp; j + +) for (int i=1; i<=num; i++) {if (i+ (1<<j) <= num)        PRICE[I][J] = max (price[i][j-1], price[i+ (1<< (j-1))][j-1]);    }}int Main () {int n, m;    scanf ("%d%d", &n, &m);    memset (input, 0, sizeof (input));        for (int i=0; i<n; i++) {int pos, PRI;        scanf ("%d%d", &pos, &pri);    Input[pos] = max (PRI, Input[pos]);    } RMQ (N-1);        for (int i=0; i<m; i++) {int result, l, R;        scanf ("%d%d", &l, &r);        int tmp = log ((r-l+1) *1.0)/log (2.0);        result = Max (price[l][tmp], price[r-(1<<tmp) +1][tmp]);           if (!result) cout<< "None" <<endl;    else cout<<result<<endl; } return 0;}



Hihocode #1299 Discount Tickets

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.