码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 424 D Biathlon Track

时间:2015-07-30 21:26:38      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出一个布满数字的网格,可以上下左右走,若下一个格子的数字比当前格子要大或者小或者一样,分别都要付出代价。给出一个期望代价,问当要求顺时针走出一个长宽均大于3的矩形的四条边后,最接近期望代价的情况。


做法:由于长宽大于3,直接暴力枚举即可。


#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int val[3];
int psum[4][310][310],a[310][310];
int main()
{
	int n,m,t;
	scanf("%d%d%d",&n,&m,&t);
	for(int i=0;i<3;i++)
		scanf("%d",&val[i]);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			scanf("%d",&a[i][j]);
	for(int i=0;i<n;i++)
		for(int j=1;j<m;j++)
			if(a[i][j-1]==a[i][j])
				psum[0][i][j]=psum[0][i][j-1]+val[0];
			else if(a[i][j-1]<a[i][j])
				psum[0][i][j]=psum[0][i][j-1]+val[1];
			else
				psum[0][i][j]=psum[0][i][j-1]+val[2];
	for(int i=0;i<n;i++)
		for(int j=m-2;j>-1;j--)
			if(a[i][j]==a[i][j+1])
				psum[1][i][j]=psum[1][i][j+1]+val[0];
			else if(a[i][j]>a[i][j+1])
				psum[1][i][j]=psum[1][i][j+1]+val[1];
			else
				psum[1][i][j]=psum[1][i][j+1]+val[2];
	for(int i=0;i<m;i++)
		for(int j=1;j<n;j++)
			if(a[j-1][i]==a[j][i])
				psum[2][j][i]=psum[2][j-1][i]+val[0];
			else if(a[j-1][i]<a[j][i])
				psum[2][j][i]=psum[2][j-1][i]+val[1];
			else
				psum[2][j][i]=psum[2][j-1][i]+val[2];
	for(int i=0;i<m;i++)
		for(int j=n-2;j>-1;j--)
			if(a[j][i]==a[j+1][i])
				psum[3][j][i]=psum[3][j+1][i]+val[0];
			else if(a[j][i]>a[j+1][i])
				psum[3][j][i]=psum[3][j+1][i]+val[1];
			else
				psum[3][j][i]=psum[3][j+1][i]+val[2];
	int mn=INT_MAX,ans[4];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			for(int k=i+2;k<n;k++)
			{
				int t1=psum[3][i][j]-psum[3][k][j];
				for(int l=j+2;l<m;l++)
				{
					int t2=psum[2][k][l]-psum[2][i][l];
					int t3=psum[0][i][l]-psum[0][i][j];
					int t4=psum[1][k][j]-psum[1][k][l];
					int sub=abs(t-t1-t2-t3-t4);
					if(sub<mn)
					{
						mn=sub;
						ans[0]=i;ans[1]=j;ans[2]=k;ans[3]=l;
					}
				}
			}
	for(int i=0;i<4;i++)
		printf("%d ",ans[i]+1);
}


time limit per test
4.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 takes td 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?=?3tu?=?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 nm and t (3?≤?n,?m?≤?3001?≤?t?≤?109) — the sizes of the land plot and the desired distance covering time.

The second line also contains three integers tptu 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 test(s)
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



版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 424 D Biathlon Track

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/47155761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!