POJ 2452 Sticks Problem (暴力或者rmq+二分)

题意:给你一组数a[n],求满足a[i] < a[k] < a[j] (i <= k <= j)的最大的 j - i 。

析:在比赛时,我是暴力做的,虽然错了好多次,后来说理解是rmq,我又用rmq写了一次,发现rmq还没有我暴力快,rwq 2000多,暴力才700.

暴力中加了一个优化条件就是前枚举 i 时,下一个 i 值不一定是i+1,而是满足条件中的最大值的位置。这样优化就是时间很短了。

如果用rmq,就得用两个dp数组分别记录最大值和最小值的下标,然后枚举 i,在i+1 - n-1这个区间中求第一个小于 a[i] 的数,然后再从 i+1 - 该数,

求最大的那个数的下标。不断更新答案即可。

代码如下:

暴力的代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){  return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){  return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];

int main(){
    while(scanf("%d", &n) == 1){
        for(int i = 0; i < n; ++i)  scanf("%d", a+i);
        int ans = 0;
        int x;
        for(int i = 0; i < n-ans; i = x+1){
            x = Min(x, a[i]);
            int mmax = a[i];
            x = i;
            for(int j = i+1; j < n; ++j){
                if(mmax < a[j]){
                    x = j;
                    mmax = a[j];
                }
                if(a[j] < a[i]) break;
                if(mmax <= a[j]){ ans = Max(ans, j-i); }
            }
        }
        printf("%d
", ans ? ans : -1);
    }
    return 0;
}

rmq的代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;

}
int dp1[maxn][20], dp2[maxn][20];
int a[maxn];
inline int Min_(int i, int j){ return a[i] < a[j] ? i : j; }
inline int Max_(int i, int j){ return a[i] < a[j] ? j : i; }

void rmq_init(){
    for(int i = 0; i < n; ++i)  dp1[i][0] = dp2[i][0] = i;
    for(int j = 1; (1<<j) <= n; ++j)
        for(int i = 0; i + (1<<j) - 1 < n; ++i){
            dp1[i][j] = Min_(dp1[i][j-1], dp1[i+(1<<(j-1))][j-1]);
            dp2[i][j] = Max_(dp2[i][j-1], dp2[i+(1<<(j-1))][j-1]);
        }
}

inline int rmqmin(int l, int r){
    int k = (int)(log(r-l+1.0) / log(2.0));
    return Min_(dp1[l][k], dp1[r-(1<<k)+1][k]);
}

inline int rmqmax(int l, int r){
    int k = (int)(log(r-l+1.0) / log(2.0));
    return Max_(dp2[l][k], dp2[r-(1<<k)+1][k]);
}

int solve(int i){
    int l = i+1, r = n-1;
    while(l < r){
        int mid = (l+r) >> 1;
        if(a[rmqmin(l, mid)] > a[i]) l = mid + 1;
        else r = mid;
    }
    return rmqmax(i, l);
}

int main(){
    while(scanf("%d", &n) == 1){
        for(int i = 0; i < n; ++i) scanf("%d", a+i);
        rmq_init();
        int ans = 0;
        for(int i = 0; i < n - ans - 1; ++i){
             int j = solve(i);
             ans = Max(ans, j-i);
        }
        printf("%d
", ans ? ans : -1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5978577.html