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 some staffs who were in the same group. In a group, the difference of the
ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.
Input
In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0<k<=10^9),indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less
than k. The second line contains n integers:a[1],a[2],…,a[n](0<=a[i]<=10^9),indicate the i-th staff’s ability.
Output
For each test,output the number of groups.
Sample Input
2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9
Sample Output
5
28
Hint
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
Source
/**
hdu5289||2015多校聯合第一場1002貪心+RMQ
題目大意:找出連續子區間,其最大最小值只差小於k
解題思路:用rmq維護區間最大最小值只差,貪心掃一遍即可,複雜度O(nlogn)
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int N=200005;
int a[N],n,m;
int dp1[N][30];
int dp2[N][30];
void RMQ_init(int n)
{
for(int i=1;i<=n;i++)
{
dp1[i][0]=a[i];
dp2[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)///白書上的模板,次行稍作改動,否則dp陣列要擴大一倍防止RE
{
dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
}
}
}
int rmq(int x,int y)
{
int k=0;
while((1<<(k+1))<=y-x+1)k++;
return max(dp1[x][k],dp1[y-(1<<k)+1][k])-min(dp2[x][k],dp2[y-(1<<k)+1][k]);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
RMQ_init(n);
int k=1;
long long sum=0;
for(int i=1;i<=n;i++)
{
while(rmq(k,i)>=m&&k<i)k++;
sum+=(i-k+1);
// printf("dis i k i-k+1:%d %d %d %d\n",rmq(k,i),i,k,i-k+1);
}
printf("%I64d\n",sum);
}
return 0;
}
相關文章
- HDU 5389 Zero Escape(2015年多校聯合第八場 動態規劃)動態規劃
- 24牛客多校第一場
- HDU 5375 Gray code(2015年多校聯合 動態規劃)動態規劃
- 貪心
- 2024牛客多校第一場 - Mirror Maze
- 反悔貪心
- Supermarket(貪心)
- 【貪心】【二分】[NOIP2015]跳石頭
- hdu5336 多校聯合第四場1010 模擬+bfs優先佇列佇列
- 2014多校聯合第9場1011題||hdu 4970 樹狀陣列陣列
- noip多校聯考總結
- 貪心模式記錄模式
- 貪心、構造合集
- 貪心演算法演算法
- 反悔貪心雜題
- 2014多校聯合第十場A題||hdu 4971 最小割定理在最大權閉合圖上的應用
- 2014多校聯合第9場1006||hdu 4965 矩陣乘法和快速冪矩陣
- RMQ_第一彈_Sparse TableMQ
- Least Cost Bracket Sequence(貪心)ASTRacket
- 牛客 tokitsukaze and Soldier 貪心
- HDU 4550卡片遊戲(貪心)遊戲
- 24/03/20 貪心(一)
- 7.5 - 貪心篇完結
- 貪心 做題筆記筆記
- 「貪心」做題記錄
- 陳老師的多校聯合 D題 字串處理起來挺麻煩字串
- 2024牛客多校第一場A Bit Common & A Bit More Common
- 「模擬賽」多校 A 層聯訓 16
- 學一下貪心演算法-學一下貪心演算法演算法
- 貪心演算法(貪婪演算法,greedy algorithm)演算法Go
- 【例題】【連結串列】NKOJ3499【2015多校聯訓6】密碼密碼
- HDU 5813 Elegant Construction (貪心)Struct
- 淺談貪心與動歸
- 貪心演算法Dijkstra演算法
- 貪心(入門簡單題)
- 9-貪心演算法演算法
- [反悔貪心] Add One 2
- 程式猿生存指南-63 貪心姑娘