zoukankan      html  css  js  c++  java
  • 多校1 Assignment(枚举 二分 rmq) 1002

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


    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]
     
    Author
    FZUACM
     
    Source
     
    Recommend
    We have carefully selected several similar problems for you:  5299 5298 5297 5296 5295 
    哈哈
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 int n,k;
     8 int a[100005];
     9 int ma[100005][30],mi[100005][30];
    10 
    11 void inrmq()
    12 {
    13     int i,j;
    14     for(j=1;(1<<j)<=n;j++)
    15     {
    16         for(i=1;i+(1<<j)-1<=n;i++)
    17         {
    18             ma[i][j]=max(ma[i][j-1],ma[i+(1<<(j-1))][j-1]);
    19             mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
    20         }
    21     }
    22     return ;
    23 }
    24 
    25 int rmqjudge(int l,int r)
    26 {
    27     int kk=(int)(log(r-l+1.0)/log(2.0));
    28     return (max(ma[l][kk],ma[r-(1<<kk)+1][kk])-min(mi[l][kk],mi[r-(1<<kk)+1][kk]))<k;
    29 }
    30 
    31 int main()
    32 {
    33     int T;
    34     int i,j;
    35     scanf("%d",&T);
    36     while(T--)
    37     {
    38         scanf("%d %d",&n,&k);
    39         for(i=1;i<=n;i++)
    40         {
    41             scanf("%d",&a[i]);
    42             ma[i][0]=a[i];
    43             mi[i][0]=a[i];
    44         }
    45         inrmq();
    46         long long ans=0;
    47         int l,r,p,mid;
    48         for(i=1;i<=n;i++)
    49         {
    50             l=i;
    51             r=n;
    52             while(l<=r)
    53             {
    54                 mid=(l+r)>>1;
    55                 if(rmqjudge(i,mid))
    56                 {
    57                     p=mid;
    58                     l=mid+1;
    59                 }
    60                 else
    61                     r=mid-1;
    62             }
    63             ans=ans+p-i+1;
    64         }
    65         printf("%I64d
    ",ans);
    66     }
    67     return 0;
    68 } 
    View Code
  • 相关阅读:
    数据库索引的作用和长处缺点
    Spring的依赖注入
    Spring+Ibatis集成开发实例
    IOS开发之类和对象
    关于C++ const 的全面总结
    包管理器Bower使用手冊之中的一个
    项目总算完工了
    git reset and git checkout
    unity3D中协程和线程混合
    【剑指offer】左旋转字符串
  • 原文地址:https://www.cnblogs.com/cyd308/p/4668582.html
Copyright © 2011-2022 走看看