hdu4572_Bottles Arrangement_(思维+找规律)

本文介绍了一个有趣的矩阵排列问题,旨在寻找满足特定条件的最小最大行值。问题要求矩阵的每列元素不重复,且每行相邻元素之差不超过1。通过分析给出的示例,文章展示了如何构造符合条件的矩阵并计算目标值。

Problem Description
  Hunan cuisine is really wonderful! But if you don’t like spicy food, you will feel terrible since it can be hard for you to find any food without hot pepper here. Big Fan is a student from the north who was not fit to the spicy food in Changsha. He became thinner and thinner because eating little food and maintained his life mostly by drinking water. One day, he found that the wine in Hunan is pretty good, such as Jiugui, Liuyang River, Shaoyang Daqu and so on. He got addicted to it and became an alcoholic, leading a depressed life.
  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.
 

Input
  There are several test cases. For each case, the input contains one line with two integers M and N (1< M <= 10000, 3 <= N < 2×M, It is guaranteed that N is always odd).
  The input will finish with the end of file.
 

Output
  For each test case, print the minimal Y in single line.
 

Sample Input
3 3 3 5
 

Sample Output
8 11
Hint
For the first case the solution is: 1 2 3 2 1 1 3 3 2

题意:

给你n个1,n个2,n个3,... ...n个m,让你组成一个矩阵,要满足:

1.在同一行里,相邻的两个数的差值不能超过1;

2.在同一列中,不能有相同的数字;

现在需要你找出所有情况的矩阵中最大行的最小值。

解:

(为了方便叙述以 3 5 进行分析)

最糟糕的情况是:

1 1 1 1 1

2 2 2 2 2

3 3 3 3 3

那么这样的最大值为15,这显然不是我们需要的矩阵,那么我们需要替换最大行的3。

那么我们来考虑下这个,一个3,需要符合行的条件的话,放在中间位置需要两个2或者3(像这样:2 3 2    3 3 3)

现在我们需要尽可能小,肯定是选 2 3 2 ,但如果每一行都将3放在中间位置,2是不够用的,那么只能将3放在行的两边,这样一个3能对应一个2,同理,2也将进行排列2 1  2.

那么组成这样3  2  _  2  3,为了能让行小就变成3  2  1  2  3。

自己按规定组织下,得出矩阵:

3 3 2 1 1 

1 2 3 3 2
2 1 1 2 3

最大行为3 3 2 2 1.

再看3 3的情况:

最糟糕的是3 3 3,替换的话是3 2 3,如果是:3 2 1的话,按照列的规则应该完善的矩阵是:

3  2  1,

 _  3  _              

_ _ 3

按照行的规则完善:

3 2 1,

 2 3 2

 _ 2 3

显然不符合条件(3 2 2)同理。

那么我们可以得出最大行道的排列应该是等差的一个数列,m , m,m-1,m-1,m-2,m-2.....m-(n+1)/2.

求这个数列的和就行了。

代码:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main()
{
    int n,m;
    while(~scanf("%d%d",&m,&n))
    {
        int sum=0;
        int temp=m-n/2;
        int k=(n+1)/2;
        while(k--)
        {
            if(m!=temp)
            sum+=m;
            sum+=m;
            m--;
        }
        printf("%d\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值