UVALive 6855 Banks (暴力)
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 (暴力)的更多相关文章
- UVaLive 6855 Banks (水题,暴力)
题意:给定 n 个数,让你求最少经过几次操作,把所有的数变成非负数,操作只有一种,变一个负数变成相反数,但是要把左右两边的数加上这个数. 析:由于看他们AC了,时间这么短,就暴力了一下,就AC了... ...
- UVALive 5107 dfs暴力搜索
题目链接:A hard Aoshu Problem DES:给三个字符串,包含的字符是A-E范围内的.长度都不超过8.每个字符可以而且只可以匹配一个数字.两个字符不能匹配相同的数字.前两个式子之间可以 ...
- UVALive 5844 dfs暴力搜索
题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...
- UVALive 4868 Palindrometer 暴力
F - Palindrometer Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVaLive 6854 City (暴力)
题意:给定一个 n*m 的矩阵,表示有多少条道路与它相连,其中有一个-1,表示未知,道路只能横着和竖着,求-1处的值. 析:根据题意可知,一个点,与其他周围的四个点都可能相连的,也就是说肯定有共用道路 ...
- UVaLive 4868 Palindrometer (暴力 / 构造)
题意: 给定一个固定长度的字符串, 字符串是一个含有前导0的数字, 问这个数字加上多少能构成一个回文字符串. 分析: 其实这题有很多种方法, 方法12是我做完后看别人代码总结的, 方法3是我当时想的一 ...
- Gym 100299C && UVaLive 6582 Magical GCD (暴力+数论)
题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...
- UVALive 7077 - Little Zu Chongzhi's Triangles(暴力)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)
题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...
随机推荐
- MySQL-快速入门(10)触发器
1.什么是触发器 触发器是与表有关的命名数据库对象.触发器由事件来触发某个操作. 触发器是特殊的存储过程,触发器不需要call语句来调用,也不需要手工启动,只需要确定一个预定义的事件发生的时候,就会被 ...
- 部署CM集群首次运行报错:Formatting the name directories of the current NameNode.
1. 报错提示 Formatting the name directories of the current NameNode. If the name directories are not emp ...
- 循环Gray码的生成(递归)
#!/usr/bin/env python #coding:utf-8 import sys def gray_code(num, array): if num < 1: return if n ...
- JS实现每隔一段时间数量增加或减少
<div class="minge"> <p>仅剩 <span>126</span>个名额</p> </div&g ...
- mongodb启动报错,child process failed, exited with error number 1
error: child process failed, exited with error number 1 第一次安装mongodb,随后启动一般不会出现上面的错误,出现这种错误的原因一般是mon ...
- 小程序之如和使用view内部组件来进行页面的排版功能
这篇文章主要介绍了关于小程序之如和使用view内部组件来进行页面的排版功能,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 涉及知识点: 1.垂直排列,水平排列 2.居中对齐 示例: 1 ...
- luogu P5331 [SNOI2019]通信
传送门 有匹配次数限制,求最小代价,这显然是个费用流的模型.每个点暴力和前面的点连匹配边,边数是\(n^2\)的. 然后发现可以转化成一个set,每次加入一个点,然后入点对set里面的出点连边.这个s ...
- Redis数据结构&命令手册
Redis数据结构&命令手册 Redis数据结构 Redis可以存储键与5种不同数据结构之间的映射,这五种数据结构类型分别为STRING(字符串).LIST(列表).SET(集合).HASH( ...
- python 快速排序实现
# -*- coding: utf-8 -*- def quicksort(array): # 基线条件:为空或只包含一个元素的数组是"有序"的 if len(array) < ...
- Java8 将List转变为逗号分隔的字符串
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Tes ...