题目链接:

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. 记pytorch版faster rcnn配置运行中的一些坑

    记pytorch版faster rcnn配置运行中的一些坑 项目地址 https://github.com/jwyang/faster-rcnn.pytorch 一般安装配置参考README.md文件 ...

  2. Tkinter 控件详细介绍

    Tkinter 控件详细介绍 1.Button 按钮.类似标签,但提供额外的功能,例如鼠标掠过.按下.释放以及键盘操作/事件 2.Canvas 画布.提供绘图功能(直线.椭圆.多边形.矩形) ;可以包 ...

  3. 微信小程序的文件结构 —— 微信小程序教程系列(1)

    所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/548 ...

  4. window下安装php7的memcache扩展

    安装memcache:http://www.runoob.com/memcached/memcached-connection.html1.4.4 c:\memcached\memcached.exe ...

  5. smarty模板 变量 运算符 表达式 流程控制 函数

    ① 从配置文件中读取配置: 1,在模板页面加载配置文件 html页面 不是php页面<{config_load file='fo.conf'}> 2,在需要用到配置的地方加<{#si ...

  6. poj2485

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27912   Accepted: 12734 Descri ...

  7. EasyNVR无插件直播服务器软件接口调用返回“Unauthorized”最简单的处理方式

    背景需求 对于EasyNVR的受众群体十分的广泛,不仅仅有将EasyNVR作为视频直播平台直接使用的,更多的是使用EasyNVR的对应功能集成到自身系统.对于前者,只需要将软件的使用功能搞清楚即可,对 ...

  8. web 文件下载

    response.reset(); response.setContentType("octets/stream"); response.addHeader("Conte ...

  9. 【python】-- 继承式多线程、守护线程

    继承式多线程 1.定义 继承式多线程是自己自定义类,去继承theading.Tread这个类,通过类实例.start()启动,去执行run方法中的代码. import threading import ...

  10. python3保存一个网页

    import requests res = requests.get("http://www.baidu.com") savefile = open("baidu.htm ...