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左 ...
随机推荐
- 使用SPLUNK进行简单Threat Hunting
通过订阅网上公开的恶意ip库(威胁情报),与SIEM平台中网络流量日志进行匹配,获得安全事件告警. 比如,这里有一个malware urls数据下载的网站,每天更新一次: https://urlhau ...
- ubuntu 命令配置ip 网关 dns
如果是在虚拟机中使用Ubuntu,先设置好主机的网络,然后配置虚拟机Ubuntu的IP和网关 如果主机操作系统就是Ubuntu,请直接参照下文进行设置 内容如下: 1. 检验是否可以连通,就使用pin ...
- [转载]C++多态技术
摘自: http://www.royaloo.com/articles/articles_2003/PolymorphismInCpp.htm http://blog.sciencenet.cn/bl ...
- Netty框架入门
一.概述 Netty是由JBOSS提供的一个java开源框架. Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 二. ...
- Effective C++笔记(二):构造/析构/赋值运算
参考:http://www.cnblogs.com/ronny/p/3740926.html 条款05:了解C++默默编写并调用哪些函数 如果自定义一个空类的话,会自动生成默认构造函数.拷贝构造函数. ...
- PC端网站跳转手机端网站
<SCRIPT LANGUAGE="JavaScript"> function mobile_device_detect(url) { var thisOS=navig ...
- 图像显著性论文(一)—A Model of saliency Based Visual Attention for Rapid Scene Analysis
这篇文章是图像显著性领域最具代表性的文章,是在1998年Itti等人提出来的,到目前为止引用的次数超过了5000,是多么可怕的数字,在它的基础上发展起来的有关图像显著性论文更是数不胜数,论文的提出主要 ...
- Failed to lookup view 'error'
这个问题在npm run dev进行本地开发时,没有问题.但是在npm run build后,生产服务器上部署时出现问题. 我对本地的路径排查,发现写的没有问题 所以我去了生产的文件夹看路径 我去了s ...
- .NET基本权限系统框架源代码
DEMO下载地址: 百度网盘:http://pan.baidu.com/s/147ilj http://download.csdn.net/detail/shecixiong/5372895 一.开发 ...
- 转:在 Ubuntu 上使用 Nginx 部署 Flask 应用
转:http://Python.jobbole.com/84286/ 原文出处: Vladik 译文出处:开源中国 我职业生涯的大部分都在使用微软的架构,最近我决定走出技术的舒适区,步入开源 ...