codeforces 424 D Biathlon Track

题意:给出一个布满数字的网格,可以上下左右走,若下一个格子的数字比当前格子要大或者小或者一样,分别都要付出代价。给出一个期望代价,问当要求顺时针走出一个长宽均大于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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值