forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
56 lines (50 loc) · 1.3 KB
/
C.cpp
File metadata and controls
56 lines (50 loc) · 1.3 KB
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#define FOR(i,a,b) for(int i=(a),_b=(b); i <= _b; ++i)
using namespace std;
const int MN = 200111;
int n, a[MN];
int check(int pos) {
if (pos % 2 == 1) {
return a[pos] < a[pos-1] && a[pos] < a[pos+1];
}
else {
return a[pos] > a[pos-1] && a[pos] > a[pos+1];
}
}
int main() {
while (scanf("%d", &n) == 1) {
FOR(i,1,n) scanf("%d", &a[i]);
a[0] = MN;
if (n % 2 == 0) a[n+1] = 0;
else a[n+1] = MN;
vector<int> bad;
FOR(i,1,n) if (!check(i)) bad.push_back(i);
if ((int) (bad.size()) > 6) cout << 0 << endl;
else {
int res = 0;
for(int x : bad) {
FOR(i,1,n) if (i != x && (check(i) || i > x)) {
swap(a[i], a[x]);
bool good = 1;
for(int p : bad) if (!check(p)) good = 0;
if (!check(i)) good = 0;
if (!check(x)) good = 0;
res += good;
swap(a[i], a[x]);
}
}
cout << res << endl;
}
}
}