其他分享
首页 > 其他分享> > DAY3 CF重现

DAY3 CF重现

作者:互联网

CF重现赛

A. Two distinct points

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two segments [l1;r1][l1;r1] and [l2;r2][l2;r2] on the xx-axis. It is guaranteed that l1<r1l1<r1 and l2<r2l2<r2. Segments may intersect, overlap or even coincide with each other.

The example of two segments on the xx-axis.
Your problem is to find two integers aa and bb such that l1≤a≤r1l1≤a≤r1, l2≤b≤r2l2≤b≤r2 and a≠ba≠b. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment [l1;r1][l1;r1] and the second one belongs to the segment [l2;r2][l2;r2].

It is guaranteed that the answer exists. If there are multiple answers, you can print any of them.

You have to answer qq independent queries.

Input
The first line of the input contains one integer qq (1≤q≤5001≤q≤500) — the number of queries.

Each of the next qq lines contains four integers l1i,r1i,l2il1i,r1i,l2i and r2ir2i (1≤l1i,r1i,l2i,r2i≤109,l1i<r1i,l2i<r2i1≤l1i,r1i,l2i,r2i≤109,l1i<r1i,l2i<r2i) — the ends of the segments in the ii-th query.

Output
Print 2q2q integers. For the ii-th query print two integers aiai and bibi — such numbers that l1i≤ai≤r1il1i≤ai≤r1i, l2i≤bi≤r2il2i≤bi≤r2i and ai≠biai≠bi. Queries are numbered in order of the input.

It is guaranteed that the answer exists. If there are multiple answers, you can print any.

Example
inputCopy
5
1 2 1 2
2 6 3 4
2 4 1 3
1 2 1 3
1 4 5 8
outputCopy
2 1
3 4
3 2
1 2
3 7

思路&过程

在区间枚举,不同就结束

#include<iostream>
 using namespace std;
 int main()
 {
 	long long n,l1,l2,r1,r2,i,j,k;
 	bool t;
	cin>>n;
	for(k=1;k<=n;k++)
	{
		cin>>l1>>r1>>l2>>r2;
		t=true;
		i=l1;
		while(t&&(i<=r1)){
			j=l2;
			while(t&&(j<=r2))
			{
				if(i!=j) 
				{
					t=false;
					cout<<i<<" "<<j<<endl;
				}
				j=j+1;
			}
			i=i+1;
		}
	}
	return 0;
 }

B. Divisors of Two Integers

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently you have received two positive integer numbers xx and yy. You forgot them, but you remembered a shuffled list containing all divisors of xx (including 11 and xx) and all divisors of yy (including 11 and yy). If dd is a divisor of both numbers xx and yy at the same time, there are two occurrences of dd in the list.

For example, if x=4x=4 and y=6y=6 then the given list can be any permutation of the list [1,2,4,1,2,3,6][1,2,4,1,2,3,6]. Some of the possible lists are: [1,1,2,4,6,3,2][1,1,2,4,6,3,2], [4,6,1,1,2,3,2][4,6,1,1,2,3,2] or [1,6,3,2,4,1,2][1,6,3,2,4,1,2].

Your problem is to restore suitable positive integer numbers xx and yy that would yield the same list of divisors (possibly in different order).

It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers xx and yy.

Input
The first line contains one integer nn (2≤n≤1282≤n≤128) — the number of divisors of xx and yy.

The second line of the input contains nn integers d1,d2,…,dnd1,d2,…,dn (1≤di≤1041≤di≤104), where didi is either divisor of xx or divisor of yy. If a number is divisor of both numbers xx and yy then there are two copies of this number in the list.

Output
Print two positive integer numbers xx and yy — such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.

Example
inputCopy
10
10 2 8 1 2 4 1 20 4 5
outputCopy
20 8

思路&过程

找到最大那个就是答案之一,之后再从大往小搜,第一个不是它的因子的就是另一个

#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
	int n,a[200],i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	bool t=true;
	i=n;
	while((i>=1)&&t)
	{
		if(a[i-1]==a[i]||a[n]%a[i]!=0)
		{
			cout<<a[n]<<" "<<a[i];
			t=false;
		}
		i=i-1;
	}
	return 0;
}

C. Nice Garland

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ii-th lamp is sisi (‘R’, ‘G’ and ‘B’ — colors of lamps in the garland).

You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.

A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is tt, then for each i,ji,j such that ti=tjti=tj should be satisfied |i−j| mod 3=0|i−j| mod 3=0. The value |x||x| means absolute value of xx, the operation x mod yx mod y means remainder of xx when divided by yy.

For example, the following garlands are nice: “RGBRGBRG”, “GB”, “R”, “GRBGRBG”, “BRGBRGB”. The following garlands are not nice: “RR”, “RGBG”.

Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.

Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of lamps.

The second line of the input contains the string ss consisting of nn characters ‘R’, ‘G’ and ‘B’ — colors of lamps in the garland.

Output
In the first line of the output print one integer rr — the minimum number of recolors needed to obtain a nice garland from the given one.

In the second line of the output print one string tt of length nn — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.

Examples
inputCopy
3
BRB
outputCopy
1
GRB
inputCopy
7
RGBGRBB
outputCopy
3
RGBRGBR

思路&过程

前三个定了,后面次序唯一。穷举前三个单词可能的排列,找到次数最少的那一种。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char a[6][4]={"RGB","RBG","BRG","BGR","GRB","GBR"};
long long n,m,i,j,k,ans,g,minn=1e8;
char str[200010];

int f(char *a)
{
	int c=0,gg=0,i,j;
	for(i=0;i<n;i++)
	{
		if(str[i]!=a[gg]) c=c+1;
		gg=(gg+1)%3;
	}
	return c;
}

int main()
{
	cin>>n;
	cin>>str;
	for(i=0;i<6;i++)
	{
		if(f(a[i])<minn)
		{
			minn=f(a[i]);
			ans=i;
		}
	}
	cout<<minn<<endl;
	int g=0;
	for(i=0;i<n;i++)
	{
		cout<<a[ans][g];
		g=(g+1)%3;
	}
	return 0;
}

D. Diverse Garland

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ii-th lamp is sisi (‘R’, ‘G’ and ‘B’ — colors of lamps in the garland).

You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.

A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is 11) have distinct colors.

In other words, if the obtained garland is tt then for each ii from 11 to n−1n−1 the condition ti≠ti+1ti≠ti+1 should be satisfied.

Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.

Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of lamps.

The second line of the input contains the string ss consisting of nn characters ‘R’, ‘G’ and ‘B’ — colors of lamps in the garland.

Output
In the first line of the output print one integer rr — the minimum number of recolors needed to obtain a diverse garland from the given one.

In the second line of the output print one string tt of length nn — a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.

Examples
inputCopy
9
RBGRRBRGG
outputCopy
2
RBGRGBRGR
inputCopy
8
BBBGBRRR
outputCopy
2
BRBGBRGR
inputCopy
13
BBRRRRGGGGGRR
outputCopy
6
BGRBRBGBGBGRG

思路&过程

连续两个一样的话根据下一个变动两个中的后一个,三个一样只要变中间就行。

#include<iostream>
using namespace std;
int main()
{
	int n,m,i,j,sum,ans;
	char s[200010];
	cin>>n;
	cin>>s;
	ans=0;
	for(i=0;i<n-1;i++)
	{
		if(s[i]==s[i+1])
		{
			ans=ans+1;
			if(s[i]=='R')
			{
				if(i+2<n && s[i+2]!='B') s[i+1]='B';
				else s[i+1]='G';
			}
			else if(s[i]=='B')
			{
				if(i+2<n && s[i+2]!='R') s[i+1]='R';
				else s[i+1]='G';
			}
			else if(s[i]=='G')
			{
				if(i+2<n && s[i+2]!='B') s[i+1]='B';
				else s[i+1]='R';
			}
		}
	}
	cout<<ans<<endl;
	cout<<s;
	return 0;
}

后记

总用时一时零三分
Arwen要去吃饭啦,就打一半咯。
没有男朋友的Arwen又只能胡乱解决啦,羡慕舍友,嘤。

标签:number,garland,DAY3,CF,xx,lamps,重现,print,input
来源: https://blog.csdn.net/ArwenNi/article/details/89075159