CF Round # 632 div2 solution to a problem

\(Codeforces\) \(Round\) \(632\)

A. Little Artem

\(Description:\)

\ (n-\) OK \ (m \) column of the table, each grid may be colored black or white
in mind \ (W is \) is no black and white squares lattice surrounding number
referred to \ (B \) is not as white squares around black grid number
in claim constructs a staining protocol, such that \ (B = W + 1 \ )

\(Solution:\)

Primary School Olympiad title
directly to the first row, first column dyed white, others are dyed black
at this time \ (W = 1 \) , \ (B = 2 \)
is clearly the program to form all sizes are established

\(Code:\)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,t;
int main(){
	int i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		printf("W");
		for(i=2;i<=m;i++)printf("B");
		printf("\n");
		for(i=2;i<=n;i++){
			for(j=1;j<=m;j++)
				printf("B");
			printf("\n");
		}
	}
	return 0;
}

B. Kind Anton

\(Description:\)

Given a length \ (n-\) array \ (A_N \) \ ((a_i \ in \ lbrace-1,0,1 \ rbrace) \)
each operation may be \ (A \) of any one of element \ (a_i \) accumulated into its rearward \ (a_j \) a \ ((i <j) \
) after any operations through Q, can \ (a \) into an array of \ (B \)

\(Solution:\)

Because of \ (A \) elements only \ (0, -1 \) are three values, difficult to find the following points:
1. If a number preceded by \ (1 \) , then it can become any a larger than its integer
2. If there is a number in front of \ (--1 \) , then it can become any smaller than its integer
then we need only compare \ (a_i \) and \ (b_i \) of the size of the relationship, and then look at the statistics \ (1 \) and \ (- 1 \) appears the situation can be.

\(Code:\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,t,a[100005],b[100005];
int main(){
	int i,j;
	scanf("%d",&t);
	while(t--){
		int f1=0,f2=0;
		scanf("%d",&n);
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		for(i=1;i<=n;i++)scanf("%d",&b[i]);
		int flag=1;
		for(i=1;i<=n;i++){
			if(f1&&f2)break;
			if(b[i]>a[i]&&f1==0){flag=0;break;}
			if(b[i]<a[i]&&f2==0){flag=0;break;}
			if(a[i]==1)f1=1;
			if(a[i]==-1)f2=1;
		}
		if(flag)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

C. Eugene and an array

\(Description:\)

Given a length \ (n-\) of the array, wherein the request list of all the sub-sub-columns and the number is not zero

\(Solution:\)

Consider two endpoints Sequences \ (L \) and \ (R & lt \) , the following Order \ ((l, r) \ ) represents the left point \ (L \) , the right end point \ (R & lt \) sub column, easy to get the following conclusions:
1. If the current sub-column is not legitimate, then contains all the current sub-sub-column column is not legitimate
2. If the current sub-column legitimate, then it's all child columns are legitimate
3. If \ (( l, r + 1) \) method, which is then compared to the \ ((l, r) \ ) is increased as the number of legal child \ (r-l + 1 \
) thereby obtaining the following algorithm:
1. pretreatment out prefix and then sort, then number to facilitate statistics
2. for the current left and right endpoint \ (l \) and \ (r \) , as will be \ (r \) to the right, until the current sub-columns are not legitimate , update each right answer
3. the \ (L \) to the right, until the current legal sub-array
4. repeat \ (2 \) , \ (3 \)Step down through the complete array
5. Output answer

\(Code:\)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>//kai long long
using namespace std;
typedef long long lol;
int n,a[200005],cnt[200005];
lol ans;
struct node{
	lol sum;
	int x,k;
}b[200005];
bool cmp1(const node a,const node b){return a.sum<b.sum;}
bool cmp2(const node a,const node b){return a.x<b.x;}
int main(){
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(i=1;i<=n;i++){
		b[i].sum=b[i-1].sum+a[i];
		b[i].x=i;
	}
	sort(b,b+n+1,cmp1);
	for(i=0,j=0;i<=n;i++)
		if(i==0||b[i].sum!=b[i-1].sum)b[i].k=++j;
		else b[i].k=j;
	sort(b,b+n+1,cmp2);
	cnt[b[0].k]++;
	i=1;j=0;
	while(1){
		while(cnt[b[j+1].k]==0){
			j++;
			cnt[b[j].k]++;
			ans+=j-i+1;
			if(j>=n)break;
		}
		if(j>=n)break;
		while(cnt[b[j+1].k]){
			cnt[b[i-1].k]--;
			i++;
		}
	}
	printf("%lld\n",ans);
	return 0;
}

D. Challenges in school №41

\(Description:\)

Some children do not listen to see chaos about
the number of children given \ (n-\) , and gives them to see the direction of \ ((L \) \ (or \) \ (R & lt) \)
each operation you can select one or several of the children currently being affectionate adjacent view, and reverse them
asked if he could go through \ (k \) operations so that there is no longer a child affectionately on the TV

\(Solution:\)

Set the direction of each of the two children turned into an adjustment, and all the children to reverse the current state of an operation
is not difficult to find, the number of adjustments are determined, the order does not change with changes
to \ (n-\) a child, does not exceed the operation discussed \ (n-1 \)
then count the number of times an adjustment \ (SUM \) , require several rounds of operation \ (max \) , and record each adjustment position
if \ (sum \ geq k \) and \ (K \ GEQ max \) , then the solvability must output a set of solutions to
other cases no solution

\(Code:\)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,ans[3005][3005],cnt[3005],sum,mmax;
char s[3005];
int main(){
	int i,j,k,len,o;
	scanf("%d%d",&n,&m);
	scanf("%s",s+1);
	len=strlen(s+1);
	for(i=1;i<n;i++){
		j=1;
		while(j<=n){
			if(s[j]=='R'&&s[j+1]=='L'){
				swap(s[j],s[j+1]);
				ans[i][++cnt[i]]=j;
				j+=2;
			}
			else j++;
		}
		sum+=cnt[i];
		if(cnt[i])mmax=i;
	}
	if(sum<m||m<mmax){
		printf("-1\n");
		return 0;
	}
	else{
		int need=sum-m;
		j=1;k=1;
		for(i=1;i<=m;i++){
			if(need==0){
				printf("%d %d\n",1,ans[j][k]);
				k++;
				if(k>cnt[j]){j++;k=1;}
			}
			else{
				int s=min(need,cnt[j]-k);
				need-=s;
				printf("%d ",s+1);
				for(k,o=k+s;k<=o;k++)printf("%d ",ans[j][k]);
				printf("\n");
				if(k>cnt[j]){j++;k=1;}
			}
		}
	}
	return 0;
}

E. Road to 1600

\(Description:\)

Construct a \ (n * n \) board that read digital \ (1 \) ~ \ (n * n \) , then there is a "vehicle" a "post", the pieces are moving to follow the following rules:
1 . If \ (1 \) step of moving up the grid has not traversed, the non-selected grid traversed digital mobile smallest lattice.
2. If \ (1 \) step of moving up the grid are traversed all, if there are not lattice traversal, the traversal is not selected digital transmission grating of the grating minimal.
Pieces beginning in \ (1 \) number grid, and deemed to have been traversed \ (1 \) number grid.
Chessboard structure is such that the number of transmission "car" is strictly less than the number of transmission "after" the.

\(Solution:\)

\ (n \ leq 2 \) , the apparently no solution, since in this case, "rear" of the transfer count is 0, other cases discussed below
taking into account the "rear" and "car" except that "rear" can be moved take the diagonal direction, we can get the general idea:
"after" with the number of the oblique direction "after" lured away, so that finally need to reach the finish line by transfer, and "car" without
(this konjac can only think of the roughly ideas, and the AC code reference code on the CF, construction is amazing, and so the official explanation of it)

\(Code:\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,a[505][505];
int main(){
	int i,j;
	scanf("%d",&n);
	if(n<=2){
		printf("-1\n");
		return 0;
	}
	a[1][n-1]=n*n;
	for(i=1;i<n;i++)
		a[i][n]=n*n-i-1;
	a[n][n]=n*n-1;
	int cnt=0;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			if(!a[i][j])a[i][j]=++cnt;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++)
			printf("%d ",a[i][j]);
		printf("\n");
	}
	return 0;
}

F. Kate and imperfection

\(Description:\)

Given a number \ (n-\) , output \ (n-1 \) the number
of \ (I \) number represents the interval \ ([1, n] \ ) selecting one of the \ (I \) Element group, so that \ (I \) between the number twenty-two \ (GCD \) maximum minimum, this minimum output

\(Solution:\)

Such a configuration set, to make the maximum value of gcd minimum, first of all put into them all primes, this minimum value is 1
then put into such becomes minimum number of 2, 3, 4 and the like and then
if we go into a composite number, then it's all about numbers in the collection must have been
difficult to find this is a Erichsen sieve process, so maintain it by Erichsen sieving the greedy
and after sorting output to

\(Code:\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int isprime[500005],size,prime[500005],ans[500005];
int main(){
	int n,i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++)ans[i]=1;
	for(i=2;i<=n;i++)
		for(j=i*2;j<=n;j+=i)
			ans[j]=i;
	sort(ans+1,ans+n+1);
	for(i=2;i<=n;i++)printf("%d ",ans[i]);
	printf("\n");
	return 0;
}

Brbt

T1 a primary school Olympiad title actually wanted to ten minutes
T3 yard most of the day did not come out code, the code also thief ugly, like a mentally handicapped as
T4 can actually wrong questions, ten minutes to do the next morning out
T5T6 to read a title. . .
He retired two years OI players can really dish mercilessly chant ~

Guess you like

Origin www.cnblogs.com/huangdalaofighting/p/12669111.html