题意:给定n,m<=300的矩阵,然后求一个长宽大于2的矩形,使得是从左上角位置开始逆时针绕边框一圈的时间最少。时间的计算是:给定3个数tu,tp, td,路径上数字增加为tu,相等为tp否则为td。

思路:直接预处理出4个数组,然后直接暴力。n4的方法。但是常数小可以4000+ms过。

还有一种n3logn的方法。。

具体是固定右下角,然后枚举右上角,然后通过一个通过地推维护一个前缀,二分查找。。

不过常数肯定比暴力大估计快不了多少。。

code:

 #include <bits/stdc++.h>
#define M0(x) memset(x, 0, sizeof(x))
#define repf(i, a, b) for(int i = (a); i <= (b); ++i)
#define repd(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;
int U[][], D[][], L[][], R[][], a[][];
int n, m;
int tp, tu, td, t; inline int f(const int& a, const int& b){
return a == b ? tp : (a > b ? tu : td);
} void init(){
scanf("%d%d%d", &tp, &tu, &td);
M0(U), M0(D), M0(L), M0(R), M0(a);
repf(i, , n) repf(j, , m) scanf("%d", &a[i][j]);
repf(i, , n) repf(j, , m){
D[i][j] = D[i-][j] + f(a[i][j], a[i-][j]);
R[i][j] = R[i][j-] + f(a[i][j], a[i][j-]);
}
repd(i, n, ) repd(j, m, ){
U[i][j] = U[i+][j] + f(a[i][j], a[i+][j]);
L[i][j] = L[i][j+] + f(a[i][j], a[i][j+]);
}
} void solve(){
int ansd = 0x3fffffff, ans = ;
int lx, ly, rx, ry, d;
int tmp;
for (int x = ; x <= n; ++x)
for (int y = ; y <= m; ++y)
for (int x1 = x + ; x1 <= n; ++x1)
for (int y1 = y + ; y1 <= m; ++y1){
tmp = D[x1][y1] - D[x][y1] + U[x][y] - U[x1][y];
tmp += (R[x][y1] - R[x][y] + L[x1][y] - L[x1][y1]);
d = abs(tmp - t);
if (d < ansd || (d == ansd && tmp < ans)){
lx = x, ly = y;
rx = x1, ry = y1;
ansd = d, ans = tmp;
}
}
// cout <<ans << endl;
printf("%d %d %d %d\n", lx, ly, rx, ry);
} int main(){
// freopen("a.in", "r", stdin);
while (scanf("%d%d%d", &n, &m, &t) != EOF){
init();
solve();
}
return ;
}

codeforces 424D的更多相关文章

  1. codeforces 424D Biathlon Track

    codeforces 424D Biathlon Track 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define f ...

  2. CodeForces 424D: ...(二分)

    题意:给出一个n*m的矩阵,内有一些数字.当你从一个方格走到另一个方格时,按这两个方格数字的大小,有(升,平,降)三种费用.你需要在矩阵中找到边长大于2的一个矩形,使得按这个矩形顺时针行走一圈的费用, ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Ms sql pivot unpivot

    --建表 create table dbo.orders ( orderid int not null primary key nonclustered, orderdate datetime not ...

  2. PIC32MZ tutorial -- Input Capture

    Today I accomplish a simple application for PIC32MZ EC Starter Kit. This application uses Input Capt ...

  3. NHibernate系列文章六:NHibernate数据类型映射

    摘要 NHibernate支持所有的数据库数据类型. 以SQL Server数据库为例,下表是NHibernate支持的SQL Server数据库最常见的数据类型对照表. 第一列是NHibernate ...

  4. Java利用aspose-words将word文档转换成pdf(破解 无水印)

    首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...

  5. Ninject的使用

    摘要 DI容器的一个责任是管理他创建的对象的生命周期.他应该决定什么时候创建一个给定类型的对象,什么时候使用已经存在的对象.他还需要在对象不需要的时候处理对象.Ninject在不同的情况下管理对象的生 ...

  6. memcpy code

    memcpy #include <stddef.h> //#include <stdint.h> //uintptr_t is quoted.#include "st ...

  7. 在本地机器上能访问tomcat,远程机器访问不了的解决方法

    问题描述:在测试服务器上搭建了一个tomcat,在测试服务器上能用ip打开tomcat.我用自己的机器能远程桌面能登录到测试服务器上,但在自己的机器上无法通过ip来访问测试服务器上的tomcat. 解 ...

  8. WebForm 简单控件、复合控件

    简单控件: Label:被编译成span 样式表里设置lable的高度:  display:inline-block; Text  --文本 ForeColor  --字体颜色 Visible  -- ...

  9. (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  10. sp_MSforeachtable使用方法

    1)说明系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程,从ms sql 6.5开始.存放在SQL Server的MASTER数据库中. ...