zoukankan      html  css  js  c++  java
  • 2015 Multi-University Training Contest 1 Assignment

    Assignment

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


    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
     
    解题:线段树+尺取法 或者 单调队列 然而我不会单调队列 也可以用st表
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 100010;
     5 struct node {
     6     int lt,rt,minv,maxv;
     7 } tree[maxn<<2];
     8 int d[maxn];
     9 inline void pushup(int v) {
    10     tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv);
    11     tree[v].maxv = max(tree[v<<1].maxv,tree[v<<1|1].maxv);
    12 }
    13 void build(int lt,int rt,int v) {
    14     tree[v].lt = lt;
    15     tree[v].rt = rt;
    16     if(lt == rt) {
    17         tree[v].minv = tree[v].maxv = d[lt];
    18         return;
    19     }
    20     int mid = (lt + rt)>>1;
    21     build(lt,mid,v<<1);
    22     build(mid+1,rt,v<<1|1);
    23     pushup(v);
    24 }
    25 int query(int lt,int rt,int v,bool qmax) {
    26     if(lt <= tree[v].lt && rt >= tree[v].rt)
    27         return qmax?tree[v].maxv:tree[v].minv;
    28     int ret = qmax?INT_MIN:INT_MAX;
    29     if(lt <= tree[v<<1].rt)
    30         ret = qmax?max(ret,query(lt,rt,v<<1,qmax)):min(ret,query(lt,rt,v<<1,qmax));
    31     if(rt >= tree[v<<1|1].lt)
    32         ret = qmax? max(ret,query(lt,rt,v<<1|1,qmax)):min(ret,query(lt,rt,v<<1|1,qmax));
    33     return ret;
    34 }
    35 int main() {
    36     int n,m,kase;
    37     scanf("%d",&kase);
    38     while(kase--) {
    39         scanf("%d%d",&n,&m);
    40         for(int i = 0; i < n; ++i)
    41             scanf("%d",d+i);
    42         build(0,n-1,1);
    43         int low = 0,high = 0;
    44         LL ret = 0;
    45         while(low <= high && high < n) {
    46             int minv = query(low,high,1,false);
    47             int maxv = query(low,high,1,true);
    48             if(maxv - minv < m) ret += high-low+1;
    49             if(maxv - minv >= m) low++;
    50             else high++;
    51         }
    52         cout<<ret<<endl;
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    oc之数组反序输出示例
    OC--有这么一个 整数 123456789,如何将这个整数的每一位数,从末位开始依次放入数组中,并遍历 倒序输出字符串
    oc--截取字符串(从网址中截取用户名和密码)
    iOS 第七期考核题(字符串以及字典的使用 数组内容转换成字符串)
    iOS 第六期考核题(字典的使用)
    iOS 第五期考核题(字典与数组嵌套,字典的排序/删除)
    Linux服务启动报错日志分析
    新手Linux命令-1
    新手Linux命令-2
    计划任务服务
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4665626.html
Copyright © 2011-2022 走看看