[洛谷P3444] [POI2006]ORK-Ploughing
洛谷题目链接[POI2006]ORK-Ploughing
题目描述
Byteasar, the farmer, wants to plough his rectangular field. He can begin with ploughing a slice from any of the field's edges, then he can plough a slice from any unploughed field's edges, and so on, until the whole field is ploughed. After the ploughing of every successive slice, the yet-unploughed field has a rectangular shape. Each slice has a span of 111 , and the length and width of the field are the integers nnn and mmm .
Unfortunately, Byteasar has only one puny and frail nag (horse) at his disposal for the ploughing. Once the nag starts to plough a slice, it won't stop until the slice is completely ploughed. However, if the slice is to much for the nag to bear, it will die of exhaustion, so Byteasar has to be careful. After every ploughed slice, the nag can rest and gather strength. The difficulty of certain parts of the field varies, but Byteasar is a good farmer and knows his field well, hence he knows every part's ploughing-difficulty.
Let us divide the field into m×nm\times nm×n unitary squares - these are called tiles in short.
We identify them by their coordinates (i,j)(i,j)(i,j) , for 1≤i≤m1\le i\le m1≤i≤m and 1≤j≤n1\le j\le n1≤j≤n .
Each tile has its ploughing-difficulty - a non-negative integer.
Let ti,jt_{i,j}ti,j denote the difficulty of the tile which coordinates are (i,j)(i,j)(i,j) .
For every slice, the sum of ploughing-difficulties of the tiles forming it up cannot exceed a certain constant kkk - lest the nag dies.
A difficult task awaits Byteasar: before ploughing each subsequent slice he has to decide which edge of the field he'll plough, so that the nag won't die. On the other hand, he'd like to plough as few slices as possible.
TaskWrite a programme that:
reads the numbers kkk , mmm and nnn from the input file, as well as the ploughing-difficulty coefficients, determines the best way to plough Byteasar's field, writes the result to the output file.
Byteasar想耕种他那块矩形的田,他每次能耕种矩形的一边(上下左右都行),在他每次耕完后,剩下的田也一定是矩形,每块小区域边长为 111 ,耕地的长宽分别为 mmm 和 nnn ,不幸的是Byteasar只有一匹老弱的马,从马开始耕地开始,只有当它耕完了一边才会停下休息。但有些地会非常难耕以至于马会非常的累,因此Byteasar需要特别小心。当耕完了一边之后,马可以停下来休息恢复体力。每块地耕种的难度不一,但是Byteasar都非常清楚。我们将地分成 m×nm\times nm×n 块单位矩形——我们用坐标 (i,j)(i,j)(i,j) 来定义它们。每块地都有一个整数 ti,jt_{i,j}ti,j ,来定义 (i,j)(i,j)(i,j) 的耕种难度。所以每次马耕一边地时的难度就是所有它耕种的地的难度总和,对于这匹虚弱的马而言,这个值不能超过他的体力值。Byteasar想知道在马不死掉的情况下最少需要耕多少次才能把地耕完。
输入输出格式
输入格式:
There are three positive integers in the first line of the input file: kkk , mmm and nnn ,separated by single spaces, 1≤k≤200 000 0001\le k\le 200\ 000\ 0001≤k≤200 000 000 , 1≤m,n≤20001\le m,n\le 20001≤m,n≤2000 .
In the following nnn lines there are the ploughing-difficulty coefficients.
The line no. j+1j+1j+1 contains the coefficients t1,j,t2,j...,tn,mt_{1,j},t_{2,j}...,t_{n,m}t1,j,t2,j...,tn,m , separated by single spaces, 0≤ti,j≤100 0000\le t_{i,j}\le 100\ 0000≤ti,j≤100 000 .
输出格式:
Your programme should write one integer to the output file: the minimum number of slices required to plough the field while satisfying the given conditions. Since we care for animals, we guarantee that the field can be ploughed according to the above rules. But remember, saving the nag is up to you!
输入输出样例
输入样例#1:
12 6 4
6 0 4 8 0 5
0 4 5 4 6 0
0 5 6 5 6 0
5 4 0 0 5 4
输出样例#1:
8
说明
感谢@NaVi_Awson 提供翻译
sto Navi_Awson orz
一句话题意: 一个矩形中每个位置有一个值,你的马也有一个体力值,每次可以选择当前矩形中的最上下左右四个方向耕地,前提是这一条的权值和小于等于马的体力值.问最少耕地次数.
题解: 首先假设我们不考虑体力的限制,那么最少耕地的次数就是\(min(n, m)\).也就是说,有一个贪心策略是一直按照横着取,直到取不了再按照横着取.
这个贪心策略是正确的,因为最优策略(在不考虑体力值的情况下)是只横着耕地或是只竖着耕地,而一直横着耕地直到不能横着耕地再按照竖着耕地的方法,是最接近最优策略的,所以这个贪心是没有问题的.
但是我们需要考虑这样一个问题:假设我们一直按照横着取,横着取不了了,现在要取竖的,那么我怎么知道要取左边一条还是右边的一条呢?显然这个是会影响到答案的,所以这个不能忽略掉.为了避免这样的判断导致答案不是最优,我们通过枚举竖着取的条数,每次枚举左边选取的条数不超过我枚举的值,这样就可以通过枚举-贪心的方法枚举出所有的情况计算,并通过贪心策略取得最优值.复杂度\(O(n^2)\)
#include<bits/stdc++.h>
using namespace std;
const int N=2000+5;
const int inf=2147483647;
int k, n, m, a[N][N], l[N][N], c[N][N], ans = inf;
inline int gi(){
int ans = 0, f = 1; char i = getchar();
while(i>'9' || i<'0'){ if(i == '-') f = -1; i = getchar(); }
while(i>='0' && i<='9') ans = ans*10+i-'0', i = getchar();
return ans * f;
}
inline int column_ans(int lim){
register int res = 0, lc = 1, rc = m, ul = 1, dl = n;
while(lc <= rc && ul <= dl){
res++;
if(c[dl][lc]-c[ul-1][lc] <= k){ lc++; continue; } // left -> right
if(c[dl][rc]-c[ul-1][rc] <= k){ rc--; continue; } // right -> left
if(l[ul][rc]-l[ul][lc-1] <= k && ul < lim){ ul++; continue; } // up -> down
if(l[dl][rc]-l[dl][lc-1] <= k){ dl--; continue; } // down -> up
return inf;
}
return res;
}
inline int line_ans(int lim){
register int res = 0, lc = 1, rc = m, ul = 1, dl = n;
while(lc <= rc && ul <= dl){
res++;
if(l[ul][rc]-l[ul][lc-1] <= k){ ul++; continue; } // up -> down
if(l[dl][rc]-l[dl][lc-1] <= k){ dl--; continue; } // down -> up
if(c[dl][lc]-c[ul-1][lc] <= k && lc < lim){ lc++; continue; } // left -> right
if(c[dl][rc]-c[ul-1][rc] <= k){ rc--; continue; } // right -> left
return inf;
}
return res;
}
int main(){
//freopen("ork.in", "r", stdin);
//freopen("ork.out", "w", stdout);
k = gi(), m = gi(), n = gi();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) a[i][j] = gi();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) l[i][j] = l[i][j-1]+a[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) c[i][j] = c[i-1][j]+a[i][j];
for(int i=1;i<=n;i++) ans = min(ans, line_ans(i));
for(int i=1;i<=m;i++) ans = min(ans, column_ans(i));
cout << ans << endl;
return 0;
}
[洛谷P3444] [POI2006]ORK-Ploughing的更多相关文章
- 洛谷P3444 [POI2006]ORK-Ploughing [枚举,贪心]
题目传送门 ork 格式难调,题面就不放了. 分析: 一道偏难的贪心和枚举题.考试的时候是弃疗了...yyb巨佬已经讲的很详细了,推荐他的博客.这里小蒟蒻就只放代码了. Code: #include& ...
- 洛谷P3434 [POI2006]KRA-The Disks(线段树)
洛谷题目传送门 \(O(n)\)的正解算法对我这个小蒟蒻真的还有点思维难度.洛谷题解里都讲得很好. 考试的时候一看到300000就直接去想各种带log的做法了,反正不怕T...... 我永远只会有最直 ...
- 洛谷P3435 [POI2006]OKR-Period of Words [KMP]
洛谷传送门,BZOJ传送门 OKR-Period of Words Description 一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅当存在串B, ...
- 洛谷P3434 [POI2006]KRA-The Disks [模拟]
题目传送门 KRA 题目描述 For his birthday present little Johnny has received from his parents a new plaything ...
- 【题解】洛谷P3435 [POI2006] OKR-Periods of Words(KMP)
洛谷P3435:https://www.luogu.org/problemnew/show/P3435 思路 来自Kamijoulndex大佬的解释 先把题面转成人话: 对于给定串的每个前缀i,求最长 ...
- 洛谷 P3437 [POI2006]TET-Tetris 3D 解题报告
P3437 [POI2006]TET-Tetris 3D 题目描述 The authors of the game "Tetris" have decided to make a ...
- 洛谷P3434 [POI2006]KRA-The Disks
P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...
- 洛谷 P3434 [POI2006]KRA-The Disks
P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...
- 洛谷 P3434 [POI2006]KRA-The Disks 贪心
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输出样例 输出样例 说明 思路 AC代码 题面 题目链接 P3434 [POI2006]KRA-The Disks 题目 ...
随机推荐
- Python高级编程-itertoos模块
Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先我们看看itertools模块提供的几个“无限”迭代器, import itertools naturals = ...
- 【转】C++后台开发之我见
工作也快两年了,偶然看到自己以前写过的一些技术博客,发现自己自毕业后一直没有更新过自己的技术博客,趁现在是刚过完春节快要回公司工作之际,谈谈我个人对后台开发的一些个人见解,希望能够对在校的学生或者刚刚 ...
- Windows平台下安装.net coreclr
.net coreclr 已经发布RC1版本,安装方法如下: 1.安装DNVM,DNVM是.net运行时管理器,负责管理所有版本的.net运行时(.net framework..net coreclr ...
- iOS开发实现UIView随着子控件的高度的变化而变化
例子 其实看完上面的叙述,你会思考,到底什么情况下,一个UIView需要只设置坐标不设置大小呢?其实这种场景相当普遍.比如,我们常常会碰到,一个View中有两个Label,两个Label的高度均和内容 ...
- chrome extension demos
chrome extension demos demo https://github.com/hartleybrody/buzzkill/blob/master/bootstrap.js https: ...
- WPF数据视图学习
当你绑定集合到ItemsControl,数据视图被安静地在幕后创造.视图位于数据源和绑定控件之间.数据视图是通往数据源的一个窗口.它跟踪当前项目,它支持诸如排序,过滤,和分组特征.这些特征独立于数据对 ...
- 使用WebClient类对网页下载源码,对文件下载保存及异步下载并报告下载进度
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAx4AAAI7CAIAAADtTtpYAAAgAElEQVR4nO3dX6xlV33Y8f3UJFUqHq
- 【bzoj1212】[HNOI2004]L语言 AC自动机
题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...
- Python 文件对象和方法
Python文件对象和方法 1.打开和关闭文件 Python提供了必要的函数和方法进行默认情况下的文件基本操作,我们可以用file对象做大部分文件操作. open()方法 我们必须先用Python内置 ...
- Luogu1041 NOIP2003传染病控制(搜索)
暴搜加个最优性剪枝即可.一直觉得正式比赛出这种不能一眼看出来暴搜就行了的搜索题的出题人都是毒瘤. #include<iostream> #include<cstdio> #in ...