您的位置:首页 > 移动开发

Codeforces 602B - Approximating a Constant Range

2018-02-23 21:56 344 查看
题目链接: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;
}


噢,不懂就要问(当然不是问我,问大佬问师兄)!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces