C/C++教程

2021 ccpc 女生赛部分题解

本文主要是介绍2021 ccpc 女生赛部分题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A

正反各跑一遍匹配

正着满足,反着不满足,没有坐反

正着不满足,反着满足,坐反

都满足,不知道

#include <bits/stdc++.h>
using namespace std;


int n , m ,  x , y , a[20] , b[20] , c1[20] , c2[20] , op , t1 , t2;


int main()
{
    cin >> n >> x >> y;
    for( register int i = 1 ; i <= n ; i ++ ) cin >> a[i];
    cin >> m;
    for( int i = 1 ; i <= m ; i ++ ) cin >> b[i];
    t1 = t2 = 1;
    if( x < y )
    {
        for( int i = 1 ; i <= m ; i ++ ) if( a[ x + i ] != b[i] ) t1 = 0;
        if( x - 1 >= m )
        {
            for( int i = 1 ; i <= m ; i ++ ) if( a[ x - i ] != b[i] ) t2 = 0;
        }
        else t2 = 0;
    }
    else
    {
        for( int i = 1 ; i <= m ; i ++ ) if( a[ x - i ] != b[i] ) t1 = 0;
        if( n - x >= m )
        {
            for( int i = 1 ; i <= m ; i ++ ) if( a[ x + i ] != b[i] ) t2 = 0;

        }
        else t2 = 0;
    }
    if( t1 && t2 ) puts("Unsure");
    else if( t1 ) puts("Right");
    else puts("Wrong");
    return 0;
}

D

相邻的两个村庄之间一定会连一条道路,权值就是两个村庄的较大值

证明

设区间[l,r],有\(\forall i \in [l,r] , a_k \ge a_i(l\le k\le r)\),则该区间可分为三个连通块,[l,k-1],[k+1,r][k]无论怎样左右两个联通块和[k]连接的权值都是\(a_k\),所以不妨让其与相邻的两个村庄相连,递归操作就可以得到结论

#include <bits/stdc++.h>
using namespace std;

#define ll long long
ll n , l , r , ans;
int main()
{
    cin >> n >> l;
    for( register int i = 1 ; i < n ; i ++ )
    {
        cin >> r;
        ans += max( l , r ) , l = r;
    }
    cout << ans << endl;
}

G

当\(r\)足够大时,每个基站都可以覆盖所有的点

\[lim_{r\to +\infty} f(r)=\frac{1}{n} \]

#include <bits/stdc++.h>
using namespace std;


int main()
{
    double a;
    cin >> a;
    printf("%.9lf" , 1.0 / a );
    return 0;
}

I

模拟题

#include <bits/stdc++.h>
#define fx ( x + dx[dir] )
#define fy ( y + dy[dir] )
using namespace std;

const int N = 55;
const int dx[] = { -1 , -1 , 0 , 1 , 1 , 1 , 0 , -1 };
const int dy[] = { 0 , +1 , +1 , 1 , 0 , -1 , -1 , -1 };
int x , y , n , m , t , v = 0, dir = 0;
char mapp[N][N] , opt;

int main()
{
    cin >> n >> m;
    for( register int i = 0 ; i <= m + 1 ; i ++ ) mapp[i][0] = mapp[i][ m + 1 ] = '#';
    for( register int i = 0 ; i <= n + 1 ; i ++ ) mapp[0][i] = mapp[ n + 1 ][i] = '#';
    for(register int i = 1 ; i <= n ; i ++ )
    {
        for( register int j = 1 ; j <= m ; j ++ )
        {
            cin >> mapp[i][j];
            if( mapp[i][j] == '*') x = i , y = j;
        }
    }
    cin >> t;
    for( register int i = 1 ; i <= t ; i ++ )
    {
        cin >> opt;
        if( opt == 'U' ) v ++;
        else if( opt == 'D' ) v = max( 0 , v - 1 );
        else if( opt == 'L' ) dir = ( dir + 7 ) % 8;
        else if( opt == 'R' ) dir = ( dir + 1 ) % 8;
        //move
        register bool f = 1;
        for( register int j = 1 ; j <= v && f; j ++ )
        {
            if( mapp[x][fy] == '#' && mapp[fx][y] == '#' && dir % 2 == 1 ) f = 0 , v = 0;
            else if( mapp[fx][fy] == '#' ) f = 0 , v = 0;
            else x = fx , y = fy;
        }
        if( f ) cout << x << " " << y << endl;
        else cout << "Crash! " << x << ' ' << y << endl;
    }

    return 0;
}

K

统计-数量

#include <bits/stdc++.h>
using namespace std;


int main()
{
    int n , cnt = 0;
    cin >> n;
    string s;
    getchar();
    for( register int i = 1 ; i <= n ; i ++ )
    {
        getline( cin , s );
        for( auto t : s )
        {
            if( t == '-' ) cnt ++;
        }
    }
    cout << cnt << endl;

}
这篇关于2021 ccpc 女生赛部分题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!