A10293 | Bear and Up-Down
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The life goes up and down, just like nice sequences. Sequence $t_{1},t_{2},...,t_{n}$ is called nice if the following two conditions are satisfied:
- $t_{i}<t_{i+1}$ for each odd $i<n$ ;
- $t_{i}>t_{i+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 $t_{1},t_{2},...,t_{n}$ . 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 $t_{i}$ and $t_{j}$ 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.
- $t_{i}<t_{i+1}$ for each odd $i<n$ ;
- $t_{i}>t_{i+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 $t_{1},t_{2},...,t_{n}$ . 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 $t_{i}$ and $t_{j}$ 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<=150000$ ) — the length of the sequence.
The second line contains $n$ integers $t_{1},t_{2},...,t_{n}$ ( $1<=t_{i}<=150000$ ) — the initial sequence. It's guaranteed that the given sequence is not nice.
The second line contains $n$ integers $t_{1},t_{2},...,t_{n}$ ( $1<=t_{i}<=150000$ ) — 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.
输入输出样例
输入 #1
5 2 8 4 7 7
输出 #1
2
输入 #2
4 200 150 100 50
输出 #2
1
输入 #3
10 3 2 1 4 1 4 1 4 1 4
输出 #3
8
输入 #4
9 1 2 3 4 5 6 7 8 9
输出 #4
0
In the first sample, there are two ways to get a nice sequence with one swap:
1. Swap $t_{2}=8$ with $t_{4}=7$ .
2. Swap $t_{1}=2$ with $t_{5}=7$ .
In the second sample, there is only one way — Limak should swap $t_{1}=200$ with $t_{4}=50$ .
1. Swap $t_{2}=8$ with $t_{4}=7$ .
2. Swap $t_{1}=2$ with $t_{5}=7$ .
In the second sample, there is only one way — Limak should swap $t_{1}=200$ with $t_{4}=50$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted