Brownie Slicing 【二分】

链接:https://ac.nowcoder.com/acm/contest/3886/B
来源:牛客网

题目描述

Bessie has baked a rectangular brownie that can be thought of as an RxC grid (1 <= R <= 500; 1 <= C <= 500) of little brownie squares. The square at row i, column j contains NijN_{ij}Nij (0 <= NijN_{ij}Nij <= 4,000) chocolate chips.
Bessie wants to partition the brownie up into A*B chunks (1 <= A <= R; 1 <= B <= C): one for each of the A*B cows. The brownie is cut by first making A-1 horizontal cuts (always along integer
coordinates) to divide the brownie into A strips. Then cut each strip *independently* with B-1 vertical cuts, also on integer
boundaries. The other A*B-1 cows then each choose a brownie piece, leaving the last chunk for Bessie. Being greedy, they leave Bessie the brownie that has the least number of chocolate chips on it.
Determine the maximum number of chocolate chips Bessie can receive, assuming she cuts the brownies optimally.
As an example, consider a 5 row x 4 column brownie with chips
distributed like this:
         1 2 2 1
         3 1 1 1
         2 0 1 3
         1 1 1 1
         1 1 1 1
Bessie must partition the brownie into 4 horizontal strips, each with two pieces. Bessie can cut the brownie like this:
       1 2 | 2 1
       ---------
       3 | 1 1 1
       ---------
       2 0 1 | 3
       ---------
       1 1 | 1 1
       1 1 | 1 1
Thus, when the other greedy cows take their brownie piece, Bessie still gets 3 chocolate chips.

输入描述:

* Line 1: Four space-separated integers: R, C, A, and B
* Lines 2..R+1: Line i+1 contains C space-separated integers: Ni1,...,NiCN_{i1}, ..., N_{iC}Ni1,...,NiC

输出描述:

* Line 1: A single integer: the maximum number of chocolate chips that Bessie guarantee on her brownie
示例1

输入

复制
5 4 4 2 
1 2 2 1 
3 1 1 1 
2 0 1 3 
1 1 1 1 
1 1 1 1 

输出

复制
3

思路
  求一个最小值最大,很容易想到二分,枚举切行切列的情况 check
CODE
  1 #include <bits/stdc++.h>
  2 #define dbg(x) cout << #x << "=" << x << endl
  3 #define eps 1e-8
  4 #define pi acos(-1.0)
  5 
  6 using namespace std;
  7 typedef long long LL;
  8 
  9 template<class T>inline void read(T &res)
 10 {
 11     char c;T flag=1;
 12     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
 13     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
 14 }
 15 
 16 namespace _buff {
 17     const size_t BUFF = 1 << 19;
 18     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
 19     char getc() {
 20         if (ib == ie) {
 21             ib = ibuf;
 22             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
 23         }
 24         return ib == ie ? -1 : *ib++;
 25     }
 26 }
 27 
 28 int qread() {
 29     using namespace _buff;
 30     int ret = 0;
 31     bool pos = true;
 32     char c = getc();
 33     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
 34         assert(~c);
 35     }
 36     if (c == '-') {
 37         pos = false;
 38         c = getc();
 39     }
 40     for (; c >= '0' && c <= '9'; c = getc()) {
 41         ret = (ret << 3) + (ret << 1) + (c ^ 48);
 42     }
 43     return pos ? ret : -ret;
 44 }
 45 
 46 const int maxn = 507;
 47 
 48 int R,C,A,B;
 49 int a[maxn][maxn];
 50 int sum[maxn][maxn];
 51 
 52 bool check(int x) {
 53     int line_last, row_last;
 54     int line_have, row_have;
 55     line_have = 0, line_last = 0;
 56     
 57     for ( int i = 1; i <= R; ++i ) {
 58         row_have = 0, row_last = 0;
 59         for ( int j = 1; j <= C; ++j ) {
 60             //dbg(i), dbg(j);
 61             //printf("n1:%d n2:%d\n",sum[i][j] - sum[i][row_last] - sum[line_last][j] + sum[line_last][row_last],x);
 62             if(sum[i][j] - sum[i][row_last] - sum[line_last][j] + sum[line_last][row_last] >= x) {
 63                 row_have++;
 64                 row_last = j;
 65             }
 66         }
 67         if(row_have >= B) {
 68             line_last = i;
 69             line_have++;
 70         }
 71     }
 72     dbg(line_have);
 73     return line_have >= A;
 74 }
 75 
 76 int main()
 77 {
 78     int l = 1, r = 0, mid = 0;
 79     scanf("%d %d %d %d",&R, &C, &A, &B);
 80     for ( int i = 1; i <= R; ++i ) {
 81         for ( int j = 1; j <= C; ++j ) {
 82             scanf("%d",&a[i][j]);
 83         }
 84     }
 85     for ( int i = 1; i <= R; ++i ) {
 86         for ( int j = 1; j <= C; ++j ) {
 87             sum[i][j] = sum[i - 1][j] + sum[i][j-1] - sum[i - 1][j - 1] + a[i][j];
 88         }
 89     }
 90     
 91     l = 1;
 92     r = sum[R][C];
 93     while(l + 1 <  r) {
 94         mid = (l + r) >> 1;
 95         //printf("l:%d r:%d\n",l,r);
 96         //dbg(mid);
 97         if(check(mid)) {
 98             l = mid ;
 99         }
100         else {
101             r = mid ;
102         }
103     }
104     cout << l << endl;
105     return 0;
106 }
View Code

 

全部评论

相关推荐

来,说点可能被同行“骂”的大实话。🙊当初接数字马力Offer时,朋友都说:“蚂蚁的“内包”公司?你想清楚啊!”但入职快一年后的今天,我反而对他有了不一样的看法!🔹&nbsp;是偏见?还是信息差!之前没入职之前外面都在说什么岗位低人一等这类。实际上:这种情况不可至否,不能保证每个团队都是其乐融融。但我在的部门以及我了解的周边同事都还是十分好相处的~和蚂蚁师兄师姐之间也经常开一些小玩笑。总之:身份是蚂蚁公司给的,地位是自己挣的(一个傲娇女孩的自述)。🔹&nbsp;待遇?玩的就是真实!试用期工资全额发!六点下班跑得快(早9晚6或者早10晚7,动态打卡),公积金顶格交。别听那些画饼的,到手的钱和下班的时间才是真的(都是牛马何必难为牛马)。🔹&nbsp;能不能学到技术?来了就“后悔”!我们拥有权限直通蚂蚁知识库,技术栈多到学不完。说“学不到东西”的人,来了可能后悔——后悔来晚了(哈哈哈哈,可以不学但是不能没有)!💥&nbsp;内推地址:https://app.mokahr.com/su/ueoyhg❗我的内推码:NTA6Nvs走我的内推,可以直达业务部门,面试流程更快速,进度可查!今天新放HC,之前挂过也能再战!秋招已经正式开始啦~机会就摆在这,敢不敢来试一试呢?(和我一样,做个勇敢的女孩)
下午吃泡馍:数字马力的薪资一般哇,5年经验的java/测试就给人一万出头,而且刚入职第三天就让人出差,而且是出半年
帮你内推|数字马力 校招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务