Cf 102 Div.2

來源:互聯網
上載者:User

A題

我是for迴圈暴力作的,題解裡最牛逼的做法是O(1):

O(1) solution - one may derive it from the previous approach: since x+c = d1 => 2*x + r1 - c1 = d1 => x = (d1+c1-r1)/2So, you find x, check that it is in [0..9], restore all other numbers as in the previous approach and check that all conditions are met.


B題

簡單類比,注意科學計數 12,345,678.9裡面逗號的位置,應該是 ( length - current_pos ) % 3 ==0處打逗號;


C題

給定一個數n表示(A - 1) × (B - 2) × (C - 2) ,要求A*B*C的最大值和最小值。n<=
10^9;

很值得說的一個題,對n<= 10^9,兩層for迴圈x*y*c作為n的3哥因子拆分,保證x<=y<=z,複雜度是 O(
1000*1000 );

注意一個情況就是枚舉的x,y,z後來變成(A - 1) × (B - 2) × (C - 2)形式應該是有三種情況。

/*Jan 13, 2012 4:15:42 PM yimao C-Help Farmer GNU C++ Accepted 50ms 1400KB*/#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define MM(a,b) memset(a,b,sizeof(a));typedef unsigned long long u64;typedef long long lld;#define maxnlld mmax,mmin,n;void up_max(lld &x,lld y){if(x==-1||x<y)x=y;}void up_min(lld &x,lld y){if(x==-1||x>y)x=y;}void work(lld a,lld b,lld c){    lld t= a*b*c-n;    up_max( mmax, t );    up_min( mmin, t );}int main(){    lld x,y,z;    while(cin>>n){        mmax=-1, mmin= -1;        for(x=1;x<1001;++x){            if(n%x==0){                lld k= n/x;                for(y=x;y*y<=k;++y){                    if( k%y==0 ){                        lld z= k/y;                        // n= x*y*z= (A-1)*(B-2)*(C-2);                        work( x+1, y+2, z+2 );                        work( y+1, x+2, z+2 );                        work( z+1, x+2, y+2 );                    }                    }                }            }            cout<<mmin<<" "<<mmax<<endl;    }    }


D題

這個題完全不會。

在n*m棋盤上放棋子,要求棋子之間的平方距離不得為5。

下面有個別人寫的題解:

提示說這個是騎士巡遊的二分圖。我感覺可以這麼想。把所有格子看做節點,所有的不能共存的點之間有一條邊。我們的目標就是刪掉最少的節點,使這個圖中沒有連通邊。這明顯就是騎士巡遊二分圖的特點,奇數步與偶數步是不相交的,所以很明顯,答案是(m*n+1)>>1對於小資料需要加特判


E題

n*m(1<=n,m<=9)的棋盤,要求放置T字型,每個T字型是佔5個格子,如下4種:

/*  ###    ..#    .#.    #..
    .#.    ###    .#.    ###
    .#.    ..#    ###    #..
*/

問最多可以放多少個,並輸出方案,並且不同的T之間用A,B,C...字母區別。


難題,太難不會,直接貼代碼。

/*Jan26,2012 1:52:19 PM yimao E-Help Caretaker GNU C++ Accepted 220ms 1400KB*/#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>#include <stack>using namespace std;#define MM(a,b) memset(a,b,sizeof(a));typedef unsigned long long u64;typedef long long lld;#define maxn 9/*  ###    ..#    .#.    #..    .#.    ###    .#.    ###    .#.    ..#    ###    #..*///choose a point(upper left),then make position of other points follow:const int shape[4][4][2]= {    { {0,1},{0,2},{1,1},{2,1} },    { {1,0},{2,0},{1,-1},{1,-2} },    { {1,0},{2,0},{2,-1},{2,1} },    { {1,0},{2,0},{1,1},{1,2} },};int m,n; //n row, m column;struct node{    char g[maxn][maxn];    int num,sx,sy;    node(){        sx=sy=0;        MM(g,0);        num=0;    }    void printNode(){        for(int i=0;i<n;++i){            for(int j=0;j<m;++j)                printf("%c",g[i][j]?g[i][j]:'.');                puts("");        }    }};stack<node>q;int ans;node ansNode;void bfs(){    while(1){        The_end_where_I_begin:        if( q.empty() ) break;        node now= q.top();        q.pop();        if( now.num>ans ){            ans= now.num;            ansNode= now;        }        now.num++;        char ch= 'A'+now.num-1;        bool flag= 1;        int i,j,k,l;        for(i=0;i<n;++i){            int in=0;            for(j=0;j<m;++j){                if( !now.g[i][j] ){                    flag= 1;                    for(k=3;k>=0;--k){                        flag= 1;                        for(l=0;l<4;++l){                            int tx= j+ shape[k][l][1];                            int ty= i+ shape[k][l][0];                            if( tx<0 || tx>=m || ty<0 ||ty>=n || now.g[ty][tx] ){                                flag=0;                                break;                            }                        }                        if( flag ){                            in++;                            node tNode= now;                            tNode.g[i][j]= ch;                            for(l=0;l<4;++l){                                int tx= j+ shape[k][l][1];                                int ty= i+ shape[k][l][0];                                tNode.g[ty][tx]= ch;                            }                            tNode.sx= j+1, tNode.sy= i;                            q.push( tNode );                        }                    }                    if( in>1 ) goto The_end_where_I_begin;                }            }        }    }}int main(){    //freopen("E.txt","r",stdin);    ans= -1;    while(cin>>n>>m){        while(!q.empty()) q.pop();        q.push( node() );        bfs();        printf("%d\n", ans);        ansNode.printNode();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.