http://acm.hust.edu.cn/vjudge/contest/130303#problem/Aios
http://7xjob4.com1.z0.glb.clouddn.com/8ce645bf3da25e2731b2fea4c21a985b
api
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.
spa
For each test case, the output contains a single line with the value of the minimal number of magic
moves.
3d
4 1 -2 -1 3
9
2016-HUST-線下組隊賽-4
code
給出一個循環序列,每次能夠操做能夠把一個負數取反成a,並把其周圍的兩個數減去a.
求最少次數使得結果序列非負.
遞歸
若是序列可以達到所有非負的狀態,那麼不管先操做哪一個數都是同樣的次數.
因此直接暴力枚舉全部負數,遞歸處理便可.
ip
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #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; }