Doremy‘s IQ 2

K.Doremy's IQ 2

1.1思路

很明显,最优的方案是先走负的再走正的,或者是先走正的再走负的,而且使用增加/减少智力的竞赛一定是不包含最终方案里的。利用双指针维护一段答案即可

#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#include<functional>
using namespace std;
inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}//代替取模+
#define endl '\n'
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N = 7e6 + 10, M = 1e5 + 10;

inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int e[N],ne[N],h[M],w[N];
void solve()
{
    int n;
    cin >> n;
    std::vector<int> arr(n + 1);
    for(int i = 1; i <= n; i ++) cin >> arr[i];

    int cnt = 1;
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        if(arr[i] < 0) continue;
        if(arr[i] > n - i) break;
        while(arr[cnt] < 0 && arr[i] - arr[cnt] > cnt - 1) cnt ++;
        ans = max(ans,i - cnt + 1);
    }

    int idx = n;
    for(int i = n; i >= 1; i --)
    {
        if(arr[i] > 0) continue;
        if(arr[i]  * -1 > i - 1) break;
        while(arr[idx] > 0 && arr[idx] - arr[i] > n - idx) idx --;
        ans = max(ans,idx - i + 1);
    }
    cout << ans << endl;
}


signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    int tt;
    tt = read();
    //tt = 1;
    while(tt --)
    {
        solve();
    }   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值