Manthan, Codefest 18 (rated, Div. 1 + Div. 2) A、B、C、D

A. Packets(二进制长度)

和以前写过的一个找零钱的题一样。求n的二进制长度即可。


 #pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define rep(i,x,y) for(int i = x; i < y; i++)
#define Rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define line cout << "------------" << endl

typedef long long ll;
const int maxn = 1e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1010;

int main(){
	ll n;
	while(scanf("%lld",&n)!=EOF){
		ll ans = 1;
		ll cnt = 0;
		while(ans <= n){
			ans *= 2;
			cnt ++;
		}
		printf("%lld\n", cnt);
	}
    return 0;
}

B. Reach Median(思维)

先排序。

当中位数<=m 时,我们求把右边的数加至m的花费

否则,我们求把左边的书减至m的花费

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;

ll n, m;
ll a[MAXN];
int main(){
    while(scanf("%lld%lld", &n, &m) != EOF){
        clr(a);
        for(int i=0; i<n; i++)  scanf("%lld", &a[i]);
        //line;
        sort(a, a+n);
        int mid = n/2;
        ll sum = 0;
        if(a[mid] <= m){
            for(int i=mid; i<n; i++){
                if(a[i] >= m) break;
                else sum += (m-a[i]);
            }
        }
        else{
            for(int i=mid; i>=0; i--){
                if(a[i] <= m) break;
                else sum += abs(m-a[i]);
            }
        }
        printf("%lld\n", sum);
    }
    return 0;
}

C. Equalize(思维)

如果a相邻的两个与b对应位置不同且a左与a右不同,这种情况的最小花费为1

其余的情况全按照直接取反即可。

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define rep(i,x,y) for(int i = x; i < y; i++)
#define Rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define line cout << "------------" << endl

typedef long long ll;
const int maxn = 1e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1010;

int main(){
	int n;
	char a[MAXN],b[MAXN];
	scanf("%d", &n);
	scanf("%s %s", a, b);
	int cnt = 0;
	for(int i=0;i<n;i++){
		if(a[i] == b[i]) continue;
		else{
			if(a[i+1] != b[i+1] && a[i] != a[i+1]){
				cnt ++; i++;
			}
			else cnt ++;
		}
	}
	cout << cnt <<endl;
    return 0;
}

D. Valid BFS?(思维)

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 2e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;

int n;
int a[maxn];
bool vis[maxn];
vector<int>mp[maxn];

int main(){
    while(scanf("%d", &n) != EOF){
        clr(a);clr(vis);
        for(int i=0; i<maxn; i++) mp[i].clear();
        bool flag = true;
        int u, v;
        for(int i=0; i<n-1; i++){
            scanf("%d%d", &u, &v);
            mp[u].push_back(v);
            mp[v].push_back(u);
        }
        for(int i=1; i<=n; i++) scanf("%d", &a[i]);
        if(a[1] != 1) {
            printf("No\n");
            continue;
        }
        queue<int> Q;
        int top = 1;
        Q.push(top);
        vis[top] = true;
        int pos = 2;
        while(!Q.empty()){
            vector<int> x, y;
            top = Q.front();
            Q.pop();
            for(int i=0; i<mp[top].size(); i++){
                int t = mp[top][i];
                if(!vis[t]){
                    y.push_back(t);
                    vis[t] = true;
                }
            }
            int len = y.size();
            for(int i=0; i<len; i++){
                x.push_back(a[pos]);
                Q.push(a[pos]);
                pos ++;
            }
            if(x.size() != y.size()) flag = false;
            else{
                sort(x.begin(), x.end());
                sort(y.begin(), y.end());
                for(int i=0; i<x.size(); i++){
                    if(x[i] != y[i]) {
                        flag = false;
                        break;
                    }
                }
            }
            if(!flag) break;
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

    原文作者:B树
    原文地址: https://blog.csdn.net/l18339702017/article/details/82321919
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞