Codeforces Round 921 (Div. 2)

Codeforces Round 921 (Div. 2)

比赛地址

A. We Got Everything Covered!

思路

这个就是一个简单的拼接,这里很容易的发现我们最后最优的方法就是将所要拼写的字母按顺序拼接成n份就好了,但是这里需要主义一下简单的优化

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long


void solve() {
	int n,k;cin>>n>>k;
	string s="";
	for(int i=0;i<k;i++){
		s+=('a'+i);
	}
	// cout<<(s+s)<<endl;
	if(n==1){
		cout<<s<<endl;
		return ;
	}
	else{
		string s1="";

		int n1=n/2;
		for(int i=1;i<=n1;i++){
			s1+=s;
		}
		s1+=s1;
		if(n%2){
			s1+=s;

		}
		cout<<s1<<endl;
		
		return ;
		
	}
	
}

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int t; cin >> t; while(t--)
	solve();
	return 0;
}

B. A Balanced Problemset?

思路

如何找到最优的分组,事实上我们只要找到距离n最近且比n大的x的因子就可以了,我们可以利用折半查找,因为一个因子是一对一对出现的

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long


void solve() {
	int x,n;
	cin>>x>>n;
	if(x%n==0){
		cout<<x/n<<endl;
		return ;
	}
	int res=0;
	int minn=0x3f3f3f3f;
	for(int i=1;i<=sqrt(x);i++){
		// cout<<x/i<<endl;
		
		if(x%i==0){
			// cout<<i<<" "<<x/i<<endl;
			
			if(i>n){
				if(minn>(i-n)){
					res=i;
					minn=i-n;
				}
			}
			if((x/i)>n){
				if(minn>(x/i-n)){
					res=x/i;
					minn=x/i-n;
					
				}
			}
		}
		// cout<<minn<<" ";
		
	}
	// cout<<res<<endl;
	
	// while(x%n!=0){
	// 	n+=1;
	// 	// cout<<n<<endl;
		
	// }
	// cout<<n<<endl;
	
	cout<<x/res<<endl;
	
	
}

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int t; cin >> t; while(t--)
	solve();
	return 0;
}

C. Did We Get Everything Covered?

思路

这个就是A题的一个逆向思考,我们如何判断给定的字符串是不是合法的,就需要考虑能不能组成由n组k个字母的排列就可以了,如果>=n就是可以的,否则直接查找最后少哪些字母就可以

Code

#include <bits/stdc++.h>

#define ios cin.tie(nullptr)->sync_with_stdio(false)
#define endl '\n'
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, b, a) for (int i = (b); i >= (a); --i)
#define int long long
using namespace std;

void solve() {
    int n, k, m;
    string s;
    cin >> n >> k >> m >> s;
    set<char> st;
    int cnt = 0;
    string ans;
    rep(i, 0, m - 1) {
        st.insert(s[i]);
        if (ssize(st) == k) cnt++, ans += s[i], st.clear();
    }

    if (cnt >= n) cout << "YES" << endl;
    else {
        cout << "NO" << endl;
        rep(i, 0, k - 1) {
            if (st.find('a' + i)==st.end()) {
                ans += string(n - cnt, 'a' + i);
                cout << ans << endl;
                return;
            }
        }
    }
}

signed main() {
    ios;
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

posted @ 2024-01-28 12:07  du463  阅读(99)  评论(0编辑  收藏  举报