IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C. Bear and Up-Down 暴力
C. Bear and Up-Down
题目连接:
http://www.codeforces.com/contest/653/problem/C
Description
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.
Sample Input
5
2 8 4 7 7
Sample Output
2
Hint
题意
一个序列定义为nice的话,就是这个序列满足阶梯状。
就是如果i是偶数,那么ai>ai-1,ai>ai+1
如果i是奇数,那么ai<ai+1,ai<ai-1
现在允许你交换两个数的位置,问你一共有多少种交换方式,使得这个序列变成nice
保证一开始的序列不是nice的。
题解:
我们定义不nice的数就是不满足条件的位置。
我们可以大胆猜测一发,不nice的数一定不会有很多,因为一次交换最多影响6个数,所以我们把这些不nice的数直接扔到一个数组里面。
然后暴力去和整个序列去交换就好了。
然后check也是只用check那些不nice的位置和你交换的那个位置的数。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int n;
int a[maxn];
vector<int>tmp;
long long ans = 0;
set<pair<int,int> >S;
bool check()
{
for(int i=0;i<tmp.size();i++)
{
for(int j=-1;j<=1;j++)
{
if(tmp[i]+j==0)continue;
if(tmp[i]+j==n+1)continue;
int pos = (tmp[i]+j);
if(pos%2==1)
{
if(a[pos]>=a[pos+1])return false;
if(a[pos]>=a[pos-1])return false;
}
else
{
if(a[pos]<=a[pos+1])return false;
if(a[pos]<=a[pos-1])return false;
}
}
}
return true;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=1e9;
if(n%2==1)a[n+1]=1e9;
else a[n+1]=-1;
for(int i=1;i<=n;i++)
{
if( i & 1 ){
bool ok = true;
if( i + 1 <= n && a[i] >= a[i+1] ) ok = false;
if( i - 1 >= 1 && a[i] >= a[i-1] ) ok = false;
if( ok == false ) tmp.push_back( i );
}else{
bool ok = true;
if( i + 1 <= n && a[i] <= a[i+1] ) ok = false;
if( i - 1 >= 1 && a[i] <= a[i-1] ) ok = false;
if( ok == false ) tmp.push_back( i );
}
}
if(tmp.size()>30)
return puts("0"),0;
for(int i=0;i<tmp.size();i++)
{
for(int j=1;j<=n;j++)
{
if(tmp[i]==j)continue;
swap(a[tmp[i]],a[j]);
bool ok = check();
if(j%2==1)
{
if(a[j]>=a[j+1]||a[j]>=a[j-1])ok=false;
}
if(j%2==0)
{
if(a[j]<=a[j+1]||a[j]<=a[j-1])ok=false;
}
if(ok)
{
pair < int , int > SS = make_pair( min( tmp[i] , j ) , max( tmp[i] , j ) );
if(!S.count(SS)){
S.insert( SS );
ans++;
}
}
swap(a[tmp[i]],a[j]);
}
}
cout<<ans<<endl;
}
IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C. Bear and Up-Down 暴力的更多相关文章
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing
B. Bear and Compressing 题目链接 Problem - B - Codeforces Limak is a little polar bear. Polar bears h ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树
E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) D. Delivery Bears 二分+网络流
D. Delivery Bears 题目连接: http://www.codeforces.com/contest/653/problem/D Description Niwel is a littl ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing 暴力
B. Bear and Compressing 题目连接: http://www.codeforces.com/contest/653/problem/B Description Limak is a ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题
A. Bear and Three Balls 题目连接: http://www.codeforces.com/contest/653/problem/A Description Limak is a ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)——A - Bear and Three Balls(unique函数的使用)
A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))
传送门 A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input ...
- IndiaHacks 2016 - Online Edition (CF) . D
这题思路很简单,二分m,求最大流是否大于等于x. 但是比赛过程中大部分的代码都被hack了... 精度问题,和流量可能超int 关于精度问题,这题真是提醒的到位,如果是先用二分将精度控制在10^-8左 ...
随机推荐
- gunicorn之日志详细配置
gunicorn的日志配置 gunicorn的日志配置相关的常用参数有4个,分别是accesslog,access_log_format,errorlog,loglevel. accesslog:用户 ...
- Fedora8 U盘安装
(一)分区 在XP下"我的电脑“管理功能,对硬盘分区,目的是从逻辑分区中拿出20G空间,分成3个盘(必须为逻辑盘): (1)512MB 用作Linux swap分区: (2)200MB ...
- 离线下载pip包进行安装【转】
Host-A 不能上网,但是需要在上面安装Python-package 通过另外一台能上网的Host-B主机 1. 下载需要离线安装的Packages 在Host-B上执行如下命令: 安装单个Pack ...
- 【并行计算】用MPI进行分布式内存编程(二)
通过上一篇中,知道了基本的MPI编写并行程序,最后的例子中,让使用0号进程做全局的求和的所有工作,而其他的进程却都不工作,这种方式也许是某种特定情况下的方案,但明显不是最好的方案.举个例子,如果我们让 ...
- FPM定制RPM包
安装FPM FPM是ruby写的打包工具,ruby版本要大于1.8.5 #安装ruby环境和gem包管理器 [root@test88 ~]# yum install -y ruby rubygems ...
- python-unittest学习
在说unittest之前,先说几个概念: TestCase 也就是测试用例 TestSuite 多个测试用例集合在一起,就是TestSuite TestLoader是用来加载TestCase到Test ...
- #include<stdarg.h> 可变参数使用
今天上计算方法这课时觉得无聊至极,于是拿出C++编程之道来看了看..无意之中看到了#include<stdarg.h> va_list,va_start,va_end等东西,不知是怎么用的 ...
- UFLDL 教程学习笔记(六)主成分分析
教程:http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks/ 以及这篇博文,写的很清楚:http://blog. ...
- 强大到无与伦比的Graphviz
图1 hello world 尝试画复杂一些的图: 一直苦苦寻找用于图论的画图软件,偶然在Matrix67的这篇博文里找到. Graphviz使用dot语言,这门不仅语言非常简单易学,而且功能却非常强 ...
- 应用nslookup命令查看A记录、MX记录、CNAME记录和NS记录
https://blog.csdn.net/qq_38058202/article/details/80468688