4356: 情人节的阻击(两种情况)

题目链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4356

4356: 情人节的阻击


Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 447 Solved: 27
Description
情人节又快到了,每到这个时候总是 FFF 团最忙碌的时候,他们为了让社会更加和谐,致力于降低社会的狗粮度。
在一个 n*m 影院中,有 x 个男生和 y 个女生,保证 x + y = n * m, 如果有男女相邻或者前后而坐,狗粮度就会增加1,
现在他们已经黑入了影院的订票系统,想请你重新设置每个人的位置,使得影院的狗粮度最低。
Input
多组测试。每组输入形如:
n m x y

1<=n, m<=200
Output
每组输出形如:
ans

Sample Input

2 2 1 3
4 4 3 13

Sample Output

2
4

思路:这一题有两种思路。
1.按照正方形来安排,这样接触的面积(即狗粮)是最少的。
2.按照最短的边来排,这样保证接触面积最少。

当(2*正方形的边长)小于最短的边长时,按照第一种计算。
否则,按照第二种计算。

给个样例:100 100 9 9991

在一角放3*3的这样狗粮是6。
如果是第二种情况,狗粮是10。

代码:

#include
#include
#include
#include
#include
using namespace std;
int main()
{
    int n,m,x,y;
    while(~scanf("%d%d%d%d",&n,&m,&x,&y))
    {
        if(x<y)//x是人数最多的
        {
            int t=x;
            x=y;
            y=t;
        }
        if(n>m)//n是最短的边
        {
            int t=n;
            n=m;
            m=t;
        }
        int r=sqrt(y*1.0);//正方形边长
        if(r*2if(y-r*r>r)
                printf("%d\n",r*2+2);
            else if(y==r*r)
                printf("%d\n",r*2);
            else printf("%d\n",r*2+1);
            continue;
        }
        //第二种情况
        int p=x%n;
        if(p)
        {
            if(y/n)
                printf("%d\n",n+1);
            else printf("%d\n",y%n+1);
        }
        else
        {
            if(y)
                printf("%d\n",n);
            else printf("0\n");
        }
    }
    return 0;
}

你可能感兴趣的:(ACM,算法,题目)