SDUT 2021 Spring Team Contest--- 3(Gym 102896)

48 篇文章 0 订阅
34 篇文章 0 订阅

A - Almost Balanced Tree

题目链接

答案:

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
using namespace std;

int pos=1;
int dp[N];
int lr[N];
int rr[N];
bool flag;

int DFS(int a,int b){
    int res=pos++;
    if(b) dp[res]=2,b--;
    else dp[res]=1,a--;
    int xb=b/2;
    int yb=b-b/2;
    int k=(yb>xb)*min(a,2);
    int xa=k;
    int ya=0;
    a-=k;
    xa+=a/2;
    ya+=a-a/2;
    if(abs((xa+2*xb)-(ya+2*yb))>1) {
        puts("-1");
        flag=1;
        return -1;
    }
    if(xa+xb) lr[res]=DFS(xa,xb);
    if(ya+yb) rr[res]=DFS(ya,yb);
    return res;
}

void solve()
{
    int a,b;
    cin>>a>>b;
    DFS(a,b);
    if(!flag){
        for(int i=1;i<pos;i++){
            cout<<dp[i]<<" "<<lr[i]<<" "<<rr[i]<<endl;
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

C - Color the Tree

题目链接

答案:

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define pb push_back
const int N = 1e5 + 10;
using namespace std;

int dp[25];
vector<int>vp[25];

void solve(){
    int n;
    cin>>n;
    for(int i=2;i<=n;i++) cin>>dp[i];
    for(int i=n;i>=2;i--){
        vp[i].pb(i);
        vector<int>st=vp[dp[i]];
        vp[dp[i]].clear();
        bool flag=0;
        if(vp[i].size()%2==0) flag=1;
        vector<int>::iterator it;
        for(it=vp[i].begin();it!=vp[i].end();it++){
            if(flag){
                for(int j=0;j<(int)st.size();j++){
                    vp[dp[i]].pb(st[j]);
                }
            }
            else{
                for(int j=st.size()-1;j>=0;j--){
                    vp[dp[i]].pb(st[j]);
                }
            }
            vp[dp[i]].pb(*it);
            flag^=1;
        }
        for(int j=0;j<(int)st.size();j++){
            vp[dp[i]].pb(st[j]);
        }
    }
    cout<<(int)vp[1].size()<<endl;
    for(int i=(int)vp[1].size()-1;i>=0;i--) cout<<vp[1][i]<<" ";
    cout<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

E - Easy Measurements

题目链接

答案:

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
using namespace std;

void solve(){
    int t;
    cin>>t;
    while(t--){
        ll b,d;
        cin>>b>>d;
        ll gcd=__gcd(b,d);
        ll bb=b*b/gcd;
       // b/=gcd;
        d/=gcd;
        ll tot=0;
        cout<<(b-1)/(d)<<endl;
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

K - Kate’s 2021 Celebration

题目链接

答案:

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
using namespace std;

int dp[N];

void solve(){
    int n;
    cin>>n;
    int minn=INF;
    int pos=0;
    for(int i=1;i<=n;i++){
        string s;
        int w;
        cin>>w>>s;
        memset(dp,0,sizeof(dp));
        int len=s.size();
        for(int j=0;j<len;j++){
            dp[s[j]-'0']++;
        }
        if(dp[1]>=1&&dp[2]>=2&&dp[0]>=1){
            if(minn>w){
                minn=w;
                pos=i;
            }
        }
    }
    cout<<pos<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

M - Miser

题目链接

答案:

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define pb push_back
const int N = 1e6 + 10;
using namespace std;

vector<int> dp[N];
int tot=0;
int vis[N];

void solve(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int k;
        cin>>k;
        while(k--){
            int x;
            cin>>x;
            dp[i].pb(x);
        }
    }
    for(int i=n;i>=1;i--){
        int maxn=-1;
        vector<int>::iterator it;
        for(it=dp[i].begin();it!=dp[i].end();it++){
            maxn=max(maxn,vis[*it]);
        }
        tot=max(tot,maxn++);
        for(it=dp[i].begin();it!=dp[i].end();it++){
            vis[*it]=maxn;
        }
    }
    cout<<tot+1<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值