Codeforces 602B - Approximating a Constant Range

题目链接:http://codeforces.com/problemset/problem/602/B

题意:给出一个数n,还有一个长度为n的序列,求这个数列中一个最长的子序列,这个子序列必须满足极差在1之内。
!!注意题干中 it’s guaranteed that |ai + 1 - ai| ≤ 1.

不会看题解还是看不会系列,题解有一种是边输入边处理系列,还有一种是用队列(又是不会看题解代码不懂,我太菜了),那就写一写前者吧。
上代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<cctype>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<ctime>
#define mst(a,b) memset(a,b,sizeof(a))
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
const int N = 1e9;
const int mod=(int)1e9+7;
const int INF=0x3f3f3f3f;
const long long LINF=(1LL<<62);
typedef long long LL;
//#define LOCAL
const double PI = acos(-1.0);
const int maxn=1e5+5;
using namespace std;

int a[maxn];

int main()
{
#ifdef LOCAL
    freopen("test.txt", "r", stdin);
#endif // LOCAL
    //ios::sync_with_stdio(false);
    int n, ans = 0; scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        int t; scanf("%d", &t);     //边输入边处理让我们不用考虑后面的情况,方便许多。
        if(a[t-1]>a[t+1]) ans = max(ans, i-max(a[t-2],a[t+1]));

        //这里的if是先考虑先减后增的情况,因为有保证相邻俩数差的绝对值在1以内
        //6 5 4 3 4 5, 于是就检验t-2和t+1即3和6哪个比较近,选3是因为为45的子序列
        //3 2 2 2 2,近似减后增(解释太勉强了(逃),选t+1的情况

        else ans = max(ans, i-max(a[t+2],a[t-1]));

        //这里就是先增后减的情况,类似上面的。

        a[t] = i;                   //更新t的位置
    }
    printf("%d\n", ans);
    return 0;
}

噢,不懂就要问(当然不是问我,问大佬问师兄)!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值