CodeForces #294 div.2 题解

本文解析了五道涉及国际象棋棋子价值评估、数字序列分析、队伍组合优化、字符串特征统计及树节点距离判断的编程题,提供了两种解决思路并附带详细代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A题:

题意:给出一个国际象棋棋盘,上面有若干个棋子,每个棋子用一个字母表示,并且每种棋子有对应的价值,判断黑白两方场上的棋子哪方的价值更高

思路:遍历棋盘,统计价值即可

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c );
#define RDS(s) scanf ( "%s", s );
#define PIL(a) printf ( "%d\n", a );
#define PIIL(a,b) printf ( "%d %d\n", a, b );
#define PIIL(a,b,c) printf ( "%d %d %d\n", a, b, c );
#define PSL(s) printf ( "%s\n", s );
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
    int x, y, cnt;
    node(){}
    node( int _x, int _y ) : x(_x), y(_y) {}
    node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
int main()
{
    string str[8];
    int sum_1 = 0;
    int sum_2 = 0;
    REP ( i, 0, 7 ){
        cin >> str[i];
        REP ( j, 0, str[i].size() - 1 ){
            if ( str[i][j] == 'q' )sum_1 += 9;
            if ( str[i][j] == 'r' )sum_1 += 5;
            if ( str[i][j] == 'b' || str[i][j] == 'n' ) sum_1 += 3;
            if ( str[i][j] == 'p' )sum_1 += 1;
            if ( str[i][j] == 'Q' )sum_2 += 9;
            if ( str[i][j] == 'R' )sum_2 += 5;
            if ( str[i][j] == 'B' || str[i][j] == 'N' ) sum_2 += 3;
            if ( str[i][j] == 'P' )sum_2 += 1;
        }
    }
    if ( sum_2 > sum_1 )
        cout << "White" << endl;
    else if ( sum_2 < sum_1 )
        cout << "Black" << endl;
    else if ( sum_1 == sum_2 )
        cout << "Draw" << endl;
    return 0;
}


B题:

题意:第一行给出一个数字n,接下来的二、三、四行分别给出n,n-1,n-2个数,第三行中所有数字一定出现在第二行中,第四行中所有数字一定出现在第三行中,求第三行和第四行被删去的数字

思路:(1)对三行数字分别排序,遍历查询不同的数字   (2)对同一行数字进行异或

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c );
#define RDS(s) scanf ( "%s", s );
#define PIL(a) printf ( "%d\n", a );
#define PIIL(a,b) printf ( "%d %d\n", a, b );
#define PIIL(a,b,c) printf ( "%d %d %d\n", a, b, c );
#define PSL(s) printf ( "%s\n", s );
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
    int x, y, cnt;
    node(){}
    node( int _x, int _y ) : x(_x), y(_y) {}
    node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
//异或处理
int main()
{
    int k;
    cin >> k;
    int num_1 = 0, num_2 = 0, num_3 = 0;
    int num = 0;
    REP ( j, 0, k - 1 ){
        cin >> num;
        num_1 ^= num;
    }
    REP ( j, 0, k - 2 ){
        cin >> num;
        num_2 ^= num;
    }
    cout << ( num_1 ^ num_2 ) << endl;
    REP ( j, 0, k - 3 ){
        cin >> num;
        num_3 ^= num;
    }
    cout << ( num_2 ^ num_3 ) << endl;
    return 0;
}


//排序处理
int num1[1000005];
int num2[1000005];
int num3[1000005];
int main()
{
   clr ( num1, 0 );
   clr ( num2, 0 );
   clr ( num3, 0 );
    int k;
    cin >> k;
    REP ( j, 0, k - 1 ){
        cin >> num1[j];
    }
    sort ( num1, num1 + k );
    REP ( j, 0, k - 2 ){
        cin >> num2[j];
    }
    sort ( num2, num2 + k - 1 );
    REP ( j, 0, k - 3 ){
        cin >> num3[j];
    }
    sort ( num3, num3 + k - 2 );
    REP ( j, 0, k ){
        if ( num1[j] != num2[j] ){
            cout << num1[j] << endl;
            break;
        }
    }
    REP ( j, 0, k ){
        if ( num2[j] != num3[j] ){
            cout << num2[j] << endl;
            break;
        }
    }
    return 0;
}


C题:

题意:有n个老生和m个新生要组队,有两种组队方式,一新生两老生或一老生两新生,求最多能组几个队

思路:遍历所有可能即可

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c );
#define RDS(s) scanf ( "%s", s );
#define PIL(a) printf ( "%d\n", a );
#define PIIL(a,b) printf ( "%d %d\n", a, b );
#define PIIL(a,b,c) printf ( "%d %d %d\n", a, b, c );
#define PSL(s) printf ( "%s\n", s );
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
    int x, y, cnt;
    node(){}
    node( int _x, int _y ) : x(_x), y(_y) {}
    node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
int main()
{
    int m, n, ans = 0;
    cin >> m >> n;
    REP ( i, 0, m / 2 ){
        int tmp_1 = m - 2 * i;
        int tmp_2 = n - i;
        if ( tmp_2 < 0 )continue;
        int tmp = min ( tmp_1, tmp_2 / 2 );
        ans = max ( ans, tmp + i );
    }
    cout << ans << endl;
    return 0;
}


D题:

题意:每个对应的英文字母有对应的价值,给出一个字符串,求该字符串的字串中有多少个字符串首尾字母相同并且所有字母价值和为0,

思路:map处理前后缀即可

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c );
#define RDS(s) scanf ( "%s", s );
#define PIL(a) printf ( "%d\n", a );
#define PIIL(a,b) printf ( "%d %d\n", a, b );
#define PIIL(a,b,c) printf ( "%d %d %d\n", a, b, c );
#define PSL(s) printf ( "%s\n", s );
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
    int x, y, cnt;
    node(){}
    node( int _x, int _y ) : x(_x), y(_y) {}
    node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};

map < ll, int > mp[30];
int num[30];
char str[100010];
int main()
{
    while ( cin >> num[0] ){
        REP ( i, 1, 25 ){
            cin >> num[i];
        }
        REP ( i, 0, 25 ){
            mp[i].clear();
        }
        cin >> str;
        int n = strlen ( str );
        ll ans = 0;
        ll sum = 0;
        REP ( i, 0, n - 1 ){
            int id = str[i] - 'a';
            if ( mp[id].find( sum ) != mp[id].end() )
                ans += mp[id][sum];
            sum += num[id];
            mp[id][sum] ++;
        }
        cout << ans << endl;
    }
}


E题:

题意:给出一棵树上的两个节点,判断这个树上有多少个节点与这两个点的距离相同

思路:LCA问题,代码思考中



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值