结题报告-仙女教母的诅咒

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.

Examples

Input

2 4

Output

4

Input

3 4

Output

6

【思路】

题目题意:给我们一个n*m的矩阵,问我们最多可以放多少个士兵(必须满足规则:如果(x,y)处放了,那么以它走"日"字形的位置,将不能放士兵.)
题目分析:画过图之后可以分析出,对于n*m矩阵,那么它可以放的最多士兵和m的值有关。
当m=1时,最大士兵数=n;
当m=2时,按照如下摆放,俩行放士兵,俩行不放,依次下去。
当m>=3时  ,按照如下摆放,每一行是放一个士兵,空一格,再放一个士兵依次下去,下行必须与上一行刚好错开。
一开始就这样做了,结果wrong answer。

WA代码:

#include<iostream>
using namespace std;
int main()
{
    int n,m;
    while (cin>>n>>m){
        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;
            }
        }
cout<<ans<<endl;
return 0;
}

后来借鉴了大神的思路,发现自己少了一步,需要把n和m交换一次,求最大值。

AC代码:

#include<iostream>
using namespace std;
int main()
{
    int n,m;
    while (cin>>n>>m){
        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;
            }
        }
        cout<<max(res,ans)<<endl;
    }
    return 0;
}


这种规律题感觉不太好做,因为没有特定的算法, 如果看出来了是哪种算法,可以直接套用模板,这种题就不行,找到了就AC了,找不到是真没辙啊。。。。。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值