Codeforces 653C Bear and Up-Down【暴力】
题目链接:
http://codeforces.com/problemset/problem/653/C
题意:
给定序列,偶数位的数字比两边都大,则成为nice,只可以交换两个数字,问有多少种交换方式使得最后的序列为nice。
分析:
比赛的时候找规律,找呀找了好久都没看出来。。。。由于只可以交换一次,交换两个数字,所以最多改变的6个数字的关系。那么可以交换成nice的话,原来不满足的肯定很少,直接把这些数字提出来,然后暴力的和序列每个元素交换一下,看看其余不满足的是否变成满足就好啦~~~
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1500005;
int t[maxn];
vector<int>v;
typedef pair<int, int>pii;
map<pii, int>vis;
int n;
bool judge(int b)
{
if(b & 1) {
if(b < n - 1) return t[b - 1] < t[b] && t[b + 1] < t[b];
else return t[b - 1] < t[b];
}
else if(b > 0 && b < n - 1) return t[b - 1] > t[b] && t[b + 1] > t[b];
else if(b == 0) return t[b + 1] > t[b];
else return t[b - 1] > t[b];
}
int main (void)
{
cin>>n;
for(int i = 0; i < n; i++){
cin>>t[i];
}
for(int i =0; i < n; i += 2){
if(i == 0 && t[i] >= t[i + 1]){
v.push_back(i);v.push_back(i + 1);
}
if(i == n - 1 && t[i] >= t[ i - 1]){
v.push_back(i);v.push_back(i - 1);
}
else if(i > 0 && i < n - 1){
if( t[i] >= t[i + 1]){
v.push_back(i);v.push_back(i + 1);
}
else if(t[i] >= t[i - 1]){
v.push_back(i);v.push_back(i - 1);
}
else if( t[i] >= t[i + 1] && t[i] >= t[i - 1]){
v.push_back(i);v.push_back(i - 1);v.push_back(i + 1);
}
}
}
int cnt = 0;
if(v.size() > 10){
cout<<0<<endl;
return 0;
}
for(int i = 0; i < v.size(); i++){
for(int j = 0; j < n; j++){
if(t[j] != t[v[i]]){
swap(t[j], t[v[i]]);
bool ok = true;
for(int k = 0; k < v.size(); k++){
if(!judge(v[k])) ok = false;
}
if(!judge(j)) ok = false;
if(ok){
// cout<<t[i]<<' '<<t[j]<<endl;
pii p = pii(min(v[i], j),max(v[i],j));
if(!vis[p]){vis[p] = 1; cnt++;}
}
swap(t[v[i]], t[j]);
}
}
}
cout<<cnt<<endl;
return 0;
}
Codeforces 653C Bear and Up-Down【暴力】的更多相关文章
- Codeforces 791A Bear and Big Brother(暴力枚举,模拟)
A. Bear and Big Brother time limit per test:1 second memory limit per test:256 megabytes input:stand ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Codeforces 385B Bear and Strings
题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)
[Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...
- codeforces 653C C. Bear and Up-Down(乱搞题)
题目链接: C. Bear and Up-Down time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- codeforces 680E Bear and Square Grid 巧妙暴力
这个题是个想法题 先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除 复杂度O(n^3) #include <cstdio> #include <iostre ...
- Codeforces Gym 100513M M. Variable Shadowing 暴力
M. Variable Shadowing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/ ...
随机推荐
- cacti支持中文办法
1.yum groupinstall "chinese support" 2. 登陆Cacti,在主页的左边点击setting,选择paths页(console>>se ...
- java访问数据库步骤详解
eg1: public static void main(String[] args) throws ClassNotFoundException, SQLException { //第一步:加载JD ...
- c语言 预处理的使用 宏展开下的#,##
1. #include 包含头文件 2.define 宏定义(可以理解为替换,不进行语法检查) 写法 #define 宏名 宏体 加括号 #define ABC (5+3) #define AB ...
- Boxes And Balls(三叉哈夫曼编码)
题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...
- && (and)、||(or) 条件语句
当前面条件满足时,就执行后面的代码 //条件为真时,就执行其中的语句 if($a>0){ $b='This is test'; } //上面的写法太麻烦,可以这样简写 $a>0 & ...
- Python基础3 函数 变量 递归 -DAY3
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...
- 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
*题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数) public class 第三十九题按条件计算 ...
- sublime中项目无法添加文件夹
问题记录 mac中,使用vue init webpack project 后,在sublime中打开,但是添加新文件夹和删除,总提示没有权限, 然后用git提交吧 也不行,每次都要sudo 出现的提示 ...
- python数组中数据位置交换 -- IndexError: list assignment index out of range
代码: t = [-10,-3,-100,-1000,-239,1] # 交换 -10和1的位置 t[5], t[t[5]-1] = t[t[5]-1], t[5] 报错: IndexError: l ...
- Yii2 advance swiftmailer 不能发送邮件
我用的是Yii2高级模板,在配置好邮箱后,并编写测试,测试结果表明是发送成功的,但我的邮箱就是接受不了邮件. 经过排查发现,是由 common/config/main-local.php 文件的 'u ...