题目链接:

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.

题意:问有多少种交换方法使序列满足nice的条件;

思路:把不合理的位置找出来,然后暴力交换看有多少种方式,乱搞就能搞出来,然而我乱搞了一夜晚一直wa,后来好好想了不同的情况才过;

AC代码:

//代码写的跟翔一样,自己看了就想吐;

#include <bits/stdc++.h>
using namespace std;
const int N=15e4+;
int a[N],flag[N],pos[N],cnt,num,n;
int sap(int x,int y)
{
int t=a[y];
a[y]=a[x];
a[x]=t;
}
int check(int v)
{
/* if(x==3)
{
for(int j=0;j<x;j++)
{
cout<<pos[j]<<"&"<<endl;
}
}
*/
int u=v;
// cout<<v<<"@"<<pos[v-1]<<endl;
for(int j=;j<u;j++)
{
//cout<<a[1]<<"*"<<a[4]<<endl;
//cout<<pos[j]<<"#"<<j<<endl;
if(pos[j]==)
{
if(a[pos[j]+]<=a[pos[j]])return ;
continue;
}
if(pos[j]==n)
{
if(n%)
{
if(a[n-]<=a[n])return ;
continue;
}
else
{
if(a[n-]>=a[n])return ;
continue;
}
}
if(pos[j]%)
{
if(a[pos[j]-]<=a[pos[j]]||a[pos[j]+]<=a[pos[j]])return ;
}
else
{
if(a[pos[j]-]>=a[pos[j]]||a[pos[j]+]>=a[pos[j]])return ;
}
}
return ;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
a[]=;
a[n+]=;
cnt=,num=;
for(int i=;i<n;i++)
{
if(i%)
{
if(a[i]>=a[i+]){
if(!flag[i]){pos[num++]=i,flag[i]=;}
if(!flag[i+])pos[num++]=i+,flag[i+]=;
}
}
else
{
if(a[i]<=a[i+])
{
if(!flag[i]) pos[num++]=i,flag[i]=;
if(!flag[i+])pos[num++]=i+,flag[i+]=;
}
}
}
if(num>=){cout<<""<<endl;return ;}
int ans=;
for(int i=;i<num;i++)
{
for(int j=i+;j<num;j++)
{
sap(pos[i],pos[j]);
ans+=check(num);
sap(pos[j],pos[i]);
}
}
for(int i=;i<num;i++)
{
for(int j=;j<=n;j++)
{
if(!flag[j])
{
sap(pos[i],j);
pos[num]=j;
ans+=check(num+);
sap(j,pos[i]);
}
}
}
cout<<ans<<"\n";
return ;
}

codeforces 653C C. Bear and Up-Down(乱搞题)的更多相关文章

  1. codeforces 669D D. Little Artem and Dance(乱搞题)

    题目链接: D. Little Artem and Dance time limit per test 2 seconds memory limit per test 256 megabytes in ...

  2. CF_402C Searching for Graph 乱搞题

    题目链接:http://codeforces.com/problemset/problem/402/C /**算法分析: 乱搞题,不明白题目想考什么 */ #include<bits/stdc+ ...

  3. Codeforces 1182D Complete Mirror 树的重心乱搞 / 树的直径 / 拓扑排序

    题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可 ...

  4. codeforces 664B B. Rebus(乱搞题)

    题目链接: B. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codeforces 1513F - Swapping Problem(分类讨论+乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 简单题,难度 *2500 的 D2F,就当调节一下一模炸裂了的自闭的心情,稍微写写吧. 首先我看到这题的第一反应是分类讨论+数据结构,即枚 ...

  6. Codeforces 34C-Page Numbers(set+vector+暴力乱搞)

    C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. Codeforces 685C - Optimal Point(分类讨论+乱搞)

    Codeforces 题面传送门 & 洛谷题面传送门 分类讨论神题. 首先看到最大值最小,一眼二分答案,于是问题转化为判定性问题,即是否 \(\exists x_0,y_0,z_0\) 满足 ...

  8. CodeForces 509C Sums of Digits(贪心乱搞)题解

    题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小 思路:让ai刚好大于ai-1弄出来的an最小.所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生 ...

  9. hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

    题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...

随机推荐

  1. Windows找出占用端口的进程

    第一步:找出监听指定端口的进程号: C:\> netstat -ao | findstr 443  TCP    0.0.0.0:443            Sean-NotePC:0     ...

  2. Linux 随手记(文件操作)

    新建文件夹 mkdir 文件夹名 新建文件 touch 文件名 重命名 mv 文件名 新文件名 将/a目录移动到/b下,并重命名为c mv /a /b/c 复制文件 cp [选项] 源文件或目录 目标 ...

  3. 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

    [BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other.     But r ...

  4. vue组件父子组件之间传递数据

    举个栗子: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  5. iOS Base64加密

    1.Base64编码说明 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式. 如果剩下的字符不足3个字节,则用0 ...

  6. 【python】-- 面向对象引子、概念

    面向过程编程 1.编程范式 编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马 ...

  7. linux c编程:信号(五) sigsuspend

    更改进程的信号屏蔽字可以阻塞所选择的信号,或解除对它们的阻塞.使用这种技术可以保护不希望由信号中断的代码临界区.如果希望对一个信号解除阻塞,然后pause等待以前被阻塞的信号发生,则又将如何呢?假定信 ...

  8. Data Structure Array: Find the minimum distance between two numbers

    http://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/ #include <iostream> ...

  9. 0523 CSS知识点

    高级选择器分为:后代选择器.子代选择器.并集选择器.交集选择器 后代选择器 使用空格表示后代选择器.顾名思义,父元素的后代(包括儿子,孙子,重孙子) .father .item .a p{color: ...

  10. P2455 [SDOI2006]线性方程组

    P2455 [SDOI2006]线性方程组 真\(\cdot\)高斯消元模板题 由于各种hack数据被造出来~码量突增~,其实也就多了二三十行 将每行系数消到最多有一个非0数 特殊情况: 在过程同时 ...