codeforces 424D
题意:给定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的更多相关文章
- codeforces 424D Biathlon Track
codeforces 424D Biathlon Track 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define f ...
- CodeForces 424D: ...(二分)
题意:给出一个n*m的矩阵,内有一些数字.当你从一个方格走到另一个方格时,按这两个方格数字的大小,有(升,平,降)三种费用.你需要在矩阵中找到边长大于2的一个矩形,使得按这个矩形顺时针行走一圈的费用, ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- 新建jsp报错“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”
今天新建jsp文件时,就报错“Visual Page Editor has experimental support for Windows 64-bit”,然后刚好stackoverflow上面有这 ...
- Angularjs-Dirty Checking
Angularjs实现了数据双向绑定,就像下面这样: <!doctype html> <htnl ng-app> <head> <script src=&qu ...
- Kettle6使用
1.Kettle是一个开源的ETL(Extract-Transform-Load的缩写,即数据抽取.转换.装载的过程)项目,java编写,绿色无需安装 下载http://community.penta ...
- C++连接mysql的两种方式(ADO连接和mysql api连接)
一.ADO连接mysql 1.安装mysql-5.5.20-win32.msi和mysql-connector-odbc-5.3.4-win32.msi(一般两个安装程序要匹配,否则可能连接不上) ...
- uniDBGrid导入数据库(转红鱼儿)
有朋友问如何将excel导入数据库,这是我做的uniGUI项目中代码,实现uniDBGrid导入数据库的函数,因为用了kbmMW,所以你看到是将uniDBGrid导入kbmMWClientQuery, ...
- bat脚本命令循环运行程序 ,然后指定时间退出。
@echo offtitle EcCheck // 显示标题:loopif "%time%" GTR "23:00.00" (exit) else goto t ...
- PHPRPC servlet发布服务
1.服务端 web.xml PHPRPCDispacherServlet 2.客户端 controller层调用
- MS SQL Server之光标、存储过程和触发器
光标 通常数据库操作被认为是以数据集为基础的操作,但是光标被用于以记录为单位来进行操作,来获取数据库中的数据的子集.光标一般用于过程化程序里的嵌入的SQL语句. 对光标的定义如下: DECLARE C ...
- supervisor 配置
1. 生成配置文件$ echo_supervisord_conf > /etc/supervisord.conf 2.修改配置文件vi /etc/supervisord.conf找到[inclu ...
- Erlang 从入门到精通(一) 下载安装
我的电脑配置: 系统:win8.1 x64 内存:16G 在官网下载http://www.erlang.org/