hdu 5289(二分+RMQ) Assignment

题意:

给一个序列,然后求出连续的序列中最大和最小值之差小于k的个数。

思路

二分+ST

枚举每一个位置。二分下标,找一个最大的区间满足区间内最大最小值相差小于k,当前这个位置对于答案的贡献就是这个区间长度。

求一个静态数组的区间最大最小值,用ST算法就好了。

参考code:

/*
 #pragma warning (disable: 4786)
 #pragma comment (linker, "/STACK:0x800000")
 */
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <utility>
using namespace std;

template< class T > T _abs(T n) { return (n < 0 ? -n : n); }
template< class T > T _max(T a, T b) { return (!(a < b) ? a : b); }
template< class T > T _min(T a, T b) { return (a < b ? a : b); }
template< class T > T sq(T x) { return x * x; }
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template< class T > bool inside(T a, T b, T c) { return a<=b && b<=c; }

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define F(i, n) for(int (i)=0;(i)<(n);++(i))
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define repok(i, s, t, o) for(int (i)=(s);(i)<=(t) && (o);++(i))
#define MEM0(addr) memset((addr), 0, sizeof((addr)))
#define MP(x, y) make_pair(x, y)
#define REV(s, e) reverse(s, e)
#define SET(p) memset(pair, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define MEM(p, v) memset(p, v, sizeof(p))
#define CPY(d, s) memcpy(d, s, sizeof(s))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define SZ(c) (int)c.size()
#define PB(x) push_back(x)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pii pair< int, int >
#define psi pair< string, int >
#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid, r, u << 1 | 1
#define debug(x) cout << #x << " = " << x << endl

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int MAXN = 100010;
int dp1[MAXN][20];
int mm1[MAXN];
int dp[MAXN][20];
int mm[MAXN];
void initRMQ1(int n,int b[]) {
    mm1[0] = -1;
    for(int i = 1; i <= n; i++) {
        mm1[i] = ((i&(i-1)) == 0)?mm1[i-1]+1:mm1[i-1];
        dp1[i][0] = b[i];
    }
    for(int j = 1; j <= mm1[n]; j++)
        for(int i = 1; i + (1<<j) -1 <= n; i++)
            dp1[i][j] = max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
}
int rmq1(int x,int y) {
    int k = mm1[y-x+1];
    return max(dp1[x][k],dp1[y-(1<<k)+1][k]);
}

void initRMQ2(int n,int b[]) {
    mm[0] = -1;
    for(int i = 1; i <= n; i++) {
        mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
        dp[i][0] = b[i];
    }
    for(int j = 1; j <= mm[n]; j++)
        for(int i = 1; i + (1<<j) -1 <= n; i++)
            dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq2(int x,int y) {
    int k = mm[y-x+1];
    return min(dp[x][k],dp[y-(1<<k)+1][k]);
}

int n,k;
int a[MAXN];

int judge(int x,int y){
    if(rmq1(x,y) - rmq2(x,y) < k)
        return 1;
    return 0;
}

int main() {
    //READ("in.txt");
    int T;
    scanf("%d",&T);
    int l,r,m;
    ll ans;
    while(T--){
        scanf("%d%d",&n,&k);
        ans=0;
        rep(i,1,n) scanf("%d",&a[i]);
        initRMQ1(n,a);
        initRMQ2(n,a);
        rep(i,1,n){
            l = i,r = n;
            int e = i;
            while(l <= r){
                //cout << m << endl;
                m = (l+r) >> 1;
                if(judge(i,m)){
                    e = m;
                    l = m + 1;
                }else{
                    r = m - 1;
                }
            }
            //cout << ans << endl;
            ans += (e-i+1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-23 23:33:01

hdu 5289(二分+RMQ) Assignment的相关文章

HDU 5289 Assignment(二分+RMQ-ST)

Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3813    Accepted Submission(s): 1771 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered

HDU - 1845 Jimmy’s Assignment (二分匹配)

Description Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignment is to find a maximum matching in a special kind of graph. This graph is undirected, has N vertices and each vertex has degree 3. Furthermore, the gr

hdu 1845 Jimmy’s Assignment (二分图)

Jimmy's Assignment Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 896    Accepted Submission(s): 379 Problem Description Jimmy is studying Advanced Graph Algorithms at his university. His most

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries + BIT 就行了.时间复杂度O(N log N). 速度垫底了QAQ 你们都会SAM.... ---------------------------------------------------------------------- #include<cmath> #include<c

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in

zoj-3726-Alice&#39;s Print Service【二分+rmq】

答案就是 q*p[i](i为最大的s[i]<=q的位置,二分),s[i+1]*p[i+1],....s[n]*p[n] 之中最小的 要快速的查找 (i,n)区间的s*p查询手段有很多.这里我用的rmq. /* 就是一个打印分段收费政策,印的越多,单张价格越低, 输入需要印刷的数量,求最小印刷费用 打印k页的资料,给出n中付费方案, 一次打印超过s1但不超过s2的每页收费p1, 超过s2不超过s3的收费p2.....数据保证 0=s1<s2<...<sn,p1>=p1>=