CF1840C 滑雪方案

Ski Resort

题面翻译

给定 n n n k k k q q q 和一个长度为 n n n 的序列 a a a

a a a 中选出一个连续的序列,序列的长度需大于等于 k k k 并且序列中的最大值小于等于 q q q

问,共有多少种选法?

题目描述

Dima Vatrushin is a math teacher at school. He was sent on vacation for n days for his good work. Dima has long dreamed of going to a ski resort, so he wants to allocate several consecutive days and go skiing. Since the vacation requires careful preparation, he will only go for at least k days.

You are given an array a containing the weather forecast at the resort. That is, on the i -th day, the temperature will be a_i degrees.

Dima was born in Siberia, so he can go on vacation only if the temperature does not rise above q degrees throughout the vacation.

Unfortunately, Dima was so absorbed in abstract algebra that he forgot how to count. He asks you to help him and count the number of ways to choose vacation dates at the resort.

输入格式

The first line of the input contains an integer t ( 1 ≤ t ≤ 10^4 ) — the number of test cases.

Then follow the descriptions of the test cases.

The first line of each test case contains three integers n , k , q ( 1 ≤ n ≤ 2 * 10^5
, 1 ≤ k ≤ n , -10^ 9 ≤ q ≤ 10^9 ) — the length of the array a , the minimum number of days at the resort, and the maximum comfortable temperature for Dima.

The second line of each test case contains $ n $ integers a1, a2, a3, …, a_n ( -10^ 9 ≤ ai ≤ 10^9 ) — the temperature at the ski resort.

The sum of all n values over all test cases does not exceed 2 *10^5 .

输出格式

Output t integers, each of which is the answer to the corresponding test case — the number of ways for Dima to choose vacation dates at the resort.

样例 #1

样例输入 #1

7
3 1 15
-5 0 -10
5 3 -33
8 12 9 0 5
4 3 12
12 12 10 15
4 1 -5
0 -1 2 5
5 5 0
3 -1 4 -5 -3
1 1 5
5
6 1 3
0 3 -2 5 -4 -4

样例输出 #1

6
0
1
0
0
1
9

提示

In the first test case of the example, Dima can go on any day, so the suitable dates for him are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3].

In the second and fourth test cases of the example, Dima cannot go on any day due to the high temperature, so there are no suitable dates.

In the third test case of the example, Dima can only go on the dates [1, 2, 3].

分析

从n个连续的1中选择k~n个连续的1,共有多少种选择方式,举例n=9,k=1,从图中可以得出,如果是k=1,有9种选择方式,k=2,有8种选择方式,因为从第一个1开始选,选到第九个1,就不能再选了,从第九个1开始就选不了两个1了,以此类推,k=9,只有1种选择方式。
很明显这是一个等差数列,确定第一项和最后一项,使用等差数列公式即可求得选择连续的1总和,若从k=2开始,则第一项为8,即n-k+1,最后一项为1,个数也是n-k+1,使用公式(an+a1)*n/2即可求得,
(n-k+1+1)(n-k+1)/2。
想到等差数列,再做这道题就简单了。
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int a[N];
long long ans,cnt;
int main()
{
	int n,k,q,t;
	cin>>t;
	while(t--)
	{
		cin>>n>>k>>q;
		cnt=0,ans=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			if(a[i]<=q)
				cnt++;
			else
			{
				if(cnt>=k)
					ans+=(cnt-k+1)*(cnt-k+2)>>1;//因为n的最大值为2*10^5,所以两个cnt相乘会超出整数范围, 
				cnt=0;	//接上,所以ans要定义为long long,为了 (cnt-k+1)*(cnt-k+2)二者相乘不溢出, 
			}//接上,cnt同样要定义成long long。 
		}
		if(cnt>=k)
			ans+=(cnt-k+1)*(cnt-k+2)>>1;
		cout<<ans<<endl;
	}
	
	return 0;	
 } 

注意ans ,cnt 都要定义成long long类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值