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. js 自调函数

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

  2. Gson的基本使用

    gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的 ...

  3. Intersection of Two Arrays(交集)

    来源:https://leetcode.com/problems/intersection-of-two-arrays Given two arrays, write a function to co ...

  4. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  5. J.U.C|一文搞懂AQS(转)

    提到JAVA加锁,我们通常会想到synchronized关键字或者是Java Concurrent Util(后面简称JCU)包下面的Lock,今天就来扒一扒Lock是如何实现的,比如我们可以先提出一 ...

  6. Redis主从架构核心原理

    Redis-Cluster工作原理: redis集群内置了16384个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果 ...

  7. CSS链接使用伪类的顺序

    顺序为:link-visited-hover-active a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: #00FF00} /* 已访问 ...

  8. Linux 下编写.sh文件运行JAR下的Class

    #!/bin/sh #bash文件头 APP_HOME=/opt/CrxToMongodb #目录是/opt/CrxToMongodb CLASSPATH=$APP_HOME #bin目录当然是包含j ...

  9. memset,内存初始化函数

    # include <string.h> void *memset(void *s, int c, unsigned long n); 函数的功能是:将指针变量 s 所指向的前 n 字节的 ...

  10. 20190614笔记(颜色透明度,css,filter,滤镜,计算属性,json和formData转换)

    今天忙里偷闲,把最近做的笔记做一下整理. 1.json和formData互相转换 适用场景:对接后台接口.后台接口写的因人而异,不同的人有不同的风格.比如,文件上传,原本就是formData格式,有人 ...