Codeforces 653C Bear and Up-Down【暴力+思维】题目还不错~
来源:互联网 发布:stl源码剖析第二章 编辑:程序博客网 时间:2024/06/15 04:26
The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is callednice if the following two conditions are satisfied:
- ti < ti + 1 for each oddi < n;
- ti > ti + 1 for each eveni < 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 sequenceis not nice now and Limak wants to fix it by a single swap. He is going to choose two indicesi < 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.
52 8 4 7 7
2
4200 150 100 50
1
103 2 1 4 1 4 1 4 1 4
8
91 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 witht4 = 7.
- Swap t1 = 2 witht5 = 7.
In the second sample, there is only one way — Limak should swap t1 = 200 with t4 = 50.
题目大意:
需要保证最终序列是这样的:
- ti < ti + 1 for each odd i < n;
- ti > ti + 1 for each even i < n.
思路:
1、明显,如果有错误的位子超过了5个,那么肯定就没有解了。
2、如果有错误的位子小于等于4个,那么对应我们将有错误的点的周围几个点都标记上,对于这些点,我们可以选择其与其他N-1个点进行交换,并且判断是否序列满足最终的要求。
时间复杂度O(n*错误点的个数以及周围点的个数和);
所以我们判断序列是否合法的时候,只要对特殊的点:
①交换点的周围几个点。
②有错误的点和周围几个点。
进行判断是否合法即可。
时间复杂度O(需要判断的点的个数*n*错误点的个数以及周围点的个数和);
因为这些点并不多,所以我们可以暴力来搞。
只要维护好需要判断的位子点,以及需要交换的位子点即可。
3、需要谨慎度相对较高..........大家谨慎点写代码....
Ac代码:
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;int a[150500];int vis[150500];int tot;int num[150500];int n;int judge(int x,int y){ num[tot++]=x-5; num[tot++]=x-4; num[tot++]=x-3; num[tot++]=x-2; num[tot++]=x-1; num[tot++]=x; num[tot++]=x+1; num[tot++]=x+2; num[tot++]=x+3; num[tot++]=x+4; num[tot++]=x+5; num[tot++]=y-5; num[tot++]=y-4; num[tot++]=y-3; num[tot++]=y-2; num[tot++]=y-1; num[tot++]=y; num[tot++]=y+1; num[tot++]=y+2; num[tot++]=y+3; num[tot++]=y+4; num[tot++]=y+5; for(int j=0; j<tot; j++) { int v=num[j]; if(v<0||v>=n)continue; if(v%2==0) { if(v-1>=0) { if(a[v]<a[v-1])continue; else return 0; } if(v+1<n) { if(a[v]<a[v+1])continue; else return 0; } } else { if(v-1>=0) { if(a[v]>a[v-1])continue; else return 0; } if(v+1<n) { if(a[v]>a[v+1])continue; else return 0; } } } return 1;}int main(){ while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); int cnt=0; for(int i=0; i<n; i++)scanf("%d",&a[i]); for(int i=1; i<n; i++) { if(i%2==1) { if(a[i]>a[i-1])continue; else { cnt++; for(int j=-2; j<=2; j++) { if(i+j<n&&i+j>=0)vis[i+j]=1; } } } else { if(a[i]<a[i-1])continue; else { cnt++; for(int j=-5; j<=5; j++) { if(i+j<n&&i+j>=0)vis[i+j]=1; } } } } if(cnt>=5) { printf("0\n"); } else { __int64 contz=0; __int64 output=0; for(int i=0;i<n;i++) { if(vis[i]==1)num[tot++]=i; } for(int i=0; i<n; i++) { if(vis[i]==1) { for(int j=0; j<n; j++) { if(i==j)continue; swap(a[i],a[j]); if(judge(i,j)==1) { output++; if(vis[j]==1)contz++; } tot-=22; swap(a[i],a[j]); } } } output-=contz/2; printf("%I64d\n",output); } }}
- Codeforces 653C Bear and Up-Down【暴力+思维】题目还不错~
- Codeforces 653C Bear and Up-Down【暴力】
- Codeforces 653C - Bear and Up-Down 暴力
- Codeforces(653C)-C. Bear and Up-Down
- Codeforces 653C Bear and Up-Down 【模拟】
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) 653C Bear and Up-Down(暴力)
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)-C - Bear and Up-Down-暴力枚举
- Codeforces 639C Bear and Polynomials 【思维】
- Codeforces 574C Bear and Poker【思维】
- Codeforces 336C Vasily the Bear and Sequence 暴力
- Codeforces 336C Vasily the Bear and Sequence (暴力)
- Codeforces 673C Bear and Colors【暴力枚举】
- Codeforces 679C Bear and Square Grid 暴力(滑动窗口)
- Codeforces Round #405 C. Bear and Different Names (思维)
- Codeforces Round #405 C.Bear and Different Names【思维】
- Codeforces 658C Bear and Forgotten Tree 3【思维】
- CF# IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C - Bear and Up-Down
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C. Bear and Up-Down
- Win7怎么安装?推荐3种Windows7安装方法
- Android 怎么把imageview 转为Bitmap
- 写给Android开发者的混淆使用手册
- 剑指Offer系列-面试题51:数组中重复的数字
- 统计学总结之Bias(偏差),Error(误差),和Variance(方差)的区别
- Codeforces 653C Bear and Up-Down【暴力+思维】题目还不错~
- poj 1837 Balance 二维背包
- 关系代数中的除法运算(转载)
- 详解EditText输入监听TextWatcher
- 打印日期
- render_template() takes exactly 1 argument
- 垃圾归垃圾,渣滓归渣滓。尘土归尘土
- 欢迎使用CSDN-markdown编辑器
- 画图游戏(博弈论)