【IndiaHacks 2016 - Online Edition (Div 1 + Div 2) ErrichtoC】【脑洞 好题 讨论?NO!暴力】Bear and Up-Down 多少种一次交

本文主要是介绍【IndiaHacks 2016 - Online Edition (Div 1 + Div 2) ErrichtoC】【脑洞 好题 讨论?NO!暴力】Bear and Up-Down 多少种一次交,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.


#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 ...*/


这篇关于【IndiaHacks 2016 - Online Edition (Div 1 + Div 2) ErrichtoC】【脑洞 好题 讨论?NO!暴力】Bear and Up-Down 多少种一次交的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610421

相关文章

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

电脑多久清理一次灰尘合? 合理清理电脑上灰尘的科普文

《电脑多久清理一次灰尘合?合理清理电脑上灰尘的科普文》聊起电脑清理灰尘这个话题,我可有不少话要说,你知道吗,电脑就像个勤劳的工人,每天不停地为我们服务,但时间一长,它也会“出汗”——也就是积累灰尘,... 灰尘的堆积几乎是所有电脑用户面临的问题。无论你的房间有多干净,或者你的电脑是否安装了灰尘过滤器,灰尘都

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

(function() {})();只执行一次

测试例子: var xx = (function() {     (function() { alert(9) })(); alert(10)     return "yyyy";  })(); 调用: alert(xx); 在调用的时候,你会发现只弹出"yyyy"信息,并不见弹出"10"的信息!这也就是说,这个匿名函数只在立即调用的时候执行一次,这时它已经赋予了给xx变量,也就是只是

flume系列之:记录一次flume agent进程被异常oom kill -9的原因定位

flume系列之:记录一次flume agent进程被异常oom kill -9的原因定位 一、背景二、定位问题三、解决方法 一、背景 flume系列之:定位flume没有关闭某个时间点生成的tmp文件的原因,并制定解决方案在博主上面这篇文章的基础上,在机器内存、cpu资源、flume agent资源都足够的情况下,flume agent又出现了tmp文件无法关闭的情况 二、