zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    祥解使用 SQL Server 2005/2008 发送数据库邮件
    将Virtual Server 2005 的虚拟硬盘(VHD)文件复制到HyperV
    Win2008 64位 iis7 环境下安装Discuz!NT
    seo
    找了好办天才找到的asp生成图片学习一下
    1.1 开发背景 (学习图书连载csdn
    我眼中的C# 3.0
    google编程
    反射之反思
    我要减肥
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/12397010.html
Copyright © 2011-2022 走看看