Codeforces Round #230 (Div. 2) C. Blocked Points D. Tower of Hanoi
2014-02-19 23:26
387 查看
C. Blocked Points 题意:A点和B点是4-connected,的条件是
the Euclidean distance between A and B is
one unit and neither A nor B is
blocked;
or there is some integral point C, such that A is
4-connected with C, and C is
4-connected with B.
这段描述类似递归的意思,就是A 和B 4-connected 就是,A可以通过 非BLOCK的点 延伸到 B点。A B都是非blocked的。
距离 原点 的距离<=N的整数点为special 点,否则 为non-special点。
求 把最少数量的点blocked,使得所有的spe点和non-spe点 都不4-connected.
即 求距离原点的距离<=n的点构成边界,封锁起来。注意特判n==0.详见code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef __int64 LL;
#define INF 1000000000
#define N 80000000
LL n;
int main()
{
scanf("%I64d",&n);
LL sum=0;
LL last=n;
LL now;
for(LL i=1;i<=n;i++)
{
now= (LL)sqrt(1.0*(n*n-i*i));
//printf("%I64d - %I64d\n",i,now);
if(last==now) sum++;
else
{
sum+=(last-now);
}
last=now;
}
if(!n)
{
printf("1\n"); return 0;
}
printf("%I64d\n",sum*4);
return 0;
}
D. Tower of Hanoi
汉诺塔问题,不过不同的柱子之间的移动会有不同的cost,求把N个盘子从第一个移动到第三个,cost的和最小是多少。
DP,记忆化搜索。 dp
[i][j][k], 代表把n个盘子从i通过j到k。
dp
[i][j][k]的最优,是以下两种情况的最小者
1,把n-1个从l通过r移到m上,然后把第n个从l移到r上,最后把n-1个从m通过l移到r上。
2,把n-1个从l通过m移到r上,然后把第n个从l移到m上,然后把n-1个从r通过m移到l上,然后把第n个从m移到r上,最后把n-1个从l通过m移到r上。
详见code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
#define INF 1000000000
#define N 45
LL dp
[5][5][5],n;
LL cost[5][5];
LL dfs(LL nn,LL l,LL m,LL r)
{
if(!nn) return 0;
if(dp[nn][l][m][r]>=0) return dp[nn][l][m][r];
LL m1=dfs(nn-1,l,r,m)+cost[l][r]+dfs(nn-1,m,l,r);
LL m2=dfs(nn-1,l,m,r)+cost[l][m]+dfs(nn-1,r,m,l)+cost[m][r]+dfs(nn-1,l,m,r);
dp[nn][l][m][r] =min(m1,m2);
return dp[nn][l][m][r];
}
int main()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
scanf("%I64d",&cost[i][j]);
}
}
scanf("%I64d",&n);
memset(dp,-1,sizeof(dp));
printf("%I64d\n",dfs(n,1,2,3));
return 0;
}
the Euclidean distance between A and B is
one unit and neither A nor B is
blocked;
or there is some integral point C, such that A is
4-connected with C, and C is
4-connected with B.
这段描述类似递归的意思,就是A 和B 4-connected 就是,A可以通过 非BLOCK的点 延伸到 B点。A B都是非blocked的。
距离 原点 的距离<=N的整数点为special 点,否则 为non-special点。
求 把最少数量的点blocked,使得所有的spe点和non-spe点 都不4-connected.
即 求距离原点的距离<=n的点构成边界,封锁起来。注意特判n==0.详见code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef __int64 LL;
#define INF 1000000000
#define N 80000000
LL n;
int main()
{
scanf("%I64d",&n);
LL sum=0;
LL last=n;
LL now;
for(LL i=1;i<=n;i++)
{
now= (LL)sqrt(1.0*(n*n-i*i));
//printf("%I64d - %I64d\n",i,now);
if(last==now) sum++;
else
{
sum+=(last-now);
}
last=now;
}
if(!n)
{
printf("1\n"); return 0;
}
printf("%I64d\n",sum*4);
return 0;
}
D. Tower of Hanoi
汉诺塔问题,不过不同的柱子之间的移动会有不同的cost,求把N个盘子从第一个移动到第三个,cost的和最小是多少。
DP,记忆化搜索。 dp
[i][j][k], 代表把n个盘子从i通过j到k。
dp
[i][j][k]的最优,是以下两种情况的最小者
1,把n-1个从l通过r移到m上,然后把第n个从l移到r上,最后把n-1个从m通过l移到r上。
2,把n-1个从l通过m移到r上,然后把第n个从l移到m上,然后把n-1个从r通过m移到l上,然后把第n个从m移到r上,最后把n-1个从l通过m移到r上。
详见code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
#define INF 1000000000
#define N 45
LL dp
[5][5][5],n;
LL cost[5][5];
LL dfs(LL nn,LL l,LL m,LL r)
{
if(!nn) return 0;
if(dp[nn][l][m][r]>=0) return dp[nn][l][m][r];
LL m1=dfs(nn-1,l,r,m)+cost[l][r]+dfs(nn-1,m,l,r);
LL m2=dfs(nn-1,l,m,r)+cost[l][m]+dfs(nn-1,r,m,l)+cost[m][r]+dfs(nn-1,l,m,r);
dp[nn][l][m][r] =min(m1,m2);
return dp[nn][l][m][r];
}
int main()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
scanf("%I64d",&cost[i][j]);
}
}
scanf("%I64d",&n);
memset(dp,-1,sizeof(dp));
printf("%I64d\n",dfs(n,1,2,3));
return 0;
}
相关文章推荐
- 注重SEO搜索优化的div+css命名规则
- 让div居中的方法
- Codeforces Round #208 (Div. 2) Problem A Dima and Continuous Line(暴力)
- CF #262 (Div. 2)B
- Codeforces Round #304 (Div. 2) D Soldier and Number Game
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B. Guess the Permutation 水题
- AIM Tech Round 3 (Div. 2) E. Centroids (树形dp)
- js-div的变色
- Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) C.Ordering Pizza
- SRM 114 DIV2 [550]
- 弹出层Div 覆盖不住select控件的解决方法
- 栈应用--Codeforces Round #205 (Div. 2)B
- Codeforces Round #262 (Div. 2) D Little Victor and Set
- CF 209 Div2 (C) 快速幂 取分子最小的公约数
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) D. Minimization(dp)
- AIM Tech Round 3 (Div. 2) D. Recover the String 构造、贪心、多坑、WA162
- Codeforces Round #402 (Div2)D题
- Codeforces Round #437 (Div. 2) E. Buy Low Sell High
- DIV图层圆边圆角的实现
- 站在DIV+CSS的顶端