Cf 97 Div.2

A题

数组映射,读入t, 则p[ t ]= i; for( i, 1, n ) cout<< p[i];


B题

十进制,三进制的转换,+字符串处理;

处理进制转换的时候,要求反串,额,下面是代码。

/*Feb 20, 2012 11:35:13 AM	yimao  B-Ternary Logic GNU C++	Accepted 30ms 1400KB*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a));
typedef long long lld;
typedef unsigned long long u64;
#define maxn 50

void up_min(int &x,int y){if((x>y)||(x==-1))x=y;}

int tran(int x,char *ch){
    int t=0;
    while(x){
        ch[t++]= x%3+'0';
        x/= 3;
    }
    ch[t]= 0;
    return t;
}
lld trans2(char *ch, int n){
    lld i, t=1, ret= 0;
    for(i=n-1;i>=0;--i){
        ret+= t* (ch[i]-'0');
        t*= 3;
    }
    return ret;
}
int main()
{
    //freopen("B.txt","r",stdin);
    int a,b,i,j;
    char ca[maxn], cb[maxn];
    while(cin>>a>>b){
        int na= tran( a, ca );
        int nb= tran( b, cb );
        if( na<nb ){
            for(i=na;i<nb;++i)
                ca[i]='0';
            ca[nb]= 0;
            na= nb;
        }
        else{
            for(i=nb;i<na;++i)
                cb[i]= '0';
            cb[na]= 0;
            nb= na;
        }
        for(i=0,j=na-1;i<j;++i,--j)
            swap( ca[i], ca[j] );
        for(i=0,j=nb-1;i<j;++i,--j)
            swap( cb[i], cb[j] );

        for(i=0;i<na;++i){
            if( cb[i]>=ca[i] ){
                ca[i]= cb[i]-ca[i]+'0';
            }
            else{
                ca[i]= cb[i]+3-ca[i]+'0';
            }
        }

        lld ans= trans2( ca, na );
        cout<< ans <<endl;
    }
}



C题

给定n( n<= 100000 )个数字的数列; 要求选择一个数,用非本身的数代替,使得重新排序后,每个位置上的数最小,依次输出这些最小的数。

易错题。。。。。。。。。。。。。。

先sort,然后修改最后一个数字为1,再sort输出,一开始我就这样交,错了。

原因是如果所有数都是1的话,最后一个数字必须改变为一个不是本身的数,那么应该特判是否为1,如果是1,改为2后再sort;


D题

给定8个点,问是否可以得到一个正方形和一个长方形,如果可以输出点编号组合。

代码:

下面判断是否是长方形的部分,m[]保存的是4个点,我判断这4个点是否可行的做法是枚举他们的位置,按位置结合距离关系来判断。

/*Feb 20, 2012 1:15:11 PM yimao D-Rectangle and Square GNU C++	Accepted 30ms 1400KB*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a));
typedef long long lld;
typedef unsigned long long u64;
#define maxn

void up_min(int &x,int y){if((x>y)||(x==-1))x=y;}

double x[10], y[10], dis[10][10];
double Dis(int i,int j){
    return ( x[i]-x[j] ) * ( x[i]-x[j] ) + ( y[i]-y[j] )*( y[i]-y[j] );
}

bool Judge_square(int i,int j,int k,int g){
    if( dis[i][j]==dis[i][k] ){
        if( dis[g][k]==dis[g][j] && dis[g][k]==dis[i][j] && dis[i][g]==dis[j][k] )
            return 1;
    }
    if( dis[i][j]==dis[i][g] ){
        if( dis[k][g]==dis[k][j] && dis[k][g]==dis[i][j] && dis[i][k]==dis[j][g] )
            return 1;
    }
    if( dis[i][g]==dis[i][k] ){
        if( dis[j][k]==dis[j][g] && dis[i][k]==dis[k][j] && dis[i][j]==dis[g][k] )
            return 1;
    }
    return 0;
}

int m[5];
bool Judge_rec(){
    int i,j,k,g;
    for(i=1;i<=4;++i){
        for(j=1;j<=4;++j){
            if(m[i]!=m[j]){
                for(k=1;k<=4;++k){
                    if(m[i]!=m[k]&&m[j]!=m[k]){
                        for(g=1;g<=4;++g){
                            if(m[g]!=m[i]&&m[g]!=m[j]&&m[g]!=m[k]){
                                if( dis[m[i]][m[j]]==dis[m[g]][m[k]] &&
                                    dis[m[i]][m[g]]==dis[m[j]][m[k]] && dis[m[i]][m[k]]==dis[m[j]][m[g]] )
                                    return 1;
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}

int main()
{
    //freopen("D.txt","r",stdin);
    int i,j,k,g;
    for(i=1;i<=8;++i){
        scanf("%lf%lf",x+i, y+i);
    }
    for(i=1;i<8;++i)
        for(j=i+1;j<=8;++j){
             dis[i][j]= Dis(i,j);
             dis[j][i]= dis[i][j];
        }

    bool flag=0;
    for(i=1;i<6;++i){
        for(j=i+1;j<7;++j){
            for(k=j+1;k<8;++k){
                for(g=k+1;g<=8;++g){
                    if( Judge_square(i,j,k,g) ){
                        int t=0;
                        for(int cc=1;cc<=8;++cc)
                            if( cc!=i && cc!=j && cc!=k && cc!=g )
                                m[++t]= cc;

                        if( Judge_rec() ){
                            flag=1;
                            goto loop;
                        }
                    }
                }
            }
        }
    }

    loop:
        if( !flag ) puts("NO");
        else{
            puts("YES");
            printf("%d %d %d %d\n", i,j,k,g);
            printf("%d %d %d %d\n", m[1],m[2],m[3],m[4]);
        }
}


E题

不会,

只有看懂解题报告后才知道这可以说是一个水题。

值得思考。

First, let's solve the problem when there are no spoiled cards. Let  a be the number of ones and  b be the number of zeroes. It is easy to see that if  a < b then the outcome is 00, because the first player can always remove ones until they are over, which will happen before the end of the game regardless of the second player's moves. Similarly, if  a > b + 1 then the outcome is 11.

If  a = b or  a = b + 1 then the outcome is either 01 or 10. That's because the first player will always remove ones, because otherwise the outcome will be 00 which is worse than any other outcome for him. Similarly, the second player will always remove zeroes. One may notice that the first player can always remove the first card to the left with 1 written on it, because it won't make the outcome worse for him. Similarly, the second player can always remove the first card to the left with 0 written on it. That means that the last card won't be removed by anyone. Thus is the last card is 1 then the outcome is 01, otherwise it is 10.

We've learned how to solve the problem is there are no '?' signs. Now, suppose that the number of ones is  a, the number of zeroes is  band the number of question signs is  c. To check if the outcome 00 is possible, one can simply replace all question signs with zeroes and use the previous result, i.e. check if  a < b + c. Similarly, the outcome 11 is possible is  a + c > b + 1.

Let's show how to check if the outcome 01 is possible. If the last character of the string is 0, then the string is not possible. If the last character is ? then we can replace it with 1, i.e. decrease  c by 1 and increase  a by 1. Suppose we want to replace  x question signs with one and  c - x question signs with zero. Then the following equality must hold:  x + a = b + c - x + (a + b + c) mod 2. Thus,  x = (b + c - a + (a + b + c) mod 2) / 2. If the resulting value of x is non-negative and is not greater than c, then the outcome 01 is possible, otherwise it is not possible.

We can check if the outcome 10 is possible in the similar way.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值