Codeforces round 650 (Div. 3) ABCD solution

Title Link:

A. Short SubstringsAdd link description

B. Even ArrayAdd link description

C. Social DistanceAdd link description

D. Task On The BoardAdd link description

A. Short Substrings

Connect all substrings with length 2 of string a to form a new string, and calculate the original string a
Just skip the repeated ones

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
 
int t;
int main()
{
	cin>>t;
	while(t--){
		string a;
		cin>>a;
		cout<<a[0];
		for(int i=1;i<a.size();i++){
			cout<<a[i];
			if(i%2==1) i++;//Remove duplicate
		}
		cout<<endl;
	}
	return 0;
 } 

B. Even Array

Topic meaning: exchange the element position in the array so that the subscript and each element are the same parity. If you can output the exchange times, otherwise output - 1.
Solution: record the number of odd elements (q) and even elements (w) respectively. If q=w, then output Q, anyway - 1.

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a;
int t,n;
int main()
{
cin>>t;
while(t--){
  cin>>n;
  int q=0,w=0;
  for(int i=0;i<n;i++){
  	cin>>a;
  	if(i%2==0&&a%2==1) q++;
	  else if(i%2==1&&a%2==0) w++; 
  }
  if(q==w) cout<<q<<endl;
  else if(q!=w)  cout<<-1<<endl;
 
}
 return 0;
 } 

C. Social Distance

Question meaning: given a binary string, ensure k zeros between each 1, and ask how many ones can be put.
Four situations are discussed:
1. When the current k+1 is zero, add 1 at the first position;
2. If there are 2k+1 zeros in the middle, add 1 in the middle;
3. If there is continuous k+1 or more zeros at the end of the string, add 1 at the end;
4. If the string length is equal to k, the answer is 1;

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a,x;
int t,n,k;
int main()
{
   cin>>t;
while(t--){
    cin>>n>>k;
    int q=0,w=0,c=0;
    char x;
    for(int i=0;i<n;i++){
    	cin>>x;
    	if(x=='0')  w++,q++;
    	else if(x=='1') q=0;
		if(q==k+1&&i==k) c++,q=k;
		//The first situation
		else if(q==2*k+1) c++,q=k;
		//The second situation
	}
	if(q>=k+1) c++;
	//The third situation
	if(w==n&&c==0) c++;
	//The fourth situation
	cout<<c<<endl;
}
 return 0;
 } 

D. Task On The Board

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].
We will construct the string t from the largest letter. Note that if bi=0, then the fourth letter T of the i-string is the largest, so we know that I-this letter affects all bj ≠ 0. However, the string t has not been completed completely. We will do the following:
Find all I to make. bi=0 and the fourth character t of i-string is not placed;
Put all of these positions i in the string t the largest letter not used in the string. T (there should be enough letters in the string) s);
Subtraction | i − j | from all bj ≠ 0

#include<bits/stdc++.h>
using namespace std;
int a[101],q[26];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		string s;
		cin>>s;
		for(int i=0;i<26;++i)
			q[i] = 0;
		for(int i=0;i<s.size();++i)
			q[s[i]-'a']++;
		int n;
		cin>>n;
		for(int i=0;i<n;++i)
			cin>>a[i];
		char ans[n+1];
		int done = 0;
		while(done<n)
		{
			vector<int> zeros;
			for(int i=0;i<n;++i)
				if(a[i]==0)
					zeros.push_back(i),a[i] = -1;
			int ii = -1;
			for(int i=25;ii==-1&&i>=0;q[i--]=0)
				if(q[i]>=zeros.size())
					ii = i;
			for(auto ix:zeros)
			{
				for(int j=0;j<n;++j)
						a[j] -= abs(ix-j);
				ans[ix] = ii+'a';
				done++;
			}
		}
		ans[n] = '\0';
		cout<<ans<<endl;
	}
	return 0;
}


Posted on Wed, 17 Jun 2020 02:45:12 -0400 by akreation