codeforces 653C C. Bear and Up-Down(乱搞题)
题目链接:
2 seconds
256 megabytes
standard input
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.
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.
Print the number of ways to swap two elements exactly once in order to get a nice sequence.
5
2 8 4 7 7
2
4
200 150 100 50
1
10
3 2 1 4 1 4 1 4 1 4
8
9
1 2 3 4 5 6 7 8 9
0
In the first sample, there are two ways to get a nice sequence with one swap:
- Swap t2 = 8 with t4 = 7.
- 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(乱搞题)的更多相关文章
- 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 ...
- CF_402C Searching for Graph 乱搞题
题目链接:http://codeforces.com/problemset/problem/402/C /**算法分析: 乱搞题,不明白题目想考什么 */ #include<bits/stdc+ ...
- Codeforces 1182D Complete Mirror 树的重心乱搞 / 树的直径 / 拓扑排序
题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可 ...
- codeforces 664B B. Rebus(乱搞题)
题目链接: B. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 1513F - Swapping Problem(分类讨论+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 简单题,难度 *2500 的 D2F,就当调节一下一模炸裂了的自闭的心情,稍微写写吧. 首先我看到这题的第一反应是分类讨论+数据结构,即枚 ...
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 685C - Optimal Point(分类讨论+乱搞)
Codeforces 题面传送门 & 洛谷题面传送门 分类讨论神题. 首先看到最大值最小,一眼二分答案,于是问题转化为判定性问题,即是否 \(\exists x_0,y_0,z_0\) 满足 ...
- CodeForces 509C Sums of Digits(贪心乱搞)题解
题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小 思路:让ai刚好大于ai-1弄出来的an最小.所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生 ...
- hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题
题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...
随机推荐
- 【转】python做一个http接口测试框架
出处: https://my.oschina.net/bysu/blog/751634 https://my.oschina.net/u/3041656/blog/820023
- unity shader 编辑器扩展类 ShaderGUI
这应该unity5才出的新功能了,今天看文档时刚巧看到了,就来尝试了一下. 效果如图: shader 的编辑器扩展分为2种方法: 是通过UnityEditor下的ShaderGUI类来实现的,形式比较 ...
- SQL SERVER 2008查看sql执行的时间
set statistics profile onset statistics io onset statistics time ongo<这里写上你的语句...>goset statis ...
- rpc接口和http接口的区别和联系
1 什么是http接口 http接口是基于http协议的post和get接口. 2 什么是rpc接口 rpc接口就相当于调用本地接口一样调用远程服务的接口. 3 常用的rpc框架 thrift 自动代 ...
- 为什么Git 比 SVN 好
原文链接:http://www.worldhello.net/2012/04/12/why-git-is-better-than-svn.html Why Git is better than SVN ...
- 蜗牛—ORACLE基础之触发器学习(三)
版权声明:本文为大腰子原创文章,如若转载,请标明原地址. https://blog.csdn.net/u010071361/article/details/30037215 建立一个触发器, 当职工表 ...
- IOS - 执行时 (多态)
一 多态概述 多态指同一操作作用于不同的对象.能够有不同的解释.产生不同的执行结果.它是面向对象程序设计(OOP)的一个重要特征,动态类型能使程序直到执行时才确定对象的所属类.其详细 ...
- url信息
var protocol = window.location.protocol; // "http:" var host = window.location.host; //&qu ...
- Python decorator @property
@property广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性 下面的链接很好的阐述了@property的概念和应用 http: ...
- DevExpress实用心得:XtraGridControl动态添加右键菜
在使用GridControl的时候经常需要添加右键菜单. 一般的做法是自己创建菜单项,然后注册GridView的Mouse-Click事件,然后Show出定义好的菜单. 但是涉及到一些单击事件会收到编 ...