题意:给定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. 使用CodeSmith快速生成映射文件和映射类

    一 CodeSmith简介 本文以表自动生成NHibernate的映射文件和映射类的实例来说明一下本软件的使用方法. CodeSmith是一种基于模板的代码生成工具,其使用类似于ASP.NET的语法来 ...

  2. leetcode 168

    168. Excel Sheet Column Title Given a positive integer, return its corresponding column title as app ...

  3. java学习第三天

    关于上次的数据转换,如果定义的是 short 是,那么s=s+1,这样是错误的,会损失精度,但如果是s+=1.确实正确的,因为它包含了一个强制转化在里面.相当于 s=(int)(s+1);  然后特地 ...

  4. 5.Makefile的原理及应用

    1.概念 目标:目标顶格写,后面是冒号(冒号后面是依赖) 依赖:依赖是用来产生目标的原材料. 命令:命令前面一定是两个Tab,不能是定格,也不能说多个空格.命令就是要生成那个目标需要做的动作. 2.基 ...

  5. CAPI学习心得

    比较有意义资料 Education .ppt User-guide Coherent Accelerator Interface Architecture.pdf ------------------ ...

  6. empty isset array_key_exists 的区别

    empty: 参数为0或为NULL时(如上面列子),empty均返回TRUE,详细情况可以参见empty官方手册 isset: 参数为NULL时,返回FALSE,0与NULL在PHP中是有区别的,is ...

  7. Nginx编译参数大全 configure参数中文详解

    ./configure --help--help 显示本提示信息--prefix=PATH 设定安装目录--sbin-path=PATH 设定程序文件目录--conf-path=PATH 设定配置文件 ...

  8. NOI 2001 食物链(eat)

    1074 食物链 2001年NOI全国竞赛  时间限制: 3 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description ...

  9. python 循环定时器

    有时候需要循环执行某个任务,最简单的就是用thread.Timer. 谷歌了一下,发现大家竟然用sleep 来实现循环,也不知道谁想的这个方法,竟然很少有人想到join一下,很奇怪. # -*- co ...

  10. MySQL高可用方案选型参考

    http://imysql.com/2015/09/14/solutions-of-mysql-ha.shtml#rd?sukey=b0cb5c5b9e501303123bd48e86555c35e0 ...