CodeForces 142B(Help General)

题目题意:

Once upon a time in the Kingdom of Far Far Away lived Sir Lancelot, the chief Royal General. He was very proud of his men and he liked to invite the King to come and watch drill exercises which demonstrated the fighting techniques and tactics of the squad he was in charge of. But time went by and one day Sir Lancelot had a major argument with the Fairy Godmother (there were rumors that the argument occurred after the general spoke badly of the Godmother's flying techniques. That seemed to hurt the Fairy Godmother very deeply).

As the result of the argument, the Godmother put a rather strange curse upon the general. It sounded all complicated and quite harmless: "If the squared distance between some two soldiers equals to 5, then those soldiers will conflict with each other!"

The drill exercises are held on a rectangular n × m field, split into nm square 1 × 1 segments for each soldier. Thus, the square of the distance between the soldiers that stand on squares (x1, y1) and (x2, y2) equals exactly (x1 - x2)2 + (y1 - y2)2. Now not all nm squad soldiers can participate in the drill exercises as it was before the Fairy Godmother's curse. Unless, of course, the general wants the soldiers to fight with each other or even worse... For example, if he puts a soldier in the square (2, 2), then he cannot put soldiers in the squares (1, 4)(3, 4)(4, 1) and (4, 3) — each of them will conflict with the soldier in the square (2, 2).

Your task is to help the general. You are given the size of the drill exercise field. You are asked to calculate the maximum number of soldiers that can be simultaneously positioned on this field, so that no two soldiers fall under the Fairy Godmother's curse.

Input

The single line contains space-separated integers n and m (1 ≤ n, m ≤ 1000) that represent the size of the drill exercise field.

Output

Print the desired maximum number of warriors.

Example

Input
2 4
Output
4
Input
3 4
Output
6

题目题意:给我们一个n*m的矩阵,问我们最多可以放多少个士兵(必须满足规则:如果(x,y)处放了,那么以它走"日"字形的位置,将不能放士兵.)

题目分析:画过图之后可以分析出,对于n*m矩阵,那么它可以放的最多士兵和m的值有关。

当m=1时,最大士兵数=n;

当m=2时,按照如下摆放,俩行放士兵,俩行不放,依次下去。

当m>=3时  ,按照如下摆放,每一行是放一个士兵,空一格,再放一个士兵依次下去,下行必须与上一行刚好错开。

然后我们把n和m交换再来一次,求最大值。

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main()
{
    int n,m;
    while (scanf("%d%d",&n,&m)!=EOF) {
        int ans,x=n,y=m;
        if (m==1) {
            ans=n;
        }
        else if (m==2) {
            int a=n;
            if (n%4==0) n=n/4;
            else n=n/4+1;
            a=a%4;
            if (a==1) ans=4*(n-1)+2;
            else ans=4*n;
        }
        else if (m==3) {
             int a=n;
             if (n%2==0) n=n/2;
             else n=n/2+1;
             a=a%2;
             if (a==0) ans=n*3;
             else ans=(n-1)*3+2;
        }
        else {
            if (n%2==0) ans=n*m/2;
            else {
                if (m%2==0) ans=(n-1)*m/2+m/2;
                else ans=(n-1)*m/2+m/2+1;
            }
        }
        int res=ans;
        swap(x,y);
        n=x;m=y;
        if (m==1) {
            ans=n;
        }
        else if (m==2) {
            int a=n;
            if (n%4==0) n=n/4;
            else n=n/4+1;
            a=a%4;
            if (a==1) ans=4*(n-1)+2;
            else  ans=4*n;
        }
        else if (m==3) {
             int a=n;
             if (n%2==0) n=n/2;
             else n=n/2+1;
             a=a%2;
             if (a==0) ans=n*3;
             else ans=(n-1)*3+2;
        }
        else {
            if (n%2==0) ans=n*m/2;
            else {
                if (m%2==0) ans=(n-1)*m/2+m/2;
                else ans=(n-1)*m/2+m/2+1;
            }
        }
        printf("%d\n",max(res,ans));
    }
    return 0;
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值