Codeforces Round #592 (Div. 2)A-D

本文精选了四道算法竞赛题目及解决方案,涉及数学、图论、动态规划和搜索算法,通过实际代码展示了如何解决复杂问题,适合算法学习者深入研究。

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

传送门

A:签到
code

#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 7;
typedef  long long ll;
list<int>x, y, z;
int n;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    srand((int)time(0));
    int t;
    cin>>t;
    while (t--){
        double a,b,c,d,k;
        cin>>a>>b>>c>>d>>k;
        int n1 =ceil(1.0*a/c);
        int n2 = ceil(1.0*b/d);
        if (n1+n2>k)
            cout<<-1<<endl;
        else {
            cout<<n1<<" "<<k-n1<<endl;
        }
    }
    return 0;
}

B:签到
code

#include <bits/stdc++.h>
using namespace std;
const int mx = 2e6 + 7;
typedef  long long ll;
list<int>x, y, z;
int n;
char a[mx];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    srand((int)time(0));
    int t;
    cin>>t;
    while (t--){
        cin>>n;
        cin>>a;
        int mx= -1;
        for(int i=0;i<n;i++){
            if (a[i]=='1'){
                if (abs(i - 0)+1>mx){
                    mx = abs(i - 0)+1;
                }
                if(abs(i-(n-1))+1>mx) {
                    mx = abs(i-(n-1))+1;
                }
            }
        }
        if (mx==-1)
            cout<<n<<endl;
        else
            cout<<(mx)*2<<endl;
    }
    return 0;
}

C
题意:给你n,p,w,d。
解方程:xw+yd = p; x+y+z=n。x,y,z为未知数
(1≤n≤1e12,0≤p≤1e17,1≤d<w≤1e5)
题解:听说是扩展GCD模板题,我用了及其暴力的方法,就是直接枚举1e6个w和d的倍数,看看这些数字能不能凑出xw+yd = p的等式,然后判断第二条式子。最后特判一些特殊情况,比如一开始p就能被w或者d或者w+d整除。
code

#include <bits/stdc++.h>
using namespace std;
const int mx = 2e6 + 7;
typedef long long ll;
typedef unsigned  long long ull;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ull n,p,w,d;
    cin>>n>>p>>w>>d;
    if (w*n<p){
        cout<<-1<<endl;
    }
    else if(p==0){
        cout<<"0 0 "<<n<<endl;
    }
    else if (p<d){
        cout<<-1<<endl;
    }
    else if (p<w){
        if (p%d==0){
            cout<<0<<" "<<p/d<<" "<<n - p/d<<endl;
        }
        else{
            cout<<-1<<endl;
        }
    }
    else {
        int f = 1;
        if(p%w==0){
            ull ww = p/w;
            if (ww<=n) {
                cout << ww << " " << 0 << " " << n-ww << endl;
                f= 0;
            }
        }
        if(p%d==0&&f){
            ull dd = p/d;
            if (dd<=n) {
                cout << 0 << " " << dd << " " << n-dd << endl;
                f= 0;
            }
        }
        if (p%(d+w)==0&&f){
            ull wd = p/(d+w);
            if (2*wd<=n) {
                cout << wd << " " << wd << " " << n-wd-wd << endl;
                f= 0;
            }
        }
        if (f) {
            for (int i = 1; i <= mx; i++) {
                ull cp = p - w * i;
                if (cp < 0)
                    break;
                if (cp % d == 0) {
                    ull dd = cp / d;
                    if (dd + i > n)
                        continue;
                    cout << i << " " << dd << " " << n - i - dd << endl;
                    f = 0;
                    break;
                }
            }
        }
        if (f) {
            for (int i = 1; i <= mx; i++) {
                ull cp = p - d*i;
                if (cp<0)
                    break;
                if (cp%w==0) {
                    ull ww = cp/w;
                    if (ww+i>n)
                        continue;
                    cout<<ww<<" "<<i<<" "<<n-ww-i<<endl;
                    f = 0;
                    break;
                }
            }
        }
        if (f)
            cout<<-1<<endl;
    }
    return 0;
}

D
题意:给你一颗树,然后要你把树上的每一个节点都用一种颜色表示,一共有三种颜色,以及涂上每一种颜色的代价,要求该点和与其邻近的所有点构成的集合中,没有重复的颜色,所以显然,只有当这一颗树为一条链的时候才符合。而且为链的时候,有且只有6种路径,全排列找路径就OK。注意这一条链,不一定是从1开始,所以一开始要找开头。
code

#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5 + 7;
typedef long long ll;
typedef unsigned  long long ull;
ll a[4][mx];
ll r[10][mx];
ll ro[mx];
vector<int> edge[mx];
int h[mx];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[1][i];
    for(int i=1;i<=n;i++)
        cin>>a[2][i];
    for(int i=1;i<=n;i++)
        cin>>a[3][i];
    int f = 0,s =0;
    for(int i=1;i<=n-1;i++){
        int u,v;
        cin>>u>>v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        h[u]++;
        h[v]++;
        if (h[u]>=3||h[v]>=3)
            f= 1;
    }
    if(f){
        cout<<-1<<endl;
    }else {
        int t=0;
        for(int i=1;i<=n;i++) {
            if (h[i]==1)
                s = i;
        }
        int fa =0;
        for(int i=1;i<=n;i++){
            ro[++t] = s;
            for(auto k:edge[s]){
                if (k==fa) continue;
                fa = s;
                s = k;
                break;
            }
        }
        int aa[3]={1,2,3};
        int ca = 1;
        ll mi = 1e18+7;
        int index =0;
        do{
            ll ans = 0;
            for(int i=1;i<=t;i++){
                int co = aa[(i-1)%3];
                ans += a[co][ro[i]];
                r[ca][ro[i]] = co;
            }
            if (ans<mi){
                mi = ans;
                index = ca;
            }
            ca++;
        }while(next_permutation(aa,aa+3));
        cout<<mi<<endl;
        for(int i=1;i<=n;i++){
            cout<<r[index][i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值