Approximating a Constant Range

原题地址:http://codeforces.com/contest/602/problem/B

题意
给定一个序列,满足abs(a[i+1]-a[i])<=1
要求找到最长的一个子序列[l,r]满足序列中最大值max和最小值之差小于等于1

题解
要求找到题意要求的最长序列,对应的序列有什么特征呢?
假如序列起点终点是l,r,那么应该有abs(A[l-1]-A[r])==2
所以我们枚举终点,对应的起点可以用一个p数组O(1)维护和查询
同时,要求这个序列中A[r]-1和A[r]+1不能同时出现,也可以用p数组实现判断

p[x]:x出现的最后位置
也就是上一个x出现的下标
有的时候因为x过大的缘故,需要离散化或者用map代替数组实现这一功能

#include<bits/stdc++.h>

using namespace std;
const int maxn=1e5;

int p[maxn+10];
int arr[maxn+5];

int main(void)
{
    #ifdef ex1
    freopen ("in.txt","r",stdin);
    #endif

    int n;
    scanf("%d",&n);

    for (int i=1;i<=n;++i)
    {
        scanf("%d",&arr[i]);
    }

    int ans=0;
    for (int i=1;i<=n;++i)
    {
        int x=arr[i];
        p[x+2]=i;

        int k=max(p[x+4],p[x]);
        if (p[x+3]>k && p[x+1]>k)
        {
            ans=max(ans,i-min(p[x+3],p[x+1]));
        }
        else
        {
            ans=max(ans,i-k);
        }
    }

    printf("%d\n",ans);

} 
时间: 2024-08-04 21:00:08

Approximating a Constant Range的相关文章

Codeforces 602B Approximating a Constant Range(想法题)

B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a suffi

codeforces 602B Approximating a Constant Range

B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a suffi

cf602B Approximating a Constant Range

B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output When Xellos was doing a practice course in university, he once had to measure the intensity of an effect t

【Virtual Judge】H - 大概做不来的题 Approximating a Constant Range

Description In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and yif and onl

codeforces#333 div2 B. Approximating a Constant Range

http://codeforces.com/contest/602/problem/B 这道题的两种做法, 滑窗+线段树查询区间最值: #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int

codeforce -602B Approximating a Constant Range(暴力)

CF上说是数据结构类型.表示不会. 题意是,找连续的并且任意两个数相差不超过1的最长串. 思路:题中说相邻的两个数相差不超过1: 那么cnt最小为2,cnt赋初值2:由于要相差不超过一,所以每个串的最大值最小值相差不能超过一, 那么从第三个元素开始,如果abs(a[i]-max)<=1&&abs(a[i]-min)<=1,就cnt++,表示该元素能加入上一个串,因为有新数字加入,所以更新max,和minn. 如果不符合的话cnt置为2就以a[i],开始向前找串. 1 #incl

【Virtual Judge】G - 一般水的题2-Approximating a Constant Range

Description When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number o

CF-Approximating a Constant Range

Description When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number o

CodeForces - 602B

题目链接:https://vjudge.net/problem/284704/origin Approximating a Constant Range When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equi