题目链接:

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【暴力】的更多相关文章

  1. 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 ...

  2. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  3. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  4. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  5. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  6. [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)

    [Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...

  7. 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 ...

  8. codeforces 680E Bear and Square Grid 巧妙暴力

    这个题是个想法题 先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除 复杂度O(n^3) #include <cstdio> #include <iostre ...

  9. Codeforces Gym 100513M M. Variable Shadowing 暴力

    M. Variable Shadowing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/ ...

随机推荐

  1. Hello Shell

    shell是Linux平台的瑞士军刀,能够自动化完成很多工作.要了解UNIX 系统中可用的 Shell,可以使用 cat /etc/shells 命令.使用 chsh 命令 更改为所列出的任何 She ...

  2. grep的几个参数

    -a 在二进制问就爱你中,以文本方式进行搜索 -c 计算找到搜索字符串的次数 -i 忽略大小写 -n 输出行号 -v 反向选择,即没有显示搜索字符串内容的那一行 grep -n '\.$'  file ...

  3. Java练习题00

    问题: 将一个数字与数组中的元素比较, 如果该数字存在数组中,给出该数字在数组中的位置: 如果该数字不在数组中,给出提示.   代码: public class Page84{     public ...

  4. react基础语法(一)元素渲染和基础语法规则

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. echarts简单用法快速上手

    1.html结构 简单说就是一个标签一个图表:2.初始化:var myEcharts = echarts.init(document.getElementById("xxx")): ...

  6. vs2010 在函数级别设置优化

    平时开发的时候,为了方便调试,visual studio 的Configuration 设置成Release. 同时为了事后调试,Optimization总是设置成Disabled.这样做是方便查看变 ...

  7. docker使用阿里云镜像加速器(属于自己的专属加速器)

    https://cr.console.aliyun.com/cn-shanghai/mirrors

  8. Java实现Web页面前数字字母验证码实现

    最近公司做项目开发中用到了验证码实现功能,将实现代码分享出来, 前段页面实现代码: 为了表达清晰,样式部分代码去掉了,大家根据自己的需求,自己添加样式. 页面JS代码:触发变动验证码改变的JS 后台 ...

  9. python基础一 day9 函数升阶(1)

    函数 可读性强 复用性强def 函数名(): 函数体 return 返回值所有的函数 只定义不调用就一定不执行 先定义后调用 函数名() #不接收返回值返回值 = 函数名() #接收返回值 返回值 没 ...

  10. 深度学总结:skip-gram pytorch实现

    文章目录 skip-gram pytorch 朴素实现网络结构训练过程:使用nn.NLLLoss()batch的准备,为unsupervised,准备数据获取(center,contex)的pair: ...