CodeForces 213C Relay Race(dp)
2016-07-19 10:08
316 查看
CodeForces 213C Relay Race
Relay Race
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells
(represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs
towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1).
After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell(i, j),
then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player
goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th
number on the i-th line ai, j( - 1000 ≤ ai, j ≤ 1000) is
the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
input
output
input
output
input
output
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3),
and for Rubik: (3, 3), (3, 2), (2, 2),(2, 1), (1, 1). The figure
to the sample:
Furik's path is marked with yellow, and Rubik's path is marked with pink.
题意:
接力赛跑,给出一张nxn的地图,一个人站在(1,1),另一个人站在(n,n),地图上每个点有一个数值(-1000~+1000),先从(1,1)向(n,n)跑(只能向下或向右),然后由(n,n)跑回(1,1)(只能向左或向上),每跑到一个点会获得该点的数值,每个点的数值只能获得一次。
问能取得的数值的总和最大为多少。
分析:
相当于有两条由(1,1)到(n,n)的路径,求路径上所有点的值的总和最大为多少(每个点只能取一次)。d[time][x1][x2],time为当前步数,x1为路径1的当前位置,x2为路径2的当前位置,由于每一步只跟上一步的状态有关,所以第一维可以简化大小为2。
考虑负数最大值情况初始化d。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int max(int a,int b,int c){
if(a>b){
if(a>c)return a;
else return c;
}
else{
if(b>c)return b;
else return c;
}
}
int main(){
int n,map[305][305],dp[2][305][305],x,ma;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
memset(map,0,sizeof(map));
for(int i=0;i<2;i++){
for(int j=0;j<305;j++){
for(int k=0;k<305;k++){
dp[i][j][k]=-100000000;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j]);
}
}
x=0;
dp[0][1][1]=map[1][1];
for(int i=1;i<2*n-1;i++){
x^=1;
for(int j=(i+1<n?1:i-n+2);j<=(i+1<n?i+1:n);j++){
for(int k=j;k<=(i+1<n?i+1:n);k++){
if(j==k){
ma=max(dp[x^1][j-1][k-1],dp[x^1][j-1][k],dp[x^1][j][k]);
dp[x][j][k]=ma+map[j][i-j+2];
}
else{
ma=max(dp[x^1][j-1][k-1],dp[x^1][j-1][k],dp[x^1][j][k]);
if(ma<dp[x^1][j][k-1])ma=dp[x^1][j][k-1];
dp[x][j][k]=ma+map[j][i-j+2]+map[k][i-k+2];
}
//printf("j %d k %d dp %d\n",j,k,dp[x][j][k]);
}
}
}
if(n==1)printf("%d\n",map[1][1]);
else printf("%d\n",dp[x]
);
return 0;
}
Relay Race
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells
(represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs
towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1).
After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell(i, j),
then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player
goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th
number on the i-th line ai, j( - 1000 ≤ ai, j ≤ 1000) is
the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
input
1 5
output
5
input
2 11 14 16 12
output
53
input
3
25 16 2512 18 19
11 13 8
output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3),
and for Rubik: (3, 3), (3, 2), (2, 2),(2, 1), (1, 1). The figure
to the sample:
Furik's path is marked with yellow, and Rubik's path is marked with pink.
题意:
接力赛跑,给出一张nxn的地图,一个人站在(1,1),另一个人站在(n,n),地图上每个点有一个数值(-1000~+1000),先从(1,1)向(n,n)跑(只能向下或向右),然后由(n,n)跑回(1,1)(只能向左或向上),每跑到一个点会获得该点的数值,每个点的数值只能获得一次。
问能取得的数值的总和最大为多少。
分析:
相当于有两条由(1,1)到(n,n)的路径,求路径上所有点的值的总和最大为多少(每个点只能取一次)。d[time][x1][x2],time为当前步数,x1为路径1的当前位置,x2为路径2的当前位置,由于每一步只跟上一步的状态有关,所以第一维可以简化大小为2。
考虑负数最大值情况初始化d。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int max(int a,int b,int c){
if(a>b){
if(a>c)return a;
else return c;
}
else{
if(b>c)return b;
else return c;
}
}
int main(){
int n,map[305][305],dp[2][305][305],x,ma;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
memset(map,0,sizeof(map));
for(int i=0;i<2;i++){
for(int j=0;j<305;j++){
for(int k=0;k<305;k++){
dp[i][j][k]=-100000000;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j]);
}
}
x=0;
dp[0][1][1]=map[1][1];
for(int i=1;i<2*n-1;i++){
x^=1;
for(int j=(i+1<n?1:i-n+2);j<=(i+1<n?i+1:n);j++){
for(int k=j;k<=(i+1<n?i+1:n);k++){
if(j==k){
ma=max(dp[x^1][j-1][k-1],dp[x^1][j-1][k],dp[x^1][j][k]);
dp[x][j][k]=ma+map[j][i-j+2];
}
else{
ma=max(dp[x^1][j-1][k-1],dp[x^1][j-1][k],dp[x^1][j][k]);
if(ma<dp[x^1][j][k-1])ma=dp[x^1][j][k-1];
dp[x][j][k]=ma+map[j][i-j+2]+map[k][i-k+2];
}
//printf("j %d k %d dp %d\n",j,k,dp[x][j][k]);
}
}
}
if(n==1)printf("%d\n",map[1][1]);
else printf("%d\n",dp[x]
);
return 0;
}
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题