AtCoder Beginner Contest 301

A - Overall Winner

#include <bits/stdc++.h>

using namespace std;

#define int long long

int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr) , cout.tie(nullptr);
	
	int n;
	cin >> n;
	string s;
	cin >> s;
	int t = 0 , a = 0;
	for( auto i : s )
		if( i == 'T' ) t ++;
		else a ++;
	if( t > a ) cout << "T";
	else if( t < a )cout << "A";
	else {
		if( s.back() == 'T' ) cout << "A";
		else cout << "T";
	}
	
	return 0;
}

B - Fill the Gaps

#include <bits/stdc++.h>

using namespace std;

#define int long long

int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr) , cout.tie(nullptr);
	
	int n;
	cin >> n;
	vector<int> a(n);
	for( int i = 0 ; i < n ; i ++ )
		cin >> a[i];
	vector<int> b;
	b.push_back( a[0] );
	for( int i = 1 ; i < n ; i ++ ){
		if( a[i] > b.back() ){
			for( int j = b.back()+1 ; j < a[i] ; j ++ )
				b.push_back(j);
		}else {
			for( int j = b.back()-1 ; j > a[i] ; j -- )
				b.push_back(j);
		}
		b.push_back( a[i] );
	}
	for( auto i : b )
		cout << i << " ";

	return 0;
}

C - AtCoder Cards

#include <bits/stdc++.h>

using namespace std;

#define int long long


int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr) , cout.tie(nullptr);
	string a , b ;
	cin >> a >> b;
	map<char ,  int > cnt , ans ;
	int d = 0;
	
	for( auto i : a )
		cnt[i] ++;
	
	for( auto i : b )
		ans[i] ++;
	
	for( auto i : "atcoder" ){
		if( cnt[i] == ans[i] ) continue;
		else if( cnt[i] > ans[i] ) ans['@'] -= cnt[i] - ans[i] , ans[i] = cnt[i];
		else cnt['@'] -= ans[i] - cnt[i] , cnt[i] = ans[i];
	}
	if( cnt['@'] < 0 || ans['@'] < 0 || cnt['@'] != ans['@'] ){
		cout << "No\n";
		return 0;
	}
	set<char> st;
	st.insert('a');
	st.insert('t');
	st.insert('c');
	st.insert('o');
	st.insert('d');
	st.insert('e');
	st.insert('r');
	
	for( auto [ k , v ] : cnt ){
		if( st.count(k) ) continue;
		if( v != ans[k] ){
			cout << "No\n";
			return 0;
		}
	}
	
	for( auto [ k , v ] : ans ){
		if( st.count(k) ) continue;
		if( v != cnt[k] ){
			cout << "No\n";
			return 0;
		}
	}
	
	cout << "Yes\n";
	
	return 0;
}

D - Bitmask

?当成是一个二进制数,去二分这个数的大小,判断的时候把二进制数还原会原数就可以比较大小了。

#include <bits/stdc++.h>

using namespace std;

#define int long long

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr) , cout.tie(nullptr);
    string s;
    int n , m = 0;
    cin >> s >> n;

    vector<int> a((int)s.size());

    for( int i = 0 ; i < (int)s.size() ; i ++ ){
        if( s[i] == '?' ) a[i] = -1, m ++;
        else if( s[i] == '1' ) a[i] = 1;
        else a[i] = 0;
    }
    
    auto op = [a]( int x ){
        auto b = a;
        for( int i = b.size()-1 ; i >= 0 ; i -- )
            if( b[i] == -1 ) b[i] = x % 2 , x /= 2;

        int ans = 0;
        for( int i : b )
            ans = ans * 2ll + i;
        return ans;
    };

    int l = 0 , r = (1ll<<m) - 1 , res = -1;
    while( l <= r ){
        int mid = ( l + r ) >> 1;
        int d = op(mid);
        if( d <= n  ) res = d , l = mid + 1;
        else r = mid - 1;
    }
    cout << res << "\n";
    return 0;
}
posted @ 2023-05-22 15:27  PHarr  阅读(22)  评论(0编辑  收藏  举报