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,但是不会啊...就想暴 ...
随机推荐
- ls 命令通配符(3)
使用过正则的人应该很熟悉通配符.如果非要解释的话,我找来一段: 通配符是一种特殊语句,主要有星号(*)和问号(?),用来模糊搜索文件. 当查找文件夹时,可以使用它来代替一个或多个真正字符:当不知道真正 ...
- ThinkPHP5 与 composer
我是通过安装composer来获取tp5安装的,也是学习一下 在 Windows 中,你需要下载并运行 Composer-Setup.exe. 安装时,要选择本地环境中php.exe作为跟目录,这个可 ...
- Spark出现java.lang.stackoverflowerror的解决方法
正在测试的程序需要多次迭代(400+次),每次迭代有复杂的运算 迭代到100多次的时候报java.lang.stackoverflowerror的错误 解决方法:先checkpoint()再count ...
- Educational Codeforces Round 72 (Rated for Div. 2) Solution
传送门 A. Creating a Character 设读入的数据分别为 $a,b,c$ 对于一种合法的分配,设分了 $x$ 给 $a$ 那么有 $a+x>b+(c-x)$,整理得到 $x&g ...
- xss过滤与单例模式(对象的实例永远用一个)
kindeditor里面可以加入script代码,使用re可以过滤掉python有个专门的模块可以处理这种情况,beautifulsoup4 调用代码: content = XSSFilter().p ...
- Django验证码实现
1.点击验证码更换新的验证码 2.验证码必须是图片形式的 3.验证码实现的流程 服务端: a. session中保存随机验证码,如:87fs b.把验证码写到一个白板里面制作成图片 c. 在页面中显示 ...
- C#导出大量数据到excel,怎么提升性能
一,要提升性能,我们先要知道耗时的地方在哪里 1,数据库查询,2,把数据组合成新集合循环嵌套太多 二,那我们怎么优化呢? 一,数据库查询,1>,数据库查询:如果数据量小,我们可以用临时datat ...
- vuex深入
多模块 http://www.php.cn/js-tutorial-385084.html Vuex 模块化+命名空间后, 如何调用其他模块的 state, actions, mutations, ...
- Codeforces 959 树构造 暴力求最小字典序互质序列
A B C 题目给你一个结论 最少需要min((odd,even)个结点可以把一棵树的全部边连起来 要求你输出两颗树 一棵树结论是正确的 另外一棵结论是正确的 正确结论的树很好造 主要是错误的树 题目 ...
- Java数据封装成树形结构,多级
参考地址:https://blog.csdn.net/chendu500qiang/article/details/91493147 1.实体类 @data public class PublishS ...