CF1692G 2^Sort 题解

该问题要求找到一个数组中满足特定条件的长度为k+1的子序列数量。条件是子序列中的元素乘以2的幂后严格递增。给出的解决方案包括对原数组进行模拟下标操作,计算一个辅助数组f,并利用前缀和来统计满足条件的子序列个数。
摘要由CSDN通过智能技术生成

题目

链接

https://www.luogu.com.cn/problem/CF1692G

字面描述

题面翻译

给你一个长度为 n   ( ∑ n < 2 ⋅ 1 0 5 ) n \ (\sum n < 2\cdot 10^5) n (n<2105) 的数组 a a a,问你在这个数组中,有多少个长度为 k + 1   ( 1 ≤ k < n ) k + 1 \ (1\le k < n) k+1 (1k<n) 的区间,符合以下的条件:

2 0 ⋅ a i < 2 1 ⋅ a i + 1 < 2 2 ⋅ a i + 2 <  ⁣ ⋯ < 2 k ⋅ a i + k 注: i 为这个区间开始的位置 2^0 \cdot a_i < 2^1 \cdot a_{i + 1} < 2^2 \cdot a_{i + 2} < \dotsi < 2^k \cdot a_{i + k}\\ \footnotesize{注:i 为这个区间开始的位置} 20ai<21ai+1<22ai+2<<2kai+k注:i为这个区间开始的位置

tzyt翻译

题目描述

Given an array $ a $ of length $ n $ and an integer $ k $ , find the number of indices $ 1 \leq i \leq n - k $ such that the subarray $ [a_i, \dots, a_{i+k}] $ with length $ k+1 $ (not with length $ k $ ) has the following property:

  • If you multiply the first element by $ 2^0 $ , the second element by $ 2^1 $ , …, and the ( $ k+1 $ )-st element by $ 2^k $ , then this subarray is sorted in strictly increasing order.

More formally, count the number of indices $ 1 \leq i \leq n - k $ such that $ $KaTeX parse error: Can't use function '$' in math mode at position 84: …\cdot a_{i+k}. $̲ $

输入格式

The first line contains an integer $ t $ ( $ 1 \leq t \leq 1000 $ ) — the number of test cases.

The first line of each test case contains two integers $ n $ , $ k $ ( $ 3 \leq n \leq 2 \cdot 10^5 $ , $ 1 \leq k < n $ ) — the length of the array and the number of inequalities.

The second line of each test case contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \leq a_i \leq 10^9 $ ) — the elements of the array.

The sum of $ n $ across all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output a single integer — the number of indices satisfying the condition in the statement.

样例 #1

样例输入 #1
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12

样例输出 #1

2
3
2
3
1
0

提示

In the first test case, both subarrays satisfy the condition:

  • $ i=1 $ : the subarray $ [a_1,a_2,a_3] = [20,22,19] $ , and $ 1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19 $ .
  • $ i=2 $ : the subarray $ [a_2,a_3,a_4] = [22,19,84] $ , and $ 1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84 $ .

In the second test case, three subarrays satisfy the condition: - $ i=1 $ : the subarray $ [a_1,a_2] = [9,5] $ , and $ 1 \cdot 9 < 2 \cdot 5 $ .

  • $ i=2 $ : the subarray $ [a_2,a_3] = [5,3] $ , and $ 1 \cdot 5 < 2 \cdot 3 $ .
  • $ i=3 $ : the subarray $ [a_3,a_4] = [3,2] $ , and $ 1 \cdot 3 < 2 \cdot 2 $ .
  • $ i=4 $ : the subarray $ [a_4,a_5] = [2,1] $ , but $ 1 \cdot 2 = 2 \cdot 1 $ , so this subarray doesn’t satisfy the condition.

思路

对原数组进行模拟下标 x ( 2   n ) x(2~n) x(2 n)
根据题目
a x − 1 < 2 ⋅ a x a_{x-1}<2\cdot a_x ax1<2ax
∴ f x = 1 \therefore f_x=1 fx=1
否则, f x = 0 f_x=0 fx=0

f 数组 f数组 f数组进行前缀和预处理,就可以实现 O ( n ) \Omicron(n) O(n)时间复杂度的统计了。

代码实现

#include<bits/stdc++.h>
using namespace std;

const int maxn=2e5+10;
int t,n,k,ans;
int a[maxn],f[maxn];
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&k);
		int op=0;
		ans=0;
		memset(a,0,sizeof(a));
		for(int i=1;i<=n;i++){
			int x;
			scanf("%d",&x);
			if(2*x>op)a[i]=1;
			op=x;
		}
		for(int i=1;i<=n;i++){
			f[i]=f[i-1]+a[i];
			if(i-k-1<0)continue;
			if(f[i]-f[i-k]==k)++ans;
		}
		printf("%d\n",ans);
	}
	
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

materialistOier

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值