The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied:
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.
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.
Print the number of ways to swap two elements exactly once in order to get a nice sequence.
5 2 8 4 7 7
2
4 200 150 100 50
1
10 3 2 1 4 1 4 1 4 1 4
8
9 1 2 3 4 5 6 7 8 9
0
In the first sample, there are two ways to get a nice sequence with one swap:
In the second sample, there is only one way — Limak should swap t1 = 200 with t4 = 50.
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include<bitset> #include<algorithm> #include<time.h> using namespace std; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } #define MS(x,y) memset(x,y,sizeof(x)) #define MC(x,y) memcpy(x,y,sizeof(x)) #define MP(x,y) make_pair(x,y) #define ls o<<1 #define rs o<<1|1 typedef long long LL; typedef unsigned long long UL; typedef unsigned int UI; template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; } template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; } const int N = 15e4+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f; int n, m; int a[N]; int b[N]; bool check(int p) { if (p & 1) { if (p > 1 && a[p - 1] <= a[p])return 0; if (p < n && a[p + 1] <= a[p])return 0; } else { if (p > 1 && a[p - 1] >= a[p])return 0; if (p < n && a[p + 1] >= a[p])return 0; } return 1; } set< pair<int, int> >sot; bool tryit(int x, int y) { if (x > y)swap(x, y); if (sot.count(MP(x, y)))return 0; bool flag = 1; swap(a[x], a[y]); if (!check(x) || !check(y))flag = 0; if(flag)for (int i = 1; i <= m; ++i) { if (!check(b[i]))flag = 0; if (!check(b[i] + 1))flag = 0; } swap(a[x], a[y]); sot.insert(MP(x, y)); return flag; } int solve() { if (m > 4)return 0; sot.clear(); int ret = 0; for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (tryit(b[i], j))++ret; if (tryit(b[i] + 1, j))++ret; } } return ret; } int main() { while (~scanf("%d", &n)) { for (int i = 1; i <= n; ++i)scanf("%d", &a[i]); m = 0; for (int i = 1; i < n; ++i) { if (i & 1) { if (a[i] >= a[i + 1])b[++m] = i; } else { if (a[i] <= a[i + 1])b[++m] = i; } } printf("%d\n", solve()); } return 0; } /* 【trick&&吐槽】 这题深深羞辱了我,因为我是个傻叉 【题意】 给你一个数列a[],定义它是nice数列,需要满足 ai < ai + 1 for each odd i < n; ai > ai + 1 for each even i < n. 然而,现在这个数列并不是nice数列, 不过我们可以做一次交换,使得a[x]与a[y]做交换。 问你有多少种交换方案,可以使得这个数列变为nice数列 【类型】 讨论 暴力 【分析】 我们只能交换一次,于是交换所能产生的影响,就非常局部。 如果对于一个奇数位置i,有t[i] >= t[i + 1],那么我们至少要改变这t[i]与t[i+1]中的一个 如果对于一个偶数位置i,有t[i] <= t[i + 1],那么我们也至少要改变t[i]与t[i+1]中的一个 定义这样的位置都为"错位置",用b[]存储 定义"错位置"个数为num 显然只有在错位置<=4的时候,我们才有可能交换使得问题 于是我们只要枚举m*2个位置做交换,同时检测交换位置和原始错位置的正确性即可。 【时间复杂度&&优化】 O(4*n*4*3) about ... */