2024牛客暑期多校训练营10 K Doremy‘s IQ 2

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

Doremy is asked to test nnn contests. The difficulty of contest iii is ai​. In the following n days, Doremy has to test all n contests, one contest per day. Each contest should be tested only once, but can be tested in any order. Initially, Doremy's IQ is 0, and it may change after each test.

If Doremy chooses to test a contest with difficulty d and now Doremy's IQ is x, the following happens:

  • if d>x , Doremy will feel inspired, so her IQ increases by 1;
  • if d<x , Doremy will feel fooled, so her IQ decreases by 1;
  • if d=x , Doremy will feel satisfied, and her IQ will not change.

Doremy wants to encounter as many d=x-type contest as possible. Please help Doremy find out the number.

输入描述:

The input consists of multiple test cases. The first line contains a single integer t (1≤t≤) - the number of test cases. The description of the test cases follows.

The first line contains one integer n (1 ≤n≤ ) - the number of contests.

The second line contains nnn integers a1,a2,⋯ ,an (−n≤a1≤a2≤⋯≤an≤n) - the difficulty of each contest.

It is guaranteed that the sum of nnn over all test cases does not exceed 3⋅.

输出描述:

For each test case, output the maximum number of d=x-type contests that Doremy can encounter.

示例1

输入

5
5
1 2 3 4 5
5
-2 -1 0 1 2
8
-8 -3 -2 -2 -2 1 1 8
1
0
2
0 1

输出

2
2
4
1
1

题目大意:一共有 n 场比赛 ,每场比赛的难度 为 ai ,我的初始智商为 id = 0 ,我可以任意选择 1 ~ n 之间的比赛(每场比赛只能进行一次),这是会有三种情况 :如果 ai > id ,那么 id+1;如果 ai < id ,那么 id - 1; 如果 ai = id ,那么没有影响。问:ai = id 的比赛最多有几场?

思路:这是一道贪心题,我们只需通过跑双指针记录最大值即可。这题需要跑两次双指针,第一次从 大于等于 0 的第一个数开始往右边跑,每向右边跑一位就要算一遍从当前点最大能向左边跑到哪一个值,将这段区间长度与最终答案取最值即可。第二次从小于等于 0 的第一个数开始往左边跑,每向左进一位就要算一遍从当前点最大能向右边跑到哪一个值,将这段区间长度与最终答案取最值即可。(具体操作详见代码)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.tie(0);
int a[100005];
signed main()
{
	IOS
	int _;
	cin >> _;
	while(_--){
		int n;
		cin >> n;
		for(int i=1;i<=n;i++){
			cin >> a[i];
		}
		int ans=0,cnt=1,idx=n;//cnt,idx是两根指针,ans是记录最大值的 
		for(int i=1;i<=n;i++){//第一次先跑非负数,再回头跑负数 
			if(a[i]<0) continue;//找到第一个非负数 
			if(a[i]>n-i) break;//a[i]不能超过 n-i ,因为从0跑到 a[i] 需要消耗a[i]个比a[i]大的数,如果n-i<a[i]的话,说明没有a[i]个数比a[i]大,即i跑不到这个下标 
			while(a[cnt]<0 && a[i]-a[cnt]>cnt-1) cnt++;//a[i]-a[cnt]是要花费比a[cnt]小的个数,cnt-1 是最多能花费比a[i]小的个数 
			ans=max(ans,i-cnt+1);//取最大值 
		}
		for(int i=n;i>=1;i--){//第二次先跑非正数,再回头跑正数 ,具体操作同上 
			if(a[i]>0) continue;
			if(a[i]<1-i) break;
			while(a[idx]>0 && a[i]-a[idx]<idx-n) idx--;
			ans=max(ans,idx-i+1);
		}
		cout << ans << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值