Codeforces Round #242 (Div. 2) D:http://codeforces.com/contest/424/problem/D

题意:给你一个n*m的矩阵,每个格子上面有个数字,在相邻格子之间会有一定的费用,费用根据相邻格子的大小关系确定。让你费用最接近一个数的矩阵。

题解:一看题目,想了一下,预处理,然后猜到要用二分。但是二维的怎么二分,一直没有想到什么办法,一直想的是(n*mlog(n*m))的算法,所以不对了。其实应该固定一个起点,作为矩阵的左上角的点,然后枚举右下角的纵坐标,对于右下角的横坐标就可以二分寻找了。但是这里是找最接近的数,一开始以为二分找不到,现在才知道二分的最后可以找到的就是最接近的那个数,同时二分的写法也要注意,不能写成a[mid]<=number,这样会陷入死循环。还有,这一题要预处理出一些点的距离。其实,这一题还可以直接打暴力,四层for也是能过的。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int a[N][N];
int l[N][N],r[N][N],u[N][N],d[N][N];
int n,m,t;
int tp,tu,td;
void solve(){
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(u,,sizeof(u));
memset(d,,sizeof(d));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
l[i][j]=l[i][j-];
if(j==)continue;
if(a[i][j]>a[i][j-])
l[i][j]+=tu;
else if(a[i][j]==a[i][j-])
l[i][j]+=tp;
else
l[i][j]+=td;
}
}
for(int i=;i<=n;i++){
for(int j=m;j>=;j--){
r[i][j]=r[i][j+];
if(j==m)continue;
if(a[i][j]>a[i][j+])
r[i][j]+=tu;
else if(a[i][j]==a[i][j+])
r[i][j]+=tp;
else
r[i][j]+=td;
}
}
for(int j=;j<=m;j++){
for(int i=;i<=n;i++){
u[i][j]=u[i-][j];
if(i==)continue;
if(a[i][j]>a[i-][j])
u[i][j]+=tu;
else if(a[i][j]==a[i-][j])
u[i][j]+=tp;
else
u[i][j]+=td;
}
}
for(int j=;j<=m;j++){
for(int i=n;i>=;i--){
d[i][j]=d[i+][j];
if(i==n)continue;
if(a[i][j]>a[i+][j])
d[i][j]+=tu;
else if(a[i][j]==a[i+][j])
d[i][j]+=tp;
else
d[i][j]+=td;
}
}
}
int getsum(int l1,int r1,int l2,int r2){
return l[l1][r2]-l[l1][r1]+u[l2][r2]-u[l1][r2]+r[l2][r1]-r[l2][r2]+d[l1][r1]-d[l2][r1];
}
int ans,x1,x2,y1,y2;
int main(){
scanf("%d%d%d",&n,&m,&t);
scanf("%d%d%d",&tp,&tu,&td);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>a[i][j];
solve();
ans=2e9;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
for(int k=j+;k<=m;k++){
int l=i+,r=n;
if(l>r)continue;
while(l<r){
int mid=(l+r)/;
int temp=getsum(i,j,mid,k);
if(temp>=t){
r=mid;
}
else
l=mid+;
}
int ts=getsum(i,j,l,k);
if(abs(ts-t)<abs(ans-t)){
ans=ts;
x1=i;y1=j;
x2=l;y2=k;
}
if(l<n){
ts=getsum(i,j,l+,k);
if(abs(ts-t)<abs(ans-t)){
ans=ts;
x1=i;y1=j;
x2=l+;y2=k;
}
}
if(l>i+){
ts=getsum(i,j,l-,k);
if(abs(ts-t)<abs(ans-t)){
ans=ts;
x1=i;y1=j;
x2=l-;y2=k;
}
}
}
}
}
printf("%d %d %d %d\n",x1,y1,x2,y2); }

Biathlon Track的更多相关文章

  1. codeforces 424D Biathlon Track

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

  2. Codeforces Round #242 (Div. 2) &lt;A-D&gt;

    CF424 A. Squats 题目意思: 有n(n为偶数)个x和X,求最少的变换次数,使得X的个数为n/2,输出变换后的序列. 解题思路: 统计X的个数ans,和n/2比較,少了的话,须要把n/2- ...

  3. How to accept Track changes in Microsoft Word 2010?

    "Track changes" is wonderful and remarkable tool of Microsoft Word 2010. The feature allow ...

  4. IEEE/ACM ASONAM 2014 Industry Track Call for Papers

    IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM) 2014 In ...

  5. 使用angular中ng-repeat , track by的用处

    我们见到最简单的例子是: <div ng-repeat="link in links" ></div> 如果item的值有重复的,比如links=[&quo ...

  6. 解密FFmpeg播放track mode控制

    上一篇文章(http://www.cnblogs.com/yangdanny/p/4421130.html)我们解决了在FFmpeg下如何处理H264和AAC的扩展数据,根据解出的NALU长度恢复了H ...

  7. [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys

    错误原因在于出现相同内容. 原写为: <li ng-repeat="log in logs" scroll-down> {{log}}</li> 改写为: ...

  8. track by

    ng-repeat指令中使用track by子语句解决重复数据遍历的错误 <li ng-repat="x in [2, 2]" ng-bind="x"&g ...

  9. [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq

    angularjs 使用ng-repeat报错 <div ng-init="words = ['高校','高校','高校']" ng-repeat="word in ...

随机推荐

  1. 【MongoDB】在windows平台下mongodb的分片集群(五)

    本篇接着上面的四篇继续讲述在window平台下mongodb的分片集群搭建. 在分片集群中也照样能够创建索引,创建索引的方式与在单独数据库中创建索引的方式一样.因此这不再多说.本篇主要聚焦在分片键的选 ...

  2. oracle重建、更新索引、索引统计信息命令

    在oracle中查找所有的表的索引的命令 select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_na ...

  3. 读取xml字符串

    string strXml = @"<MessageData><pm_id>10</pm_id><pm_title>这是公司或者产品的名称&l ...

  4. php100视频原始地址列表整理:

    php100视频原始地址列表整理: 教程名称 . 1:环境配置与代码调试 2:PHP的数据类型与源码调试 3:常用PHP运算类型介绍与应用 4: PHP条件语句介绍与应用 5:PHP循环语句的介绍与应 ...

  5. 如何设置MySQL Workbench EER Diagram 尺寸?

    ER Diagram -> Model -> Diagram Properties and Size...

  6. 用java写bp神经网络(四)

    接上篇. 在(一)和(二)中,程序的体系是Net,Propagation,Trainer,Learner,DataProvider.这篇重构这个体系. Net 首先是Net,在上篇重新定义了激活函数和 ...

  7. 介绍TableView非常不错的一篇文章

    原文:http://blog.csdn.net/fanxiaochuan/article/details/11332775 介绍TableView非常不错的一篇文章: http://www.cocoa ...

  8. C#入门经典(第五版)学习笔记(一)

    ---------------变量和表达式---------------赋值运算符:+=:-=:*=:/=:%=例如:i+=j 相当于 i=i+j i-=j 相当于 i=i-j以此类推 按位运算符:& ...

  9. 主成份分析PCA

    Data Mining 主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有6 ...

  10. AbstractFactory 模式

    ///////////////////////Product.h////////////// #ifndef _PRODUCT_H_ #define _PRODUCT_H_ class Abstrac ...