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;

}

最新文章

  1. 使用NPOI组件完成的Excel导出导入(附源代码,测试通过)
  2. [C#6] 2-nameof 运算符
  3. iOS开发(OC)中的命名规范
  4. flexgrid的应用
  5. jQuery mouseover与mouseenter的区别
  6. 数字证书管理工具keytool常用命令介绍
  7. start stack
  8. JVM垃圾收集算法——分代收集算法
  9. [编译] 6、开源两个简单且有用的安卓APP命令行开发工具和nRF51822命令行开发工具
  10. Android Studio集成Flutter
  11. [dts]Device Tree机制【转】
  12. IE6设置li的float:left,不能自适应宽的解决方法
  13. 【LOJ6053】简单的函数(min_25筛)
  14. Webstorm通用设置
  15. springboot日志配置
  16. CNTA-2019-0014 wls9-async 反序列化 rce 分析
  17. Pathon1 - 基础1
  18. SQLite数据库管理工具(SQLiteStudio)v3.1.1
  19. git web找不到new project解决方法
  20. 算法:快速排序实现 &amp; 定制比较函数

热门文章

  1. python函数-内置函数
  2. C#打印条码BarTender SDK打印之路和离开之路(web平凡之路)(转)
  3. java通过正则进行语法分析实现表达式的逻辑判断和复杂计算实现
  4. 极*Java速成教程 - (7)
  5. windows10操作系统上使用virtualenv虚拟环境
  6. [Nest] 01.初见nest.js
  7. SpringBoot自定义配置步骤
  8. 记录一次TabBar使用本地图片
  9. styled-components缺点
  10. Redis持久化rdb&amp;aof