time limit per test2.5 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on.

Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t1, t2, …, tn. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2n - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it’s possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash.

Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion.

The second line contains n integers t1, t2, …, tn (1 ≤ ti ≤ 5). On the i-th level each of 2i - 1 parts will cover ti cells before exploding.

Output

Print one integer, denoting the number of cells which will be visited at least once by any part of the firework.

Examples

input

4

4 2 2 3

output

39

input

6

1 1 1 1 1 3

output

85

input

1

3

output

3

Note

For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an ‘X’ character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39.

For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level.

【题目链接】:http://codeforces.com/contest/750/problem/D

【题解】



记忆化搜索;

如果烟花是一直朝着一个方向放的话,最高到达的位置是5*30=150

每个点作为爆炸点,是否爆炸过;

显然在不同时刻同一坐标爆炸的效果是不同的;





bo[MAXN][300][300][10]这样的bool数组进行判重工作;

第一维是当前处理的是第几个爆炸

接下来第2和第3维表示在哪一个点发生的爆炸;

第4维表示的是爆炸的延续方向;

进行这样一次爆炸过后把这个bool设置为true;

下次就不会重复工作了

可以发现爆炸的方向规律就是当前的位置中八个相邻的位置;

当前的爆炸方向的相邻方向;

所以const dx,dy那个方向数组一定要是顺时针或逆时针按顺序排的;

不然WA死.



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 30+10;
const int dx[8] = {1,1,0,-1,-1,-1,0,1};
const int dy[8] = {0,1,1,1,0,-1,-1,-1};
const double pi = acos(-1.0); int n,t[MAXN],ans = 0;
bool color[400][400];
bool vis[MAXN][400][400][10]; void dfs(int now,int x,int y,int d)
{
if (vis[now][x][y][d]==1)
return;
vis[now][x][y][d] = 1;
if (!color[x][y])
{
color[x][y] = 1;
ans++;
}
int tx = x,ty = y;
rep1(i,1,t[now]-1)
{
tx+=dx[d],ty+=dy[d];
if (!color[tx][ty])
{
color[tx][ty] = 1;
ans++;
}
}
if (now == n)
return;
//
int td = (d+1)%8;
dfs(now+1,tx+dx[td],ty+dy[td],td);
//
td = (d+7)%8;
dfs(now+1,tx+dx[td],ty+dy[td],td);
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(t[i]);
dfs(1,200,200,0);
printf("%d\n",ans);
return 0;
}

【codeforces 750D】New Year and Fireworks的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. How to remove a Data Guard Configuration from Primary Database (文档 ID 733794.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.3 [Release 10.1 to 11. ...

  2. 关于React中,map出来的元素添加事件问题

    用es6 map 的写法 直接绑定一个onTouchStart 事件不会报错. 用es5的map写法  如果不加上this  会报这个错误 无法读取未定义的属性 解决的方法是 绑定this  就可以了

  3. Android 一个异步SocketHelper

    发送流程:首先定义一个缓冲池,发送数据时仅仅是将待发送的数据加入到缓冲池中,再由后台的工作线程从缓冲池中取得待发送数据进行发送.可能某些情况下在数据发送完成时需要做一些处理(比如写日志),便定义了一个 ...

  4. JQ实现选项卡(jQuery原型插件扩展)

    下边分为两个版本,一种是点击切换选项(index.js),一种是滑过切换选项(index1.js) HTML文件: jq使用jquery-1.11.3.js版本 <!DOCTYPE html&g ...

  5. 关于javascript中私有作用域的预解释

    1.如何区分私有变量还是全局变量 1).在全局作用域下声明(预解释的时候)的变量是全局变量 2).在“私有作用域中声明的变量”和“函数的形参”都是私有变量 在私有作用域中,我们代码执行的时候遇到一个变 ...

  6. [Angular] Reactive Store and AngularFire Observables

    A simple store implemenet: import { Observable } from 'rxjs/Observable'; import { BehaviorSubject } ...

  7. 算法-对分查找(二分查找)C++实现

    这个是个主要的查找算法.由于仅仅是把数读入就须要(N)的时间量,因此我们在说这类问题的时候都是如果读入过的. 在算法经常使用的时间.将问题缩小为一部分(大约1/2),那么我们就觉得这个算法是O(log ...

  8. 内存、时间复杂度、CPU/GPU以及运行时间

    衡量 CPU 的计算能力: 比如一个 Intel 的 i5-2520M @2.5 Ghz 的处理器, 则其计算能力 2.5 * 4(4核) = 10 GFLOPS FLOP/s,Floating-po ...

  9. Altium Designer敷铜的规则设定

    InPolygon 这个词是铺铜对其他网络的设置,铺铜要离其他网络远点,因为腐蚀不干净会对 电路板有影响... 问题一:: 如下图所示,现在想让敷铜与板子边界也就是keepoutlayer的间距小一点 ...

  10. HDU 1212 Big Number 大数模小数

    http://acm.hdu.edu.cn/showproblem.php?pid=1212 题目大意: 给你一个长度不超过1000的大数A,还有一个不超过100000的B,让你快速求A % B. 什 ...