CodeForces 424D Biathlon Track【二维dp】
2015-12-30 15:47
441 查看
Description
Recently an official statement of the world Olympic Committee said that the Olympic Winter Games 2030 will be held in Tomsk. The city officials decided to prepare for the Olympics thoroughly and to build all the necessary Olympic facilities as early as possible.
First, a biathlon track will be built.
To construct a biathlon track a plot of land was allocated, which is a rectangle divided into n × m identical squares. Each of the squares has two coordinates: the number of the row
(from 1 to n), where it is located, the number of the column (from 1 to m), where it is located. Also each of the squares is characterized
by its height. During the sports the biathletes will have to move from one square to another. If a biathlete moves from a higher square to a lower one, he makes a descent. If a biathlete moves from a lower square to a higher one, he makes an ascent. If a biathlete
moves between two squares with the same height, then he moves on flat ground.
The biathlon track should be a border of some rectangular area of the allocated land on which biathletes will move in the clockwise direction. It is known that on one move on flat ground an average biathlete spends tp seconds,
an ascent takes tu seconds, a descent takestd seconds.
The Tomsk Administration wants to choose the route so that the average biathlete passes it in as close to t seconds as possible. In other words, the difference between time ts of
passing the selected track and t should be minimum.
For a better understanding you can look at the first sample of the input data. In this sample n = 6, m = 7, and the administration wants the track covering time to be as close to t = 48 seconds
as possible, also, tp = 3, tu = 6 and td = 2.
If we consider the rectangle shown on the image by arrows, the average biathlete can move along the boundary in a clockwise direction in exactly 48 seconds. The upper left corner of this track is located
in the square with the row number 4, column number 3 and the lower right corner is at square with row number 6,
column number 7.
Among other things the administration wants all sides of the rectangle which boundaries will be the biathlon track to consist of no less than three squares and to be completely contained within the selected land.
You are given the description of the given plot of land and all necessary time values. You are to write the program to find the most suitable rectangle for a biathlon track. If there are several such rectangles, you are allowed to print any of them.
Input
The first line of the input contains three integers n, m and t (3 ≤ n, m ≤ 300, 1 ≤ t ≤ 109)
— the sizes of the land plot and the desired distance covering time.
The second line also contains three integers tp, tu and td (1 ≤ tp, tu, td ≤ 100)
— the time the average biathlete needs to cover a flat piece of the track, an ascent and a descent respectively.
Then n lines follow, each line contains m integers that set the heights of each square of the given plot of land. Each of the
height values is a positive integer, not exceeding 106.
Output
In a single line of the output print four positive integers — the number of the row and the number of the column of the upper left corner and the number of the row and the number of the column of the lower right corner of the rectangle that is chosen for
the track.
Sample Input
Input
Output
挺简单的题,就看你敢不敢O(n^4)写==都说了是4500ms了,放心大胆的写吧,就是注意,正着走和反着走虽说是同一条线段,但是值不一样啊!因为上下有区别啊!!
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[305][305][4];
int num[303][303];
int n,m,tp,tu,td,t;
int f(int a,int b)
{
if(a>b) return td;
else if(a==b) return tp;
else return tu;
}
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&t))
{
scanf("%d%d%d",&tp,&tu,&td);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&num[i][j]);
for(int j=2;j<=m;j++){
for(int i=1;i<=n;i++){
dp[i][j][0]=dp[i][j-1][0]+f(num[i][j-1],num[i][j]);
}
}
for(int j=m-1;j>=1;j--){
for(int i=1;i<=n;i++){
dp[i][j][2]=dp[i][j+1][2]+f(num[i][j+1],num[i][j]);
}
}
for(int i=2;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j][1]=dp[i-1][j][1]+f(num[i-1][j],num[i][j]);
}
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=m;j++){
dp[i][j][3]=dp[i+1][j][3]+f(num[i+1][j],num[i][j]);
}
}
int maxn=0x3f3f3f3f,x1,x2,y1,y2;
bool flag=true;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(int ii=i+2;ii<=n;ii++){
for(int jj=j+2;jj<=m;jj++){
int t0=dp[i][jj][0]-dp[i][j][0];
int t1=dp[ii][jj][1]-dp[i][jj][1];
int t2=dp[ii][j][2]-dp[ii][jj][2];
int t3=dp[i][j][3]-dp[ii][j][3];
int dt=t0+t1+t2+t3;
int r=abs(dt-t);
if(r<maxn){
maxn=r;
x1=i;
y1=j;
x2=ii;
y2=jj;
if(maxn==0){
flag=false;
}
}
}
}
}
}
printf("%d %d %d %d\n",x1,y1,x2,y2);
}
return 0;
}
Recently an official statement of the world Olympic Committee said that the Olympic Winter Games 2030 will be held in Tomsk. The city officials decided to prepare for the Olympics thoroughly and to build all the necessary Olympic facilities as early as possible.
First, a biathlon track will be built.
To construct a biathlon track a plot of land was allocated, which is a rectangle divided into n × m identical squares. Each of the squares has two coordinates: the number of the row
(from 1 to n), where it is located, the number of the column (from 1 to m), where it is located. Also each of the squares is characterized
by its height. During the sports the biathletes will have to move from one square to another. If a biathlete moves from a higher square to a lower one, he makes a descent. If a biathlete moves from a lower square to a higher one, he makes an ascent. If a biathlete
moves between two squares with the same height, then he moves on flat ground.
The biathlon track should be a border of some rectangular area of the allocated land on which biathletes will move in the clockwise direction. It is known that on one move on flat ground an average biathlete spends tp seconds,
an ascent takes tu seconds, a descent takestd seconds.
The Tomsk Administration wants to choose the route so that the average biathlete passes it in as close to t seconds as possible. In other words, the difference between time ts of
passing the selected track and t should be minimum.
For a better understanding you can look at the first sample of the input data. In this sample n = 6, m = 7, and the administration wants the track covering time to be as close to t = 48 seconds
as possible, also, tp = 3, tu = 6 and td = 2.
If we consider the rectangle shown on the image by arrows, the average biathlete can move along the boundary in a clockwise direction in exactly 48 seconds. The upper left corner of this track is located
in the square with the row number 4, column number 3 and the lower right corner is at square with row number 6,
column number 7.
Among other things the administration wants all sides of the rectangle which boundaries will be the biathlon track to consist of no less than three squares and to be completely contained within the selected land.
You are given the description of the given plot of land and all necessary time values. You are to write the program to find the most suitable rectangle for a biathlon track. If there are several such rectangles, you are allowed to print any of them.
Input
The first line of the input contains three integers n, m and t (3 ≤ n, m ≤ 300, 1 ≤ t ≤ 109)
— the sizes of the land plot and the desired distance covering time.
The second line also contains three integers tp, tu and td (1 ≤ tp, tu, td ≤ 100)
— the time the average biathlete needs to cover a flat piece of the track, an ascent and a descent respectively.
Then n lines follow, each line contains m integers that set the heights of each square of the given plot of land. Each of the
height values is a positive integer, not exceeding 106.
Output
In a single line of the output print four positive integers — the number of the row and the number of the column of the upper left corner and the number of the row and the number of the column of the lower right corner of the rectangle that is chosen for
the track.
Sample Input
Input
6 7 48 3 6 2 5 4 8 3 3 7 9 4 1 6 8 7 1 1 1 6 4 6 4 8 6 7 2 6 1 6 9 4 1 9 8 6 3 9 2 4 5 6 8 4 3 7
Output
4 3 6 7
挺简单的题,就看你敢不敢O(n^4)写==都说了是4500ms了,放心大胆的写吧,就是注意,正着走和反着走虽说是同一条线段,但是值不一样啊!因为上下有区别啊!!
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[305][305][4];
int num[303][303];
int n,m,tp,tu,td,t;
int f(int a,int b)
{
if(a>b) return td;
else if(a==b) return tp;
else return tu;
}
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&t))
{
scanf("%d%d%d",&tp,&tu,&td);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&num[i][j]);
for(int j=2;j<=m;j++){
for(int i=1;i<=n;i++){
dp[i][j][0]=dp[i][j-1][0]+f(num[i][j-1],num[i][j]);
}
}
for(int j=m-1;j>=1;j--){
for(int i=1;i<=n;i++){
dp[i][j][2]=dp[i][j+1][2]+f(num[i][j+1],num[i][j]);
}
}
for(int i=2;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j][1]=dp[i-1][j][1]+f(num[i-1][j],num[i][j]);
}
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=m;j++){
dp[i][j][3]=dp[i+1][j][3]+f(num[i+1][j],num[i][j]);
}
}
int maxn=0x3f3f3f3f,x1,x2,y1,y2;
bool flag=true;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(int ii=i+2;ii<=n;ii++){
for(int jj=j+2;jj<=m;jj++){
int t0=dp[i][jj][0]-dp[i][j][0];
int t1=dp[ii][jj][1]-dp[i][jj][1];
int t2=dp[ii][j][2]-dp[ii][jj][2];
int t3=dp[i][j][3]-dp[ii][j][3];
int dt=t0+t1+t2+t3;
int r=abs(dt-t);
if(r<maxn){
maxn=r;
x1=i;
y1=j;
x2=ii;
y2=jj;
if(maxn==0){
flag=false;
}
}
}
}
}
}
printf("%d %d %d %d\n",x1,y1,x2,y2);
}
return 0;
}
相关文章推荐
- 阿里巴巴2016校招面试经验
- struts2 StrutsPrepareAndExecuteFilter 源码分析
- 添加场景的进入方式(从左往右,渐进)
- DDD领域驱动设计基本理论知识总结
- Error: [ngModel:constexpr] 的解决办法
- windows下node.js及express的安装
- 实验四 主存空间的分配和回收模拟
- 云南省政府与华为签署战略合作协议 共创云计算产业发展新局面
- c语言_FILE结构体解释及相关操作
- 给linux添加一个回收站
- GeoSever安装及服务发布
- Android Studio 安装配置详细步骤,以及使用的基本操作
- Spring事务管理--高级应用难点剖析: 第 2 部分
- Sublime Text3注册码
- ZJUT_1174
- java中通过文件路径获取文件名
- iOS 给图片加点击事件
- java正则表达式验证手机号码
- 没有一种成功是不经历磨砺的
- 计算机网络学习(二)——传输层