hdu5289 2015多校联合第一场1002 Assignment

题意:给出一个数列,问其中存在多少连续子区间,其中子区间的(最大值-最小值)<k

思路:设dp[i]为从区间1到i满足题意条件的解,最终解即为dp[n];

此外 假设对于arr[i] 往左遍历 一直到arr[r] 此时从区间r到区间i满足(最大值-最小值)<k,再往左一位即越界 或者 不满足条件,此时有 dp[i]
= dp[i-1]
+ i - r
+ 1;

因为数据量大 往左遍历时 可能会超时 ,所以用rmq打表 查找r时用二分 就过了

代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100010
using namespace std;
void mys(int& res) {
    int flag=0;
    char ch;
    while(!(((ch=getchar())>=‘0‘&&ch<=‘9‘)||ch==‘-‘))
        if(ch==EOF)  res=INF;
    if(ch==‘-‘)  flag=1;
    else if(ch>=‘0‘&&ch<=‘9‘)  res=ch-‘0‘;
    while((ch=getchar())>=‘0‘&&ch<=‘9‘)  res=res*10+ch-‘0‘;
    res=flag?-res:res;
}
void myp(int a) {
    if(a>9)
       myp(a/10);
    putchar(a%10+‘0‘);
}
/********************the end of template********************/
int arr[N];
int sm[N][30], bg[N][30];
LL dp[N];
void rmq_init(int n) {
    for(int i = 0; i < n + 1; i++)   bg[i][0] = sm[i][0] = arr[i];
    for(int j = 1; (1 << j) <= n + 1; j++) {
        for(int i = 0; i + (1 << j) - 1 < n + 1; i++) {
            bg[i][j] = max(bg[i][j - 1], bg[i + (1 << (j - 1))][j - 1]);
            sm[i][j] = min(sm[i][j - 1], sm[i + (1 << (j - 1))][j - 1]);
        }
    }
}
int rmq_min(int left, int right){
    int kk = 0;
    w((1 << (kk+1)) <= right - left +1) kk++;
    return min(sm[left][kk], sm[right - (1<<kk) +1][kk]);
}
int rmq_max(int left, int right){
    int kk = 0;
    w((1 << (kk+1)) <= right - left +1) kk++;
    return max(bg[left][kk], bg[right - (1<<kk) +1][kk]);
}
int getR(int L, int k, int R) {
    int l = L, r = R, m, ans;
    while(l <= r) {
        m = (l + r) >> 1;
        int view = rmq_max(m, R) - rmq_min(m, R);
        if(view < k) {
            ans = m;
            r = m - 1;
        }
         else l = m + 1;
    }
    return ans;
}
int main(){
    int t;
    s(t);
    w(t--){
        int n, k;
        mem(dp);
        ss(n, k);
        for(int i=1; i<=n; i++){
            s(arr[i]);
        }
        rmq_init(n);
        dp[1] = 1;
        for(int i=2; i<=n; i++){
            int R = getR(1, k, i);
            dp[i] = dp[i-1] + i - R + 1;
        }
        cout<<dp[n]<<endl;
    }
    return 0;
}

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

时间: 2024-11-08 07:40:40

hdu5289 2015多校联合第一场1002 Assignment的相关文章

hdu5289||2015多校联合第一场1002贪心+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to

hdu 5288||2015多校联合第一场1001题

http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know ∑i

hdu5294||2015多校联合第一场1007 最短路+最大流

http://acm.hdu.edu.cn/showproblem.php?pid=5294 Problem Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu's at the entrance of the tomb while Dumb Zhang's at the end of it. The tomb is made up of many chambers, the total numb

HDU 5289 Assignment(多校联合第一场1002)

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

hdu 4865 Peter&amp;#39;s Hobby(2014 多校联合第一场 E)

Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 545    Accepted Submission(s): 237 Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf

2014多校联合第一场

1001:Couple doubi 暴力打表找规律可知,对于任意的p. (1^i+2^i+...+(p-1)^i)%p={ 非0     ,i%(p-1)==0 0        ,  i%(p-1)!=0 } 所以,结果就很显然了. #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stdlib.h> #include<c

hdu 4865 Peter&#39;s Hobby(2014 多校联合第一场 E)

题意:已知昨天天气与今天天气状况的概率关系(wePro),和今天天气状态和叶子湿度的概率关系(lePro)第一天为sunny 概率为 0.63,cloudy 概率 0.17,rainny 概率 0.2.给定n天的叶子湿度状态,求这n天最可能的天气情况 分析:概率dp设 dp[i][j] 表示第i天天气为j的最大概率,pre[i][j]表示第i天天气最可能为j的前一天天气,dp[i][j]=max(dp[i-1][k]+log(wePro[k][j])+log(lePro[j][lePos[i]]

hdu 4869 Turn the pokers (2014多校联合第一场 I)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1265    Accepted Submission(s): 465 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

hdu 4865 Peter&#39;s Hobby(2014 多校联合第一场 E)

Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 545    Accepted Submission(s): 237 Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf