Bottles Arrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 746 Accepted Submission(s): 565
Now N days have passed and he is sobered. He is surprised to find that there are exactly N×M bottles around him. Another amazing fact is that there are N bottles with height 1 and N bottles with height 2 … N bottles with height M.
Now he is interested in playing with these bottles. He wants to arrange all these bottles in a rectangle with M rows and N columns which satisfied:
(1)In any column, there are no bottles with same height;
(2)In any row, the height difference between any two adjacent bottles is no more than 1.
He defined a strange function Y which equals the maximum value of the total height of any single row. He is addicted in arranging these rubbish bottles to find the minimal Y. You know that he cannot solve it with his pour IQ. You are his friend and can’t endure his decadence any more. So you decide to help him solve this problem and then bring him back to study.
The input will finish with the end of file.
3 3
3 5
8
11
For the first case the solution is:
1 2 3
2 1 1
3 3 2
题目大意:
有一个 m∗n 的矩阵,其中保证的 n 是一个奇数,在矩阵中需要满足如下两个条件:
1):每一列是
2):每一行保证相邻列之间数字差值为 1
显然,这样的矩阵有很多。
求:每个矩阵一行中所有数的和的最大值m,这众多的矩阵中,m最小的那个值。
即:
解题思路:
因为
n
是一个奇数,那么我们不妨设
根据题目描述的范围
3≤n<2∗m
,可以求得
l
的范围
因为每一列是
1−m
的组合,那么假设行数一定为第
i
行, 第
那么有如下两种情况:
1): 就是第
l
和 第
不妨设
ai,l+2≥m−1 , ai,l+3≥m−2, ... ,ai,2∗l+1≥m−l
将所有数字相加得到一个不等式(等差数列求和可得):
∑2∗l+1j=1ai,j≥(2∗l+1)∗m−l2
即为:
∑2∗l+1j=1ai,j≥n∗m−(n−12)2
2):第
l
和 第
设存在第
所以一定有
ai1,l+1=m−1
,因为
ai,l+1
已经有
m
了。
还有
ai1,l−1≥m−1 , ai1,l−2≥m−2, ... ,ai1,1≥m−l+1
ai1,l+3≥m−1 , ai1,l+4≥m−2, ... ,ai1,2∗l+1≥m−l+1
将所有数字相加得到一个不等式(等差数列求和可得):
∑2∗l+1j=1ai1,j≥(2∗l+1)∗m−l2
即为:
∑2∗l+1j=1ai1,j≥n∗m−(n−12)2
综上:
可得
ans=n∗m−(n−12)2
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-10;
int main()
{
int n, m;
while(~scanf("%d%d",&m,&n)){
printf("%d\n",m*n-(n*n-2*n+1)/4);
}
}