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. POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)

    http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...

  2. DELPHI TreeView 文件目录树和 设置节点图标 完整

        DELPHI TreeView 文件目录树和 设置节点图标   下载地址 http://download.csdn.net/detail/teststudio/6448293     需要制作 ...

  3. 再回首,Java温故知新(九):Java基础之流程控制语句

    流程控制语句分为条件语句.循环语句和中断语句. 中断语句包括break和continue,两者的区别在于break会跳出整个循环,而continue则是跳出此次循环,之后还会继续下一次循环. 条件语句 ...

  4. 各大浏览器CSS Hack收集

    各大浏览器CSS Hack收集  >>>>>>>>>>>>>>>>>>>>> ...

  5. Java-Hirbernate小结大纲

    Hibernate Hibernate是一个开放源代码的对象关系映射框架 Hibernate的核心接口一共有6个,分别为:Session.SessionFactory.Transaction.Quer ...

  6. 实现Android K的伪沉浸式

    在Android 5.0之后引入了MD风格,从而状态栏沉浸也成为了一种设计习惯.而停留在之Android L之前的Android系统则不能直接实现沉浸式,这里就介绍一下如何实现Android K系列的 ...

  7. 数据库连接报错之IO异常(The Network Adapter could not establish the connection)

    Io 异常: The Network Adapter could not establish the connection 有以下四个原因: 1.oracle配置 listener.ora 和tnsn ...

  8. NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化

    转载▼     在iOS  5 中,苹果引入了一个解析JSON串的NSJSONSerialization类. 通过该类,我们可以完成JSON数据与NSDictionary和NSArray之间的转化. ...

  9. Codeforces 543D Road Improvement(DP)

    题目链接 Solution 比较明显的树形DP模型. 首先可以先用一次DFS求出以1为根时,sum[i](以i为子树的根时,满足要求的子树的个数). 考虑将根从i变换到它的儿子j时,sum[i]产生的 ...

  10. functools学习有感

    functools的内容不多,包含四个函数(partial,reduce,update_wrapper,wraps)和一个python对象(partial Objects). functools的四个 ...