Assignment (HDU_5289)

来源:互联网 发布:刀路编程工资高吗 编辑:程序博客网 时间:2024/05/22 09:57

Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3138    Accepted Submission(s): 1441


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
24 23 1 2 410 50 3 4 5 2 1 6 7 8 9
 

Sample Output
528
Hint
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
 

Author
FZUACM

题目大意:给出一组数字序列,问从这组数字中能够找出多少种组合,要求组合为连续的,组合中各个数间的差值不超过k.

解题思路:设置两个指针i,j,然后进行移动,

代码如下:


#include"iostream"#include"cstdio"#define INF 0x3f3f3fusing namespace std; const int maxn=100005;int num[maxn];long long cnt,_max,_min;int max(int x,int y){return x > y ? x : y;}int min(int x,int y){return x < y ? x : y;}int main(){int T,n,k,t;scanf("%d",&T);while(T --){cnt = 0;_max = -INF,_min = INF;scanf("%d%d",&n,&k);for(int i = 1;i <= n;i ++)scanf("%d",&num[i]);for(int i = 1,j = 1;i <= n; i++){_min = min(_min,num[i]);_max = max(_max,num[i]);if(_max - _min < k) cnt += (i - j + 1);else{_max = num[i],_min = num[i];j = i;while(num[j] - _min < k && _max - num[j] < k){cnt ++;_min = min(_min,num[j]);_max = max(_max,num[j]);j --;}j ++;}}printf("%I64d\n",cnt);}}





题目大意:给出一组数字序列,问从这组数字中能够找出多少种组合,要求组合为连续的,组合中各个数间的差值不超过k.

解题思路:设置两个指针i,j,然后进行移动,

代码如下:
0 0
原创粉丝点击