您的位置:首页 > 其它

Codeforces(653C)-C. Bear and Up-Down

2016-12-11 23:47 381 查看
原题链接

C. Bear and Up-Down

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is
called nice if the following two conditions are satisfied:

ti < ti + 1 for
each odd i < n;

ti > ti + 1 for
each even i < n.

For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are
nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are
not.

Bear Limak has a sequence of positive integers t1, t2, ..., tn.
This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and
swap elements ti and tj in
order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different.

Input

The first line of the input contains one integer n (2 ≤ n ≤ 150 000) —
the length of the sequence.

The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 150 000)
— the initial sequence. It's guaranteed that the given sequence is not nice.

Output

Print the number of ways to swap two elements exactly once in order to get a nice sequence.

Examples

input
5
2 8 4 7 7


output
2


input
4
200 150 100 50


output
1


input
10
3 2 1 4 1 4 1 4 1 4


output
8


input
9
1 2 3 4 5 6 7 8 9


output
0


#include <bits/stdc++.h>
#define MOD 1000000007
#define maxn 150005
#define INF 1e18using namespace std;
typedef long long ll;

int num[maxn], n;
vector<int> v;
bool judge(int i){
if(i&1){
if((i == 1 || num[i] < num[i-1]) && (i == n || num[i] < num[i+1]))
return true;
return false;
}
if(num[i] > num[i-1] && (i == n || num[i] > num[i+1]))
return true;
return false;
}
int main(){
// freopen("in.txt", "r", stdin);
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", num+i);
int cnt = 0;
for(int i = 2; i <= n; i += 2){
if(num[i] <= num[i-1] || (i < n && num[i] <= num[i+1])){
cnt++;
v.push_back(i);
}
}
if(cnt > 3)
puts("0");
else if(cnt == 2){
int k1 = v[0], k2 = v[1];
int ans = 0;
if(k2 - k1 == 2){
for(int i = 1; i <= n; i++)
for(int j = k1-1; j <= k2+1; j++)
if(i != k1 && i != k2 && j <= n){
swap(num[i], num[j]);
if(judge(k1) && judge(k2) && judge(i))
ans++;
swap(num[i], num[j]);
}
}
else{
for(int i = k1-1; i <= k1+1; i++)
for(int j = k2-1; j <= k2+1; j ++)
if(j <= n){
swap(num[i], num[j]);
if(judge(k1) && judge(k2))
ans++;
swap(num[i], num[j]);
}
}
printf("%d\n", ans);
}
else if(cnt == 1){
int ans = 0;
int k = v[0];
for(int i = 1; i <= n; i++)
for(int j = k-1; j <= k+1; j++){
if(i != k && j <= n){
swap(num[i], num[j]);
if(judge(i) && judge(k)){
ans++;
}
swap(num[i], num[j]);
}
}
printf("%d\n", ans);
}
else if(cnt == 3){
int ans = 0;
int k1 = v[0], k2 = v[1], k3 = v[2];
if(k1 == k2 - 2){
swap(num[k1+1], num[k3]);
if(judge(k1) && judge(k2) && judge(k3))
ans++;
swap(num[k1+1], num[k3]);
}
if(k2 == k3 - 2){
swap(num[k1], num[k2+1]);
if(judge(k1) && judge(k2) && judge(k3))
ans++;
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: