Codeforces Round #313 (Div. 2) (ABCD)

本文解析了四道编程题目,包括货币组合问题、相框容纳问题、六边形内正三角形数量计算及字符串相似性判断。通过巧妙的算法设计与实现,提供了清晰的思路与代码实现。

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

A

题意:

给你n种货币,问用这n中货币能否组成所有的价值,如果可以输出-1,否则输出最小的不能组成的价值。

解析:

做这个题目的时候还思考了一会儿,后来发现,所有如果没有1的话,那么就算有任何值(大于1)都无法构成1,而如果有1的话,就可以构成任何值,所以这题就算判断有没有1出现,有1就输出-1,没有就输出1。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
bool dp[MAXN*2];
int n, A[1005];

int main() {
    while(scanf("%d", &n) != EOF) {
        memset(dp, false, sizeof(dp));
        for(int i = 1; i <= n; i++) {
            scanf("%d", &A[i]);
            dp[A[i]] = true;
        }
        if(dp[1])
            printf("%d\n", -1);
        else 
            printf("%d\n", 1);
    }
    return 0;
}

B

题意:

给你1个相框其长度为A1,宽度为B1,再给你两幅画,其长宽分别为(A2, B2),(A3, B3),问你能否把两张画塞进相框

解析:

只有8种情况很好考虑,直接暴力枚举过。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;

int w, h;
int A[2], B[2];

bool judge() {
    int s1 = A[0]*A[1], s2 = B[0]*B[1];
    if(s1 + s2 > w*h) return false;
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            if(max(A[i], B[j]) <= w && A[i^1] + B[j^1] <= h)
                return true;
            if(max(A[i], B[j]) <= h && A[i^1] + B[j^1] <= w)
                return true;
        }
    }
    return false;
}

int main() {
    while(scanf("%d%d", &w, &h) != EOF) {
        scanf("%d%d", &A[0], &A[1]);
        scanf("%d%d", &B[0], &B[1]);
        if(w > h) swap(w, h);
        printf("%s\n", judge() ? "YES" : "NO");
    }
    return 0;
}

C

题意:

给你一个6边形的6个边,每个角都是120°,现在问你这个6边形内,有多少个正3角形,每个正3角形。

解析:

这题首先想到的是补足思想,把6边形的3个角补全,然后再减去这3个正3角形,那么补全后的6边形肯定是一个正3角形,其每个边长为(a1+a2+a3),减去每个小正3角形,每个小正3角形的边长分别为a1a3a5,最后答案可以推算出是(a1+a2+a3)2a21a23a25

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int INF = 0x3f3f3f3f;

int a[10];

int main() {
    //freopen("in.txt", "r", stdin);
    while(scanf("%d", &a[1]) != EOF) {
        for(int i = 2; i <= 6; i++) {
            scanf("%d", &a[i]);
        }
        int len = a[1] + a[2] + a[3];
        int S =  len * len;
        int s1 = a[1]*a[1];
        int s2 = a[3]*a[3];
        int s3 = a[5]*a[5];
        int ans = (S - s1 - s2 - s3);
        printf("%d\n", ans);
    }
    return 0;
}

D

题意:

给出两个等长的字符串,定义一种判断两个字符串相等的方法,判断两个字符串是否相等。

解析:

因为是每次都将字符串分为等长的两个字符串 ,所以字符串的长度必须是偶数时才能继续划分,奇数时就要一个个的字符进行判断。

注意:

要用记忆化搜索来优化搜索,不如会超时。

my code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
#define make make_pair
using namespace std;
string A, B;

map<pair<string,string>, bool> dp;

bool dfs(string p, string q) {
    if(dp.count(make(p, q))) return dp[make(p, q)];
    if(p == q) return dp[make(p, q)] = true;
    if(p.size() == 1) return dp[make(p, q)] = false;
    if(p.size() != q.size()) return dp[make(p, q)] = false;

    int len = p.size();
    int mid = len / 2;
    string L1, R1, L2, R2;
    if(len % 2 == 0) {
        L1 = p.substr(0, mid);
        R1 = p.substr(mid, len-mid);
        L2 = q.substr(0, mid);
        R2 = q.substr(mid, len-mid);
        if(dfs(L1, L2) && dfs(R1, R2) || dfs(L1, R2) && dfs(R1, L2))
            return dp[make(p, q)] = true;
    }
    return dp[make(p, q)] = false;
}

int main() {
    while(cin >> A >> B) {
        dp.clear();
        printf("%s\n", dfs(A, B) ? "YES" : "NO");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值