IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) 653C Bear and Up-Down(暴力)

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
Note

In the first sample, there are two ways to get a nice sequence with one swap:

  1. Swap t2 = 8 with t4 = 7.
  2. Swap t1 = 2 with t5 = 7.

In the second sample, there is only one way — Limak should swap t1 = 200 with t4 = 50.




题目链接:点击打开链接

给出n个数的序列, 对任意奇下标要小于后一个, 任意偶下标大于后一个, 可以交换一次, 问有多少种复合题意的方法.

暴力枚举, 记录不符合题意的位置, 暴力交换两个位置的数进行判断, 判断时只需判断交换位置的两个数以及不符合题意数的位置.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
#define X first
#define Y second
#define exp 1e-8
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 15e4 + 5;
int n, a[MAXN], ans;
bool flag[MAXN];
std::vector<int> v;
bool Judge(int x)
{
	int val = a[x];
	if(x & 1) {
		if(x < n && val >= a[x + 1]) return false;
		if(x > 1 && val >= a[x - 1]) return false;
	}
	else {
		if(x < n && val <= a[x + 1]) return false;
		if(x > 1 && val <= a[x - 1]) return false;
	}
	return true;
}
int main(int argc, char const *argv[])
{
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	for(int i = 1; i <= n; ++i)
		if(!Judge(i)) {
			v.push_back(i);
			flag[i] = true;
		}
	if(v.size() > 10) {
		printf("0\n");
		return 0;
	}
	for(auto x : v) 
		for(int y = 1; y <= n; ++y)
			if(!flag[y] || y > x) {
				swap(a[x], a[y]);
				bool valid = Judge(x) && Judge(y);
				for(auto z : v) 
					if(!Judge(z)) valid = false;
				if(valid) ans++;
				swap(a[x], a[y]);
			}
	printf("%d\n", ans);
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值