Codeforces Round # 635 (Div. 2) Solution

A. Ichihime and Triangle

Topic:

Given that $ a ≤ x ≤ b, b ≤ y ≤ c, c ≤ z ≤ d $, let you determine $ x, y, z $ to make three sides of the triangle

Ideas:

Directly make the length of the three sides $ b, c, d $

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
    ll t,a,b,c,d;
    cin>>t;
    while(t--){
        cin>>a>>b>>c>>d;
        cout<<b<<" "<<c<<" "<<c<<endl;
    }
    return 0;
}
View Code

B. Kana and Dragon Quest game

Topic:

Given a number $ h $, you can perform two operations: $ 1.h = [h / 2 +10], 2. h = [h-10] $, ask if you can operate $ 1 $ through $ n $, and $ m $ kind of operation $ 2 $ makes $ h $ less than or equal to $ 0 $

Ideas:

By solving the inequality $ h / 2 + 10 ≤ h $, we can find that performing the operation $ 1 $ when $ h ≤ 20 $ will only make $ h $ bigger

So we first reduce $ h $ to the smallest position greater than $ 20 $ through operation $ 1 $, and then use operation $ 2 $ to determine whether it will be less than or equal to $ 0 $

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
void put1(){ puts("YES") ;}void put2(){ puts("NO") ;}void put3(){ puts("-1"); }
ll qp(ll a,ll b, ll p){ll ans = 1;while(b){if(b&1){ans = (ans*a)%p;--b;}a =
(a*a)%p;b >>= 1;}return ans%p;}
int main(){
    ll p=read();
    while(p--){
        ll a,b,c;
        cin>>a>>b>>c;
        while(b&&a>20){
            b--;
            a=a/2+10;
        }
        if(a>c*10) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}
View Cod

C. Linova and Kingdom

Topic:

Give a tree, now you can dye $ K $ nodes to white, and dye the remaining nodes to black, requiring all white nodes to pass through the node $ 1 $ and the black node and the maximum

Ideas:

Greedy can find that it is optimal to dye all nodes with the largest possible depth to white, but the problem may be that the depth cannot be sorted and accumulated, because its parent node may also be a white point.

So we also need to count the size of the subtree of the node, sort $ dep [i]-siz [i] $, and take the first $ k $

#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define io std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int manx=2e5+5;

vector<ll>g[manx];
vector<pair<ll,ll> >a;
ll cnt[manx],d[manx],col[manx];
ll n,k;
void dfs(ll u,ll pre){
    d[u]=d[pre]+1; cnt[u]=1;
    for(auto v: g[u]){
        if(v==pre) continue;
        dfs(v,u);
        cnt[u]+=cnt[v];
    }
    a.pb(mp(d[u]-cnt[u],u));
}
int main(){
    cin>>n>>k;
    for(int i=1;i<n;i++){
        ll u=read(),v=read();
        g[u].pb(v); g[v].pb(u);
    }
    dfs(1,0);
    sort(a.begin(),a.end());
    reverse(a.begin(),a.end());
    ll ans=0;
    for(int i=0;i<k;i++)
        ans+=a[i].first;
    printf("%lld\n",ans);
    return 0;
}
View Code

D. Xenia and Colorful Gems

Topic:

There are $ 3 $ types of numbers, and now we need to take out $ 1 $ from each number, so that $ (x + y) ^ {2} + (x + z) ^ {2} + (z + y) ^ {2 } $ Minimum

Ideas:

Violent enumeration of $ 3 $ numbers is definitely not going to work

Thinking further, we found that for a number $ a_ {i} $, there will be $ 4 $ ways corresponding to $ b and c $

分别是$①. a_{i} ≥ b ,a_{i} ≥ c   ②. b ≥ a_{i} a_{i} ≥ c   ③. c ≥ a_{i} a_{i} ≥ b   ④.c ≥ a_{i}  b ≥ a_{i}$

According to greed, we can also know that the closer $ b and c $ must be to $ a_ {i} $, the better, so we can bipartitely correspond to $ b and c $, and then keep updating the minimum answer.

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll; 
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int N=1e5+5;
ll t,n1,n2,n3;
ll a[N],b[N],c[N];

ll solve(ll a[],ll n1,ll b[],ll n2,ll c[] ,ll n3){
    
    ll ans=2e18,x,y,z;
    for(int i=1;i<=n1;i++){
        x=a[i];
        int pos=lower_bound(b+1,b+1+n2,x)-b;
        if(pos>=1&&pos<=n2 &&b[pos]>=x)y=b[pos];
        else continue;
        int l=1,r=n3;
        while(l<r){
            int mid=(l+r+1)>>1;
            if(c[mid]<=x) l=mid;
            else r=mid-1; 
        }
        if(c[l]<=x) z=c[l];
        else continue;
        
        ans=min(ans,(x-y)*(x-y)+(x-z)*(x-z)+(y-z)*(y-z));
    }
    return ans;
} 
int main(){
    ll t=read();
    while(t--){
        ll n1=read(),n2=read(),n3=read();
        for(int i=1;i<=n1;i++)scanf("%lld",&a[i]);
        for(int i=1;i<=n2;i++)scanf("%lld",&b[i]);
        for(int i=1;i<=n3;i++)scanf("%lld",&c[i]);
        sort(a+1,a+1+n1);
        sort(b+1 , b + 1 + n2); 
        sort (c + 1 , c + 1 + n3); 
        ll years = 2e18; 
        ans = min (ans, solve (a, n1, b, n2, c, n3)); 
        ans = min (ans, solve (a, n1, c, n3, b, n2)); 
        ans = min (ans, solve (b, n2, a, n1, c, n3)); 
        ans = min (ans, solve (b, n2, c, n3, a, n1)); 
        ans = min (ans, solve (c, n3, b, n2, a, n1)); 
        ans = min (ans, solve (c, n3, a, n1, b, n2)); 
        cost << years << endl ;; 
    } 
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/overrate-wsj/p/12709828.html