传送门:https://codeforces.com/contest/1092
目录
A
B
C
D1
D2
A. Uniform String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two integers nn and kk.
Your task is to construct such a string ss of length nn that for each ii from 11 to kk there is at least one ii-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.
You have to answer tt independent queries.
Input
The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of queries.
The next tt lines are contain queries, one per line. The ii-th line contains two integers nini and kiki (1≤ni≤100,1≤ki≤min(ni,26)1≤ni≤100,1≤ki≤min(ni,26)) — the length of the string in the ii-th query and the number of characters in the ii-th query.
Output
Print tt lines. In the ii-th line print the answer to the ii-th query: any string sisi satisfying the conditions in the problem statement with constraints from the ii-th query.
Example
input
Copy
3
7 3
4 4
6 2
output
Copy
cbcacab
abcd
baabab
Note
In the first example query the maximum possible minimal frequency is 22, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).
In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is 11).
In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is 33).
AB过于简单,直接上代码吧
#include
using namespace std;
const int MAXN=2e5+7;
typedef long long ll;
ll t,x,y,num;
ll a[MAXN];
int cmp(ll a,ll b) {
return b < a;
}
int main() {
string ans = "";
for(int i = 0; i < 26; i++) ans += i + 'a';
while(cin >> t) {
while(t--) {
int n,k;
cin >> n >> k;
string str = "";
for(int i = 0; i < n; i++) {
str += ans[i % k];
}
cout << str << endl;
}
}
return 0;
}
B. Teams Forming
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn students in a university. The number of students is even. The ii-th student has programming skill equal to aiai.
The coach wants to form n2n2 teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal (otherwise they cannot understand each other and cannot form a team).
Students can solve problems to increase their skill. One solved problem increases the skill by one.
The coach wants to know the minimum total number of problems students should solve to form exactly n2n2 teams (i.e. each pair of students should form a team). Your task is to find this number.
Input
The first line of the input contains one integer nn (2≤n≤1002≤n≤100) — the number of students. It is guaranteed that nn is even.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100), where aiai is the skill of the ii-th student.
Output
Print one number — the minimum total number of problems students should solve to form exactly n2n2 teams.
Examples
input
Copy
6
5 10 2 3 14 5
output
Copy
5
input
Copy
2
1 100
output
Copy
99
Note
In the first example the optimal teams will be: (3,4)(3,4), (1,6)(1,6) and (2,5)(2,5), where numbers in brackets are indices of students. Then, to form the first team the third student should solve 11 problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve 44 problems so the answer is 1+4=51+4=5.
In the second example the first student should solve 9999 problems to form a team with the second one.
#include
using namespace std;
const int MAXN=2e5+7;
typedef long long ll;
ll t,x,y,num;
ll a[MAXN];
int cmp(ll a,ll b) {
return b < a;
}
int main() {
int n;
while(cin >> n) {
for(int i = 1;i <= n;i++) cin >> a[i];
sort(a + 1, a + 1 + n);
ll ans = 0;
for(int i = 1;i < n;i += 2){
ans += abs(a[i] - a[i + 1]);
}
cout << ans << endl;
}
return 0;
}
C. Prefixes and Suffixes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ivan wants to play a game with you. He picked some string ss of length nn consisting only of lowercase Latin letters.
You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from 11 to n−1n−1), but he didn't tell you which strings are prefixes and which are suffixes.
Ivan wants you to guess which of the given 2n−22n−2 strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!
Input
The first line of the input contains one integer number nn (2≤n≤1002≤n≤100) — the length of the guessed string ss.
The next 2n−22n−2 lines are contain prefixes and suffixes, one per line. Each of them is the string of length from 11 to n−1n−1 consisting only of lowercase Latin letters. They can be given in arbitrary order.
It is guaranteed that there are exactly 22 strings of each length from 11 to n−1n−1. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length nn.
Output
Print one string of length 2n−22n−2 — the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The ii-th character of this string should be 'P' if the ii-th of the input strings is the prefix and 'S' otherwise.
If there are several possible answers, you can print any.
Examples
input
Copy
5
ba
a
abab
a
aba
baba
ab
aba
output
Copy
SPPSPSPS
input
Copy
3
a
aa
aa
a
output
Copy
PPSS
input
Copy
2
a
c
output
Copy
PS
Note
The only string which Ivan can guess in the first example is "ababa".
The only string which Ivan can guess in the second example is "aaa". Answers "SPSP", "SSPP" and "PSPS" are also acceptable.
In the third example Ivan can guess the string "ac" or the string "ca". The answer "SP" is also acceptable.
C题就有点意思了,给你一个长度n,以及2 * n - 2个字符串,代表着字符串s(1到n - 1)的前缀和后缀,现在要你根据前缀和后缀构造字符串s,对于第i个字符串,输出这个字符串是前缀(P)或是后缀(S)
首先我们对于n-1的后缀和前缀,就可以确定一个字符串s,然后对于n - 1的前缀和后缀,确定他们作为前缀 还是 后缀,那么每次可以拿n-i的串分别和n-1的串进行比较(判断当前字符串是n-1的前缀还是后缀),即可确定剩下的字符串是前缀还是后缀。
代码如下:(这里判断前后缀的时候自己写的麻烦了一些,其实还要一个小trick,如果一个串可以当前缀又可以做后缀,那我们就应该去判断和他长度相同的另外一个串,再决定他们是后缀还是前缀,道理很简单,如果给当前串,随便分配一个前缀或者后缀,和他长度相同的串是不一定满足条件的,所以都需要进行判断。)
#include
using namespace std;
const int MAXN=2e5+7;
typedef long long ll;
string str[200];
typedef pair P;
vector >v;
map,int>m;
bool check(string str1,string str2) {
int len = str2.size();
for(int i = 0; i < len; i++)
if(str1[i] != str2[i]) return false;
return true;
}
bool check2(string str1,string str2) {
int len = str2.size();
int len1 = str1.size();
for(int i = len - 1,j = len1 - 1; i >= 0 && j >= 0; i--,j--)
if(str2[i] != str1[j]) return false;
return true;
}
string strr;
int n;
bool check3(string str) {
for(int i = n - 2; i >= 1; i--) {
string str1 = v[i][0].first;
string str2 = v[i][1].first;
// cout << str1 << " " <> n) {
m.clear();
v.resize(n + 200);
for(int i = 0; i < 2 * n - 2; i++) {
cin >> strr;
int len = strr.size();
P p = P(strr,i);
v[len].push_back(p);
}
if(n == 2) {
cout << "PS" << endl;
continue;
}
string ans = "";
for(int i = 0; i < 2 * n - 2; i++) ans += "0";
string str1 = v[n - 1][0].first;
string str2 = v[n - 1][1].first;
string qian,hou;
string str3 = str1 + str2[n - 2];
string str4 = str2 + str1[n - 2];
// cout << str3 <= 1; i--) {
string str3 = v[i][0].first;
string str4 = v[i][1].first;
if(check(qian,str3)) {
if(check2(hou,str4)) {
ans[v[i][0].second] = 'P';
ans[v[i][1].second] = 'S';
} else {
ans[v[i][0].second] = 'S';
ans[v[i][1].second] = 'P';
}
} else {
ans[v[i][0].second] = 'S';
ans[v[i][1].second] = 'P';
}
}
cout << ans << endl;
}
return 0;
}
D1. Great Vova Wall (Version 1)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.
The current state of the wall can be respresented by a sequence aa of nn integers, with aiai being the height of the ii-th part of the wall.
Vova can only use 2×12×1 bricks to put in the wall (he has infinite supply of them, however).
Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some ii the current height of part iiis the same as for part i+1i+1, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part 11 of the wall or to the right of part nn of it).
The next paragraph is specific to the version 1 of the problem.
Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.
Vova is a perfectionist, so he considers the wall completed when:
Can Vova complete the wall using any amount of bricks (possibly zero)?
Input
The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of parts in the wall.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial heights of the parts of the wall.
Output
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero).
Print "NO" otherwise.
Examples
input
Copy
5
2 1 1 2 5
output
Copy
YES
input
Copy
3
4 5 3
output
Copy
YES
input
Copy
2
10 10
output
Copy
YES
input
Copy
3
1 2 3
output
Copy
NO
Note
In the first example Vova can put a brick on parts 2 and 3 to make the wall [2,2,2,2,5][2,2,2,2,5] and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it [5,5,5,5,5][5,5,5,5,5].
In the second example Vova can put a brick vertically on part 3 to make the wall [4,5,5][4,5,5], then horizontally on parts 2 and 3 to make it [4,6,6][4,6,6] and then vertically on part 1 to make it [6,6,6][6,6,6].
In the third example the wall is already complete.
D1:给你n个高度,你可以用任意个1 * 2的砖块可以竖、横着摆放,问是否可以摆放成高度相同的一堵墙,高度可以任意
首先我们对于两个高度相同的方块,他们是肯定可以满足的,然后对于两个相差高度差为2的倍数的,可以补成高度相同的,也是可以满足的,那么就可以用栈来模拟这个过程。
代码如下:
#include
using namespace std;
const int MAXN=2e5+7;
typedef long long ll;
string str[200];
typedef pair P;
vector >v;
map,int>m;
int a[MAXN];
int n;
int main() {
while(cin >> n) {
for(int i = 1; i <= n; i++) cin >> a[i];
stacks;
ll ans = 0;
for(int i = 1; i <= n; i++) {
if(s.empty()) s.push(a[i]);
else {
if(s.top() == a[i]) {
while(!s.empty() && a[i] == s.top()) s.pop();
}else if(abs(s.top() - a[i]) %2 == 0)
while(!s.empty() &&abs(s.top() - a[i]) %2 == 0) s.pop();
else s.push(a[i]);
}
}
if(s.size() == 1 || s.size() == 0) {
cout << "YES" << endl;
} else cout << "NO" <
和D1一样的题意,但是只能横着放,不可以竖着摆放
这个就需要判断一下特殊的条件,
比如 2 1 1 2 3 ,我们是可以先在中间(1,1)的位置放两个,然后第三层横着放两个达到满足条件的,
再比如1 2 2 1 3 ,很明显,我们的两边的1是被比1高度要高的2隔开的,所以不管怎么摆放也不能满足条件。
再一组例子, 3 2 2 3 1,对于这个例子,我们是可以把前四个摆放满足的,那么对于单独剩下来的1,也不能满足条件。
那么就有几种情况,相邻的两个相同的时候,可以出栈,如果后面入栈的要大于栈顶元素,那么这个肯定就不能满足了,直接“NO”,小于的话可以入栈,最后判断,如果栈空,证明可以满足,如果栈的大小为1,那么就是刚刚思考的第三种情况,我们用保存下来的最大值和他进行比较,如果留下来的这个val是小于最大值的,则不能满足,反之就可以;其他剩下来的情况,就都 不满足。
代码如下:
#include
using namespace std;
const int MAXN=2e5+7;
typedef long long ll;
string str[200];
typedef pair P;
vector >v;
map,int>m;
int a[MAXN];
int n;
int main() {
while(cin >> n) {
int Max = 0;
for(int i = 1; i <= n; i++) cin >> a[i],Max = max(a[i],Max);
stacks;
ll ans = 0;
for(int i = 1; i <= n; i++) {
if(s.empty()) s.push(a[i]);
else {
if(s.top() == a[i]) {
while(!s.empty() && a[i] == s.top()) s.pop();
} else {
if(s.empty()) s.push(a[i]);
else {
if(s.top() > a[i]) {
s.push(a[i]);
} else {
cout << "No" << endl;
return 0;
}
}
}
}
}
if(s.size() == 0) {
cout << "YES" << endl;
}
else if(s.size() == 1) {
if(s.top() >= Max) cout << "YES" << endl;
else{
cout << "NO" <