[hackerrank]Service Lane

https://www.hackerrank.com/challenges/service-lane

用RMQ做的,其实暴力也能过~

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    int n, t;
    cin >> n >> t;
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec[i];
    }
    vector<vector<int>> m(n);
    for (int i = 0; i < n; i++) {
        m[i].resize(10);
    }
    
    for (int d = 0; d < 10; d++) {
        for (int i = 0; i < n; i++) {
            if (d == 0) {
                m[i][d] = vec[i];
            } else {
                m[i][d] = m[i][d - 1];
                int subd = (int)pow(2, d - 1);
                int j = i + subd;
                if (j + subd <= n)
                    m[i][d] = min(m[i][d - 1], m[j][d - 1]);
            }
        }
    }
    while (t--) {
        int a, b;
        cin >> a >> b;
        int diff = (b - a + 1);
        int d = 0;
        int len = 1;
        for (; len <= diff; len*=2, d++);
        d--;
        len /= 2;
        int x = min(m[a][d], m[b - len + 1][d]);
        cout << x << endl;
    }
    return 0;
}

  

posted @ 2014-08-18 21:29  阿牧遥  阅读(151)  评论(0编辑  收藏  举报