Codeforces Round #527 (Div. 3)A B C D1 D2

A. Uniform String

题意:给出 n , k n,k ,输出长度为 n n 满足循环节的字符串。比如 k = 3 k = 3 ,就是 a b c a b c abcabc ,不足循环节长度的也要输出。

代码

#include<bits/stdc++.h>

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int T, n, k;
	cin >> T;
	while(T--) {
		cin >> n >> k;
		int cnt = n / k, t = 0;
		if(n % k != 0)
			t = n % k;
		for(int i = 0; i < k; ++i) {
			if(i == k - 1) 
				cnt += t;
			for(int j = 0; j < cnt; ++j) {
				putchar(i + 'a');
			}
		}
		puts("");
	}
    return 0;
}

B. Teams Forming

题意:给你一个数量为偶数的数列,问花费多少让他们两两成对差值最少。
题解:排序后,相邻两项做差明显是花费最少的。

代码

#include<bits/stdc++.h>

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int n, a[200] = {0}, book[200] = {0};
	cin >> n;
	for(int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a, a + n);
	int sum = 0;
	for(int i = 1; i < n; i += 2) {
		sum += a[i] - a[i - 1];
	}
	cout << sum << endl;
    return 0;
}

C. Prefixes and Suffixes

题意:不按顺序给出 2 n 1 2 \cdot n - 1 个长度为 1 1 n 1 n-1 的前缀和后缀子串,最后问你哪些是前缀哪些是后缀。
题解:由题可知,每个长度都有2个串,所以我们先从长度为 n 1 n-1 的子串假设其中一个为前缀,然后去根据长度枚举其它子串看是否满足你当前选择的这个最长前缀和最长后缀。如果当前情况不满足,就去交换一下再去枚举。

代码

#include<bits/stdc++.h>

using namespace std;
int n;
string s[202];
char ans[204];

bool solve(string pre,string suf)
{
	memset(ans, 0, sizeof ans);
	for(int i = 1; i <= n; ++i) {
		bool f = 1;
		for(int j = 0; j < 2 * n - 2; ++j) {
			if(i == s[j].size()) {
				if(pre.substr(0,i) == s[j] && f) {
					ans[j] = 'P';
					f = 0;
				}else if(suf.substr(n - i - 1, i) == s[j]){
					ans[j] = 'S';
				}
				if(!ans[j]) return 0;
			}
		}
	}
	return 1;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	cin >> n;
	string suf,pre;
	for(int i = 0; i < 2 * n - 2; ++i) {
		cin >> s[i];
		if(s[i].size() == n - 1) {
			if(!pre.size()) {
				pre = s[i];
			}else{
				suf = s[i];
			}
		}
	}
	if(solve(suf,pre)) {
		puts(ans);
	}else if(solve(pre,suf)){
		puts(ans);
	}

    return 0;
}

D1. Great Vova Wall (Version 1)

题意:给 n n 列瓦片,有 1 2 1*2 2 1 2*1 的瓦片可以用,问最后能否拼成 a i &gt; = m a x ( H ) a_i &gt;= max(H)
题解:只要看相邻两列的数量差是否满足 2 2 的倍数即可。满足就消去,最后看剩余的数量是否大于 1 1 。栈模拟。

代码

#include<bits/stdc++.h>

using namespace std;

stack< int > st;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int n,x;
	cin >> n >> x;
	st.push(x);
	for(int i = 1; i < n; ++i) {
		cin >> x;
		if(!st.empty() && (st.top() - x)% 2 == 0) {
			st.pop();
		}else{
			st.push(x);
		}
	}
	puts(st.size() > 1 ? "NO" : "YES");
    return 0;
}

D2. Great Vova Wall (Version 2)

题意:同 D 1 D1 ,只不过可供选择的瓦片只剩下 2 1 2 * 1
题解:因为只剩下 2 1 2*1 ,所以必须相邻两列高度相等才可以消去,同样栈模拟。但是要注意 1221 1221 这种情况。

代码

#include<bits/stdc++.h>

using namespace std;

stack<int> st;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
#endif
    int n, x, max1 = 0;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        max1 = max(max1, x);
        if (!st.empty()) {
            if (st.top() < x) return puts("NO"), 0;
            if (st.top() == x)
                st.pop();
            else
                st.push(x);
        } else {
            st.push(x);
        }
    }
    if (st.size() > 1 || (st.size() == 1 && st.top() < max1)) puts("NO");
    else puts("YES");
    return 0;
}


发布了219 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Eternally831143/article/details/85203442
今日推荐