题目链接:

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. CSDN--字体颜色--markdown

    在写blog时,想高亮某些字,但是发现markdown更改字体颜色不像word里那么方便,于是查了一下,要用一下代码进行更改字体颜色,还可以更改字体大小,还有字体格式 <font 更改语法> ...

  2. 大数据hadoop之zookeeper

    一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...

  3. 【BZOJ3510】首都 LCT维护子树信息+启发式合并

    [BZOJ3510]首都 Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打 ...

  4. 爬虫入门【5】PyQuery简介

    PyQuery 目前最新的版本是1.3,基于最新版本进行介绍. 主要根据PyQuery的官方文档进行了更新. from pyquery import PyQuery as pq from lxml i ...

  5. EF之POCO应用系列2——复杂类型

    在.NET开发中,EF4以前的版本以及LINQ TO SQL都不支持complex数据类型,EF4终于支持complex类型的数据了,这意味着微软的EF框架朝领域驱动方面又迈了一大步. 复杂的数据类型 ...

  6. Python高级入门01-property

    JAVA中存在对变量 私有化,公开,保护... 私有化时候,需要提供一个公开的get 和 set方法对外公开,让别人进行调用 python中同样存在    私有化变量定义是__是这个双下划线,eg:_ ...

  7. ajax (异步js+xml)

    AJAX 基础 AJAX = 异步js+xml 通过与后台服务器进行少量数据交换,实现前台网页局部更新 XMLHttpRequest对象 是 AJAX 的基础 var xmlhttp; if (win ...

  8. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

  9. LeetCode:救生艇【881】

    LeetCode:救生艇[881] 题目描述 第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. ...

  10. CentOS7 添加用户到 sudoers

    1.在装完系统后,CentOS默认普通用户是无法使用sudo命令的,这时我们就需要将登陆的用户加入/etc/sudoers 文件中 假设用户名是yhd 3.切换到超级用户:$ su - 当然输入的 p ...