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乱搞题
题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...
随机推荐
- xmlUtil 解析 创建
http://yangzi09150915.blog.163.com/blog/static/32953487201072911410398/ package com.aibi.cmdc.webSer ...
- keil中使用Astyle格式化你的代码的方法2篇合
关于Astyle Astyle 的全称是Artistic Style的简称,是一个开源的源代码格式化工具,可以对C,C++,C#以及Java等编程语言的源代码进行缩进.格式化.美化.Home Page ...
- 简洁的一键SSH脚本
这里发一个自己图省事搞的一个批量打通SSH的脚本,可能对于好多朋友也是实用的,是expect+python的一个组合实现,原理非常easy, 使用起来也不复杂,在此还是简单贴出来说说. noscp.e ...
- 信号量semaphore解析
1 基础概念 信号量在创建时须要设置一个初始值,表示同一时候能够有几个任务能够訪问该信号量保护的共享资源.初始值为1就变成相互排斥锁(Mutex),即同一时候仅仅能有一个任务能够訪问信号量保护的共享资 ...
- Tomcat startup.bat启动隐藏弹出的信息窗口
to make tomcat to use javaw.exe instead of java.exe using some startup parameter or environment vari ...
- eclipse导入web工程变成Java工程,解决方案
经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目. 解决步骤: 1.进入项目目录,可看到.project文件,文本编辑器打开. 2.找到<nature ...
- iOS 蓝牙功能 bluetooth
现将创建蓝牙工程的要点总结一下,由于工程主要涉及中心模式,所以只总结中心模式的用法 1,引入CoreBluetooth.framework 2,实现蓝牙协议,如: .h文件如下 @protocol C ...
- Xcode8:"subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0" 的警告
运行xcode8遇到这个警告: subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_lev ...
- python 深复制与浅复制------copy模块
模块解读: 浅复制: x = copy.copy(y)深复制: x = copy.deepcopy(y)(注:模块特有的异常,copy.Error) 深copy与浅copy的差别主要体现在当有混合对象 ...
- springboot工程自动生成工具
1 springboot工程自动生成网址 http://start.spring.io/ 2 工具 Spring Boot CLI