BUPT 2015 newbie practice #2 div2-C - 想一想-HDU 4925 Apple Tree

來源:互聯網
上載者:User

標籤:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C

題意:給你n×m的格子,每個格子你可以選擇給1,或者使它上下左右(如果有)的數字乘2,你對每個格子操作的先後順序是自由的,求所有格子數字總和的最大值。

t組(小於100)資料,n和m(1到100)

題解:要使總和最大,那就每隔一個格子給1,使得每個給1的格子周圍都是乘2的格子,這樣它就乘了最多次2,比如3行4列

1 0 1 0

0 1 0 1

1 0 1 0

這裡0表示使周圍的乘2,我們的順序是先給1,再乘2,於是總和是4+8+16+8+4+8=48

法一。

類比這些格子,根據n和m,構造出上述的01二維數組,再對每個格子判斷周圍幾個0,然後乘幾次2,累加答案

代碼:

#include<cstdio>#include<cstring>int ma[105][105];int main(){    int n,m,t,k,ans,u,h;    int ma[105][105];    scanf("%d",&t);    while(t--)    {        memset(ma,0,sizeof(ma));        ans=0;        k=0;        scanf("%d%d",&n,&m);        for(int i=0; i<n; i++)        {            for(int j=0+k; j<m; j+=2)                ma[i][j]=1;//設定它為1            k=!k;        }        for(int i=0; i<n; i++)        {            for(int j=0; j<m; j++)            {                if(ma[i][j])                {                    h=1;                    u=0;                    if(i-1>=0)if(!ma[i-1][j])u++;//如果為0,代表乘2                    if(i+1<n)if(!ma[i+1][j])u++;                    if(j-1>=0)if(!ma[i][j-1])u++;                    if(j+1<m)if(!ma[i][j+1])u++;                    for(int l=1; l<=u; l++)h*=2;                    ans+=h;                }            }        }        printf("%d\n",ans);    }    return 0;}
法二。

如果行列數之和為奇數,則給1,並且使它周圍為乘2,則這個1就要乘幾次2了,根據是否在邊緣,判斷乘幾次2,累加答案

代碼:

//code from lyt#include<cstdio>using namespace std;int T;int n,m;long long ans=0;long long now=0;int main(){    scanf("%d",&T);    while(T)    {        scanf("%d%d",&n,&m);        ans=0;        for(int i=1; i<=n; i++)        {            for(int j=1; j<=m; j++)            {                if((i+j)&1)                {                    now=1;                    if(i>1)                        now<<=1;                    if(j>1)                        now<<=1;                    if(i<n)                        now<<=1;                    if(j<m)                        now<<=1;                    ans+=now;                }            }        }        printf("%lld\n",ans);        T--;    }    return 0;}
法三。

通過分析推出公式(x表示n,y表示m)

ans=1,當x=1,y=1;

ans=2*(y-1),當x=1,y>1;

ans=(x-1)*2,當x>1,y=1;

ans=(x-1)*8*(y-1),當x>1,y>1;

具體怎麼分析推出的,...不詳

代碼:

//code from zdh
#include<stdio.h>int T,x,y,s;int main(){ scanf("%d",&T); while(T--) { scanf("%d%d",&x,&y); if(x>1) { if(y==1) s=(x-1)*2; else s=(x-1)*8*(y-1); } else { if(y==1) s=1; else s=2*(y-1); } printf("%d\n",s); } return 0;}

  

BUPT 2015 newbie practice #2 div2-C - 想一想-HDU 4925 Apple Tree

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.