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. libcurl使用演示样例

    简要说明:C++使用libcurl訪问"www.baidu.com".获取返回码和打印出http文件 /* * @ libcurl使用演示样例 * @ 2014.04.29 * @ ...

  2. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  3. iozone文件系统测试 与EXCEL 制图

    http://www.iozone.org/[root@monitor bin]# ./iozone -a -s 512m -f /usr/test -y -q -Rb /root. -i -i Io ...

  4. hadoop处理Excel通话记录

    前面我们所写mr程序的输入都是文本文件,但真正工作中我们难免会碰到需要处理其它格式的情况,下面以处理excel数据为例 1.项目需求 有刘超与家庭成员之间的通话记录一份,存储在Excel文件中,如下面 ...

  5. camera理论基础和工作原理

    写在前面的话,本文是因为工作中需要编写摄像头程序,因为之前没有做过这类产品,所以网上搜索的资料,先整理如下,主要参考文章如下,如果有侵权,请联系我:另外,转载请注明出处.本文不一定全部正确,如果发现错 ...

  6. java取得整数部分 代码

    Pattern p1 = Pattern.compile("[0-9]*"); Matcher m1 = p1.matcher("100.0"); boolea ...

  7. jsp中Java代码中怎么获取jsp页面元素

    举例,页面元素<td><input   value="${sl }" type="text" id="sl" name=& ...

  8. Content Providers

    Content providers manage access to a structured set of data. They encapsulate the data, and provide ...

  9. TCP与UDP区别

    原文链接:http://blog.sina.com.cn/s/blog_493309600100clrw.html TCP与UDP区别 TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当 ...

  10. Android 4.0及以上版本接收开机广播BOOT_COMPLETED、开机自启动服务

    1.BootCompletedReceiver.Java文件 public class BootCompletedReceiver extends BroadcastReceiver { @Overr ...