Sticks Problem

题目大意:

i 和 j 之间数 k (i<k<=j) 满足2个条件:

1、a[i] < a[k]

2、a[k] <= a[j]

 

思路:

对于每一个数 a[i] 我们先预处理出它右边连续比它大的数的个数 

然后对于每一个数,我们去遍历它的个数区间,可以找到一个满足条件的 i 和 j ,不妨设 tmp = j - i

下一次我们直接就可以从 i + tmp + 1 开始继续重复上面的操作了

 

#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>
#include <cmath>
#include <sstream>
#include <iostream>

#define LL long long
#define INF 0x3f3f3f3f
#define ls nod<<1
#define rs (nod<<1)+1

const double eps = 1e-10;
const int maxn = 5e4 + 10;;
const LL mod = 1e9 + 7;

int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;}
using namespace std;

int a[maxn],q[maxn],r[maxn];
int cnt;



int main() {
    int n;
    while (cin >> n) {
        cnt = 1;
        int ans = -1;
        memset(q,0, sizeof(q));
        for (int i = 1;i <= n;i++) {
            cin >> a[i];
        }
        q[1] = n + 1;
        for (int i = n;i >= 1;i--) {
            while (cnt && a[q[cnt]] > a[i])
                cnt--;
            r[i] = q[cnt]-i-1;
            q[++cnt] = i;
        }
        int maxx,tmp;
        for (int i = 1;i <= n;i += tmp+1) {
            maxx = tmp = 0;
            for (int j = 1;j <= r[i];j++) {
                if (a[i+j] > maxx) {
                    maxx = a[i+j];
                    tmp = j;
                }
            }
            if (ans < tmp)
                ans = tmp;
        }
        if (ans <= 0)
            cout << -1 << endl;
        else
            cout << ans << endl;
    }
    return 0;
}

 

posted @ 2020-03-02 17:49  _Ackerman  阅读(241)  评论(0编辑  收藏  举报