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
  • 相关阅读:
    Java根据html模板创建 html文件
    java.lang.NumberFormatException: For input string:"filesId"
    使用java开源工具httpClient及jsoup抓取解析网页数据
    JBPM5流程设计器jbpm-designer-2.4.0.Final-tomcat.war的部署没法访问的问题
    MyEclipse8.0 注册码生成代码
    图片转为byte[]、String、图片之间的转换
    java中Xml、json之间的相互转换
    java二维码小试牛刀
    进度条脚本
    如何制作一寸、二寸、六寸照片。以后不用再去照相馆了!!! 转~版本更新
  • 原文地址:https://www.cnblogs.com/cyd308/p/4668582.html
Copyright © 2011-2022 走看看