codeforces FF div.1

本文从WordPress迁移而来, 查看全部WordPress迁移文章

题目链接:DZY Loves Sequences

题意 & 题解:

容易想明白的一个性质是,你最多只能将两段相邻的上升子段连起来

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define MAXN 100010

int n,a[MAXN];
vector<pii>seg;

void create_seg() {
int st = 0, ed = 0;
seg.clear();
for (int i = 1; i < n; i++) {
if (a[i] > a[i-1]) ed = i;
else {
seg.pb(mp(st,ed));
st = ed = i;
}
}
seg.pb(mp(st,ed));
/*
for (int i = 0; i < seg.size(); i++) {
int st = seg[i].first, ed = seg[i].second;
printf("[%d, %d] -- [%d, %d]\n", st, ed, a[st], a[ed]);
}
*/
}

inline int getLen(int id) {
int st = seg[id].first, ed = seg[id].second;
return ed-st+1;
}

int solve() {
int ans = 0;
int size = seg.size();
for (int ith = 0; ith < size; ith++) {
int ith_len = getLen(ith);
ans = max(ans, ith_len);
if (ith+1 == size) continue;
int next = ith + 1;
int next_len = getLen(next);
if (ith_len == 1 || next_len == 1)
ans = max(ans, ith_len + next_len);
else {
int v1 = a[seg[ith].second-1];
int v2 = a[seg[ith].second];
int v3 = a[seg[next].first];
int v4 = a[seg[next].first+1];
if (v2+1 < v4 || v1 < v3-1)
ans = max(ans, ith_len+next_len);
}
ans = max(ans, ith_len+1);
ans = max(ans, next_len+1);
}
return ans;
}

int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
create_seg();
printf("%d\n", solve());
}
return 0;
}