我有一个数列!

我有一个数列!

Time Limit: 20000/10000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others)

Problem Description

小晴天:“我有一个数列!”

小晴天:“我还要有很多很多的数列!”

于是小晴天就把这个数列的所有连续子数列写出来。

然后小晴天把每个连续子数列中的最大的数写出来。

那么,有多少个比K大呢?

Input

多组数据,首先是一个正整数t(t<=100),表示数据的组数

对于每组数据,首先是两个整数n(1<=n<=200000),K(0<=K<=10^9).,但所有数据中的n之和不超过1000000.

接下来是n个整数a[i](1<=a[i]<=10^9)

Output

对于每组数据,输出一个整数,表示最大元素大于K的连续子序列的个数。

Sample Input

2
3 2
1 2 3 
3 1 
1 2 3

Sample Output

3
5

Hint

对于样例一,共有6个连续子序列{1}{2}{3}{1,2}{2,3}{1,2,3}(注意{1,3}不满足题意,因为不连续)

其中最大元素大于2的共有3个{3}{2,3}{1,2,3}

对于样例二,大于1的连续子序列共有5个,{2}{3}{1,2}{2,3}{1,2,3}

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <math.h>
 5 #include <string.h>
 6 using namespace std;
 7 typedef long long ll;
 8 ll t,n,k,a[200005],d[200005],cou;
 9 /**
10 从前往后扫描一遍,对于a[i]>k的值,满足条件的序列数=i+1。
11 对于a[i]<=k的值,会等于d[i-1].序列范围至少是i到前面最大值的距离。序列数就会是最大值处的数目。
12 当然这样就把所有的范围都过一遍,不会出现漏的,把所有的值加起来。
13 */
14 int main()
15 {
16     scanf("%lld",&t);
17     while(t--)
18     {
19         scanf("%lld%lld",&n,&k);
20         cou=0;
21         memset(d,0,sizeof(d));
22         for(int i=0;i<n;i++)
23         {
24                 scanf("%lld",&a[i]);
25                 if(a[i]>k)
26                 {
27                     d[i]+=i+1;
28                 }
29                 else
30                 {
31                     if(i>0) d[i]=d[i-1];
32                     else d[i]=0;
33                 }
34                 cou+=d[i];
35         }
36         printf("%d
",cou);
37     }
38     return 0;
39 }
View Code
原文地址:https://www.cnblogs.com/linxhsy/p/4454395.html