Codeforces Round #650 (Div. 3)

22 篇文章 0 订阅

A. Short Substrings

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice guesses the strings that Bob made for her.

At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.

Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.

For example, if Bob came up with the string a=“abac”, then all the substrings of length 2 of the string a are: “ab”, “ba”, “ac”. Therefore, the string b=“abbaac”.

You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.

Input
The first line contains a single positive integer t (1≤t≤1000) — the number of test cases in the test. Then t test cases follow.

Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2≤|b|≤100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.

Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.

Example
inputCopy
4
abbaac
ac
bccddaaf
zzzzzzzzzz
outputCopy
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.

In the second test case, Bob came up with the string a=“ac”, the string a has a length 2, so the string b is equal to the string a.

In the third test case, Bob came up with the string a=“bcdaf”, substrings of length 2 of string a are: “bc”, “cd”, “da”, “af”, so the string b=“bccddaaf”.

#include<iostream>
#include<cstring>
#include<cstdio>
#define Maxn 1005
using namespace std;
char s[Maxn];

int main(int argc,char* argv[])  {
	int T; scanf("%d",&T);
	while(T--) {
		scanf("%s",s + 1);
		int len = strlen(s + 1);
		printf("%c",s[1]);
		for(int i=2; i<=len-1; i+=2) printf("%c",s[i]);
		printf("%c\n",s[len]);
	}
	
	return 0;
}

B. Even Array

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a[0…n−1] of length n which consists of non-negative integers. Note that array indices start from zero.

An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0≤i≤n−1) the equality imod2=a[i]mod2 holds, where xmod2 is the remainder of dividing x by 2.

For example, the arrays [0,5,2,1] and [0,17,0,3] are good, and the array [2,4,6,7] is bad, because for i=1, the parities of i and a[i] are different: imod2=1mod2=1, but a[i]mod2=4mod2=0.

In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).

Find the minimum number of moves in which you can make the array a good, or say that this is not possible.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases in the test. Then t test cases follow.

Each test case starts with a line containing an integer n (1≤n≤40) — the length of the array a.

The next line contains n integers a0,a1,…,an−1 (0≤ai≤1000) — the initial array.

Output
For each test case, output a single integer — the minimum number of moves to make the given array a good, or -1 if this is not possible.

Example
inputCopy
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
outputCopy
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.

In the second test case, in the first move, you need to swap the elements with indices 0 and 1.

In the third test case, you cannot make the array good.

#include<iostream>
#include<cstring>
#include<cstdio>
#define Maxn 10005
using namespace std;
int a[Maxn];
int main(int argc,char* argv[]) {
	int T,n; scanf("%d",&T);
	while(T--) {
		scanf("%d",&n);
		for(int i=0; i<n; i++) scanf("%d",&a[i]);
		int cnt1 = 0,cnt2 = 0;
		for(int i=0; i<n; i++) {
			if((a[i] & 1) == (i & 1)) continue;
			if(i & 1) cnt1++;//下标是奇数  ai是偶数 
			else if(a[i] & 1) cnt2++;//下标是偶数  ai是奇数 
		}
		if(!cnt1 && !cnt2) printf("0\n");
		else if(cnt1 != cnt2) printf("-1\n");
		else printf("%d\n",cnt1); 
	}
	
	return 0;
}

C. Social Distance

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters “1” (the table is occupied) and “0” (the table is empty).

Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i−k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k.

For example, if n=8 and k=2, then:

strings “10010001”, “10000010”, “00000000”, “00100000” satisfy the rules of the restaurant;
strings “10100100”, “10011001”, “11111111” do not satisfy to the rules of the restaurant, since each of them has a pair of “1” with a distance less than or equal to k=2.
In particular, if the state of the restaurant is described by a string without “1” or a string with one “1”, then the requirement of the restaurant is satisfied.

You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s.

Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of “0” that can be replaced by “1” such that the requirement will still be satisfied?

For example, if n=6, k=1, s= “100010”, then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases in the test. Then t test cases follow.

Each test case starts with a line containing two integers n and k (1≤k≤n≤2⋅105) — the number of tables in the restaurant and the minimum allowed distance between two people.

The second line of each test case contains a binary string s of length n consisting of “0” and “1” — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two “1” is more than k.

The sum of n for all test cases in one test does not exceed 2⋅105.

Output
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0.

Example
inputCopy
6
6 1
100010
6 2
000000
5 1
10101
3 1
001
2 2
00
1 1
0
outputCopy
1
2
0
1
1
1
Note
The first test case is explained in the statement.

In the second test case, the answer is 2, since you can choose the first and the sixth table.

In the third test case, you cannot take any free table without violating the rules of the restaurant.

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#define maxn 3100

using namespace std;

const int Maxn = 0x3f3f3f3f;
typedef long long LL;
const int Mod = 1000007;
string s;
const int MinN = 0xc0c0c00c;

int main(int argc,char* argv[]) {
	int T,k,n;  scanf("%d",&T);
	while(T--) {
		scanf("%d%d",&n,&k);
		cin >> s;
		for(int i=0; i<n; i++) {
			if(s[i] == '1') {
				 for(int j=max(0,i - k); j<=min(n - 1,i + k); j++) s[j] = '#';
			}
		}
		int Ans = 0;
		for(int i=0; i<n; i++) 
			if(s[i] == '0') {
				Ans ++;
				i += k;
			} 
		printf("%d\n", Ans);
		
	}
	
	
	return 0;
}

D. Task On The Board

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp wrote on the board a string s containing only lowercase Latin letters (‘a’-‘z’). This string is known for you and given in the input.

After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some additional information.

Suppose that the string t has length m and the characters are numbered from left to right from 1 to m. You are given a sequence of m integers: b1,b2,…,bm, where bi is the sum of the distances |i−j| from the index i to all such indices j that tj>ti (consider that ‘a’<‘b’<…<‘z’). In other words, to calculate bi, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than ti and sums all the values |i−j|.

For example, if t = “abzb”, then:

since t1=‘a’, all other indices contain letters which are later in the alphabet, that is: b1=|1−2|+|1−3|+|1−4|=1+2+3=6;
since t2=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b2=|2−3|=1;
since t3=‘z’, then there are no indexes j such that tj>ti, thus b3=0;
since t4=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b4=|4−3|=1.
Thus, if t = “abzb”, then b=[6,1,0,1].

Given the string s and the array b, find any possible string t for which the following two requirements are fulfilled simultaneously:

t is obtained from s by erasing some letters (possibly zero) and then writing the rest in any order;
the array, constructed from the string t according to the rules above, equals to the array b specified in the input data.
Input
The first line contains an integer q (1≤q≤100) — the number of test cases in the test. Then q test cases follow.

Each test case consists of three lines:

the first line contains string s, which has a length from 1 to 50 and consists of lowercase English letters;
the second line contains positive integer m (1≤m≤|s|), where |s| is the length of the string s, and m is the length of the array b;
the third line contains the integers b1,b2,…,bm (0≤bi≤1225).
It is guaranteed that in each test case an answer exists.

Output
Output q lines: the k-th of them should contain the answer (string t) to the k-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any.

Example
inputCopy
4
abac
3
2 1 0
abc
1
0
abba
3
1 0 1
ecoosdcefr
10
38 13 24 14 11 5 3 24 17 0
outputCopy
aac
b
aba
codeforces
Note
In the first test case, such strings t are suitable: "aac’, “aab”.

In the second test case, such trings t are suitable: “a”, “b”, “c”.

In the third test case, only the string t equals to “aba” is suitable, but the character ‘b’ can be from the second or third position.
题目大意:
给出一个小写字符串s,和一个长为n的整数数组b,问能否从s字符串中挑出n个字母组成一个字符串,该字符串需要满足b[i]等于第i个字母与所有比第i个字母大的字符的 距离之和。
题目分析:
b[i]一定存在值为0,因为最大字母的b[i]=0,所以不断模拟,一直寻找b[i]=0的字母

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<iostream>
#define Maxn 66
using namespace std;
char s[Maxn],str[Maxn];
int b[Maxn],vis[Maxn],cnt[Maxn];
int n;
// 无限循环 去找  bi == 0 
int main(int argc,char* argv[]) {
	int T; scanf("%d",&T);
	while(T--) {
		memset(vis,0,sizeof(vis));
		memset(cnt,0,sizeof(cnt));
		scanf("%s",s + 1);	
		int len = strlen(s + 1);
		for(int i=1; i<=len; i++) cnt[s[i] - 'a'] ++;
		
		scanf("%d",&n);
		for(int i=1; i<=n; i++) scanf("%d",&b[i]);
		
		while(true) {
			int flag = 1;
			vector<int> v;
			for(int i=1; i<=n; i++) {
				if(!b[i] && !vis[i]) {
					v.push_back(i);
					vis[i] = 1; flag = 0;
				}
			}
			int index;
			for(int i=25; i>=0; i--) {// 找一个最大的字母 
				if(cnt[i]) {
					if(cnt[i] >= v.size()){
						index = i; cnt[i] = 0;
						break;
					} 
					else {
						cnt[i] = 0;
						continue;
					}
				}
			}
			while(v.size()) {
				
				for(int i=1; i<=n; i++)
					if(b[i]) {
						b[i] -= abs(i - v.back());
					}
				str[v.back()] = index + 'a';
				v.pop_back();
			}
			if(flag) break;
			
		}
		for(int i=1; i<=n; i++) printf("%c",str[i]);
		printf("\n");
	}
	
	
	return 0;
}

E. Necklace Assembly

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The store sells n beads. The color of each bead is described by a lowercase letter of the English alphabet (“a”–“z”). You want to buy some beads to assemble a necklace from them.

A necklace is a set of beads connected in a circle.

For example, if the store sells beads “a”, “b”, “c”, “a”, “c”, “c”, then you can assemble the following necklaces (these are not all possible options):
在这里插入图片描述

And the following necklaces cannot be assembled from beads sold in the store:
在这里插入图片描述
The first necklace cannot be assembled because it has three beads “a” (of the two available). The second necklace cannot be assembled because it contains a bead “d”, which is not sold in the store.
We call a necklace k-beautiful if, when it is turned clockwise by k beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace.
在这里插入图片描述
As you can see, this necklace is, for example, 3-beautiful, 6-beautiful, 9-beautiful, and so on, but it is not 1-beautiful or 2-beautiful.
In particular, a necklace of length 1 is k-beautiful for any integer k. A necklace that consists of beads of the same color is also beautiful for any k.

You are given the integers n and k, and also the string s containing n lowercase letters of the English alphabet — each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a k-beautiful necklace you can assemble.

Input
The first line contains a single integer t (1≤t≤100) — the number of test cases in the test. Then t test cases follow.

The first line of each test case contains two integers n and k (1≤n,k≤2000).

The second line of each test case contains the string s containing n lowercase English letters — the beads in the store.

It is guaranteed that the sum of n for all test cases does not exceed 2000.

Output
Output t answers to the test cases. Each answer is a positive integer — the maximum length of the k-beautiful necklace you can assemble.

Example
inputCopy
6
6 3
abcbac
3 6
aaa
7 1000
abczgyo
5 4
ababa
20 10
aaebdbabdbbddaadaadc
20 5
ecbedececacbcbccbdec
outputCopy
6
3
5
4
15
10
Note
The first test case is explained in the statement.

In the second test case, a 6-beautiful necklace can be assembled from all the letters.

In the third test case, a 1000-beautiful necklace can be assembled, for example, from beads “abzyo”.

题目大意:
给出n的珠子,要求组成一个以k为周期的字符串,并且这个字符串尽量长
题目分析
要求构造周期为k的字符串 那么以k的因子为周期构造 k也是周期,那么思路之一是枚举周期,(好像也可以二分周期)

#include<iostream>
#include<cstring>
#include<cstdio> 
#define Maxn 2005
using namespace std;
char s[Maxn];

int gcd(int a,int b) {
	if(b == 0) return a;
	else return gcd(b,a % b);
}
// 要求构造周期为k的字符串  那么以k的因子为周期构造  k也是周期 
int main(int argc,char* argv[]) {
	int cnt[Maxn];
	int T; scanf("%d",&T);
	int n,k,Ans,block,l,Num;
	while(T--) {
		Ans = 0,Num = 0;
		memset(cnt,0,sizeof(cnt));
		scanf("%d %d",&n,&k);
		scanf("%s",s);
		for(int i=0; i<n; i++) cnt[s[i] - 'a']++;
		for(int l=1; l<=n; l++) {
			int cycle = gcd(l,k);// 周期  长度除以周期==分块数量(block) 
			block = l / cycle; Num = 0;
			for(int i=0; i<26; i++)
				Num += cnt[i] / block;
			if(Num >= cycle) Ans = l;
		} 
		printf("%d\n",Ans);
	}
	
	
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值