题意:给定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. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

    今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...

  2. coroSync packmarker

    CoroSync+Pacemaker实现web高可用 2015-04-12 23:38:19 标签:CoroSync pacemaker 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 ...

  3. MySQL 批量插入 Update时Replace

    建一张试验表如下: 一.批量插入 MySQL的INSERT有一种写法如下: INSERT INTO person VALUES (NULL,'关羽', '2016-04-22 10:00:00'), ...

  4. 41. 这张表lbdeveloplog可以查看各个对象的提交情况,包括是哪个IP提交的

    lbdeveloplog 这张表lbdeveloplog可以查看各个对象的提交情况,包括是哪个IP提交的

  5. 测试Python代码

    作为程序员,懂得测试,这是必须的职业技能.很遗憾,我以前从未意识到这点,因此经历了很多叫苦不迭的开发生涯.当然了,期望每个人都成为测试高手也是不可能的,但是最基本的单元测试啥的是不惜的,尤其是现在中小 ...

  6. 使用阿里Docker镜像加速器加速

    在阿里开发者平台注册开发者账号 https://dev.aliyun.com/search.html 注册之后可以访问Docker镜像服务 https://cr.console.aliyun.com/ ...

  7. iOS崩溃日志记录工具--CrashlyTics

    http://try.crashlytics.com Crashlytics优势: 1.Crashlytics基本不会漏掉任何应用崩溃的信息 2.Crashlytics对崩溃日志管理很人性化,会根据崩 ...

  8. swift 代码添加image

    let image_ElectricianBtn = UIImage(named: "ElectricianBtn") let vimage_ElectricianBtn = UI ...

  9. diocp_tcp_client单元源码与注释

    (* * Unit owner: d10.天地弦 * blog: http://www.cnblogs.com/dksoft * homePage: www.diocp.org * * 2015-02 ...

  10. Rule of Modularity

    As Brian Kernighan once observed, “Controlling complexity is the essence of computer programming.” . ...