Codeforces Round #301 (Div. 2)A B C

A - Combination Lock 开锁最小的步骤数

char a[1010];
char c[1010];

int main()
{
    int n;
    while ( ~scanf("%d", &n) )
    {
        scanf("%s%s", a, c);
        int ans = 0;
        for( int i = 0; i < n; ++i )
        {
            ans += abs( a[i] - c[i] ) > 5 ? 10 - abs( a[i] - c[i] ) : abs( a[i] - c[i] );
            //printf("ans: %d\n", ans);
        }
        printf("%d\n", ans);
    }
    return 0;
}

B - School Marks 给出n门成绩中的k门,问在每门分数不超过p,中位数不低于y,总分不超过x的情况下,剩下n-k门的成绩可能是多少。

可以确定有一种情况是无论如何都不符合的,即 小于中位数的成绩已经有n/2门以上了,可以直接判掉,剩下的情况下,因为总分不能超过x,即总分要尽量小,那么选中位数那个最低分数y或1来当剩下k门分数总是没错的。所以只需要尽量使成绩为1的尽可能多(<=n/2),剩下的都是y,最后判断是不是总分超过x即可

const int N = 1010;

int a[N];
vector <int> v;

int main()
{
    int n, p, k, x, y;
    while( ~scanf("%d%d%d%d%d", &n, &k, &p, &x, &y) ) {
        int sum = 0, small = 0;
        v.clear();
        for( int i = 1; i <= k; ++i ) {
            scanf("%d", &a[i]);
            sum += a[i];
            small += ( a[i] < y );
        }
        if( small >= (n+1) / 2 ) {
            puts("-1");
            continue;
        }
        int tmp = 0;
        while ( small + tmp < n / 2 && k + tmp < n ) {
            v.push_back( 1 );
            ++sum;
            ++tmp;
        }
        while( k + tmp < n ) {
            v.push_back( y );
            sum += y;
            ++tmp;
        }
        if( sum > x )
            puts("-1");
        else
        {
            for( int i = 0; i < v.size(); ++i )
                printf("%d ", v[i]);
            puts("");
        }
    }
    return 0;
}

 求起点走到终点的时候终点为’X‘是否可能,并输出“YES”或“NO”,其中每次踩到(坚固的冰)’.‘上面时,坚固的并会变成易破碎的“X”。

简单的bfs

const int N = 510;

char a[N][N];
int n, m;
int sx, sy, ex, ey;
int dir[4][2] = {
    1, 0, -1, 0, 0, 1, 0, -1
};

queue < pair< int, int > > q;

bool bfs( )
{
    while( !q.empty() ) q.pop();
    q.push( make_pair( sx, sy ) );
    while( !q.empty() )
    {
        auto now = q.front();
        q.pop();
        for( int i = 0; i < 4; ++i )
        {
            int xx = now.first + dir[i][0];
            int yy = now.second + dir[i][1];
            if( xx <= 0 || xx > n || yy <= 0 || yy > m )
                continue;
            if( xx == ex && yy == ey )
            {
                if( a[xx][yy] == 'X' )
                    return 1;
                else
                {
                    a[xx][yy] = 'X';
                    q.push( make_pair( xx, yy ) );
                }
            }
            else
            {
                if( a[xx][yy] == '.' )
                {
                    a[xx][yy] = 'X';
                    q.push( make_pair( xx, yy ) );
                }
                else
                    continue;
            }
        }
    }
    return 0;
}

int main()
{
    while( ~scanf("%d%d", &n, &m) ) {
        memset( a, 'X', sizeof( a ) );
        for( int i = 1; i <= n; ++i ) {
            scanf("%s", a[i]+1);
        }
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        if( bfs( ) )
            cout << "YES" << endl;
        else
            cout << "NO" <<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值