Banks

题目链接:

http://acm.hust.edu.cn/vjudge/contest/130303#problem/A

Description


http://7xjob4.com1.z0.glb.clouddn.com/8ce645bf3da25e2731b2fea4c21a985b

Input


The input file contains several test cases, each of them as described below.
On the first line, we have the number n of banks. On the second line, we have the capitals ki
(n > i ≥ 0) of all banks, in the order in which they are found on Wall Street from Wonderland. Each
capital is separated by a single whitespace from the next one, except for the final capital which is
directly followed by the newline character.

Output


For each test case, the output contains a single line with the value of the minimal number of magic
moves.

Sample Input


```
4
1 -2 -1 3
```

Sample Output


```
9
```

Source


2016-HUST-线下组队赛-4


##题意:

给出一个循环序列,每次可以操作可以把一个负数取反成a,并把其周围的两个数减去a.
求最少次数使得结果序列非负.


##题解:

如果序列能够达到全部非负的状态,那么无论先操作哪个数都是一样的次数.
所以直接暴力枚举所有负数,递归处理即可.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define maxn 10100
#define inf 0x3f3f3f3f
#define mod 1000000007
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define IN freopen("in.txt","r",stdin);
using namespace std;

int num[maxn];

int ans, n;

void dfs(int cur) {

if(num[cur] >=0) return ;

num[cur] = -num[cur]; ans++;

int l = cur - 1; if(l == 0) l = n;

int r = cur + 1; if(r == n+1) r = 1;

num[l] -= num[cur];

num[r] -= num[cur];

if(num[l] < 0) dfs(l);

if(num[r] < 0) dfs(r);

}

int main()

{

//IN;

while(scanf("%d", &n) != EOF)
{
for(int i=1; i<=n; i++) {
scanf("%d", &num[i]);
} ans = 0;
for(int i=1; i<=n; i++) {
if(num[i] < 0) {
dfs(i);
}
} printf("%d\n", ans);
} return 0;

}

UVALive 6855 Banks (暴力)的更多相关文章

  1. UVaLive 6855 Banks (水题,暴力)

    题意:给定 n 个数,让你求最少经过几次操作,把所有的数变成非负数,操作只有一种,变一个负数变成相反数,但是要把左右两边的数加上这个数. 析:由于看他们AC了,时间这么短,就暴力了一下,就AC了... ...

  2. UVALive 5107 dfs暴力搜索

    题目链接:A hard Aoshu Problem DES:给三个字符串,包含的字符是A-E范围内的.长度都不超过8.每个字符可以而且只可以匹配一个数字.两个字符不能匹配相同的数字.前两个式子之间可以 ...

  3. UVALive 5844 dfs暴力搜索

    题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...

  4. UVALive 4868 Palindrometer 暴力

    F - Palindrometer Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  5. UVaLive 6854 City (暴力)

    题意:给定一个 n*m 的矩阵,表示有多少条道路与它相连,其中有一个-1,表示未知,道路只能横着和竖着,求-1处的值. 析:根据题意可知,一个点,与其他周围的四个点都可能相连的,也就是说肯定有共用道路 ...

  6. UVaLive 4868 Palindrometer (暴力 / 构造)

    题意: 给定一个固定长度的字符串, 字符串是一个含有前导0的数字, 问这个数字加上多少能构成一个回文字符串. 分析: 其实这题有很多种方法, 方法12是我做完后看别人代码总结的, 方法3是我当时想的一 ...

  7. Gym 100299C && UVaLive 6582 Magical GCD (暴力+数论)

    题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...

  8. UVALive 7077 - Little Zu Chongzhi's Triangles(暴力)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)

    题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...

随机推荐

  1. 第四周课程总结&第二次实验报告

    实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...

  2. Maven 中 resources 作用

    默认情况下,如果没有指定resources,目前认为自动会将src/main/resources下的.xml文件放到target里头的classes文件夹下的package下的文件夹里.如果设定了re ...

  3. ls 命令通配符(3)

    使用过正则的人应该很熟悉通配符.如果非要解释的话,我找来一段: 通配符是一种特殊语句,主要有星号(*)和问号(?),用来模糊搜索文件. 当查找文件夹时,可以使用它来代替一个或多个真正字符:当不知道真正 ...

  4. python 生成list的所有的子集 (不使用递归且不引入标准库)

    不使用递归且不引入标准库,单纯用两个for循环即可得出一个list的所有子集 L = [1, 2, 3, 4] List = [[]] for i in range(len(L)):          ...

  5. [LeetCode] 52. N皇后 II

    题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...

  6. Java关于继承中的内存分配

    1.定义         super:当前对象的父类对象         this   :当前对象,谁调用this所在的方法,this就是哪一个对象.   2.内存分析 另一个例子: public s ...

  7. CSP-S全国模拟赛第二场 【nan】

    A.count 本场比赛最难的题... 隔板法组合数容斥 xjb 搞搞就好了 //by Judge #include<cstdio> #include<iostream> #d ...

  8. 在Myeclipse下查看Java字节码指令信息

         在实际项目开发中,有时为了了解Java编译器内部的一些工作,需要查看Java文件对应的具体的字节码指令集,这里提供两种方式供参考. 一.使用javap命令      javap是JDK提供的 ...

  9. Linux安装 jdk&maven

    JDK安装 1. 下载JDK压缩包http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  10. 移动端和pc端公用样式及rem布局

    一:移动端准备工作<meta name="viewport" content="width=device-width, initial-scale=1.0, max ...