Codeforces Round#535(Div.3)A,B,C,D题解报告

传送门:https://codeforces.com/problemset/problem/1108/A
A. Two distinct points
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two segments [?1;?1] and [?2;?2] on the ?-axis. It is guaranteed that ?1<?1 and ?2<?2. Segments may intersect, overlap or even coincide with each other.

The example of two segments on the ?-axis.
Your problem is to find two integers ? and ? such that ?1≤?≤?1, ?2≤?≤?2 and ?≠?. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment [?1;?1] and the second one belongs to the segment [?2;?2].

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

You have to answer ? independent queries.

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

Each of the next ? lines contains four integers ?1?,?1?,?2? and ?2? (1≤?1?,?1?,?2?,?2?≤109,?1?<?1?,?2?<?2?) — the ends of the segments in the ?-th query.

Output
Print 2? integers. For the ?-th query print two integers ?? and ?? — such numbers that ?1?≤??≤?1?, ?2?≤??≤?2? and ??≠??. 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

题目大意:在?轴上给出两个段[?1;?1]和[?2;?2]。保证?1<?1和?2<?2。段可以相互交叉,重叠或甚至重合,你必须选择两个不同的整数点,使第一个点属于段[?1;?1],第二个点属于段[?2;?2]。可以打印任意答案。
如果a[1]!=a[3],直接输出a[1],a[3],如果a[1]=a[3],输出a[1]+1,a[3];

#include <iostream>
#include<cstdio>
#define ll long long
#define rep(i,a,b)for(int i=a;i<b;i++)
#define rep1(i,a,b)for(int i=a;i<=b;i++)
using namespace std;
const int maxn =1e6+5;
ll a[maxn];
int main() {
    int n;
    while(~scanf("%d",&n)){
        while(n--){
            rep1(i,1,4){
                scanf("%lld",&a[i]);
                //printf("a[%d]=%lld\n",i,a[i]);
            }
            ll ans=a[1],ans1=a[3];
            if(ans!=ans1)
                printf("%lld %lld\n",ans,ans1);
            else
                printf("%lld %lld\n",ans+1,ans1);
        }
    }
    return 0;
}

B - Divisors of Two Integers
传送门:https://codeforces.com/problemset/problem/1108/B
Recently you have received two positive integer numbers ? and ?. You forgot them, but you remembered a shuffled list containing all divisors of ? (including 1 and ?) and all divisors of ? (including 1 and ?). If ? is a divisor of both numbers ? and ? at the same time, there are two occurrences of ? in the list.

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

Your problem is to restore suitable positive integer numbers ? and ? 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 ? and ?.

Input
The first line contains one integer ? (2≤?≤128) — the number of divisors of ? and ?.

The second line of the input contains ? integers ?1,?2,…,?? (1≤??≤104), where ?? is either divisor of ? or divisor of ?. If a number is divisor of both numbers ? and ? then there are two copies of this number in the list.

Output
Print two positive integer numbers ? and ? — 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

题目大意:将两个数的公因子分别打散,要求你恢复这两个数,并且输出。
首先,先排序,然后最大的数肯定是我们要找的其中一个,然后遍历最大的数的除数,然后标记,注意一点就是它的除数不能等于相邻的数,不然会重复计算,wa掉。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define ll long long
#define ld long double
#define ull unsigned long long
#define inf 0x3f3f3f3f
#define rep(i,a,b)for(int i=a;i<=b;i++)
#define rep1(i,a,b)for(int i=a;i<b;i++)
#define rep2(i,a,b)for(int i=a;i>=b;i--)
#define rep3(i,a,b)for(int i=a;i>b;i--)
const int maxn=3e5+10;
using namespace std;
int arr[maxn],vis[maxn];
int n;
int main(){
    memset(vis,0,sizeof(vis));//初始化为0
    scanf("%d",&n);
    rep(i,1,n)
    scanf("%d",&arr[i]);
    sort(arr+1,arr+n+1);
    ll ans1=arr[n];
    ll ans2;
    rep(i,1,n-1){
        if(arr[n]%arr[i]==0&&arr[i]!=arr[i+1])
            vis[i]=1;//标记
    }
    rep2(i,n-1,1){
        if(!vis[i]){
            ans2=arr[i];
            break;
        }
    }
    printf("%lld %lld\n",ans1,ans2);
    return 0;
}

C. Nice Garland
传送门:https://codeforces.com/problemset/problem/1108/C

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

题目大意:替换最少的字符使得字符串符合漂亮花环的定义。漂亮花环定义:如果任何两个相同颜色的灯具有可被3整除的距离,那么花环就会被称为漂亮。
怎么做呢?首先我们能确定的是有RGB,RBG,GBR,GRB,BRG,BGR这6种大可能,就是如果你选了其中一种,只能按其中一种循环,例如RGBRGB,而不能是RGBRBG。我们遍历一下,然后每次都存一个计数变量,然后比较选出最小值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define inf 0x3f3f3f3f
using namespace std;
int main(){
string col[6]={"RGB","RBG","GBR","GRB","BGR","BRG"};//二维数组,字符串数组更方便
int n;
scanf("%d",&n);
string s;
cin>>s;
int ans=inf ,idx;
for(int i=0;i<6;i++){
int cnt=0;//计数变量要放在这,每次换成不同的字符串重新计算。例如:从“RGB”到“RBG”要重新计算不同字符个数
for(int j=0;j<n;j++)
if(s[j]!=col[i][j%3])
  cnt++;
  if(ans>cnt){
  ans=cnt;//更新最小值
  idx=i;//纪录下标,看是匹配成哪个字符串
                  }
}
printf("%d\n",ans);
for(int i=0;i<n;i++)
printf("%c",col[idx][i%3]);//匹配成哪个字符串,就直接循环输出
printf("\n");
return 0;
}

D. Diverse Garland
传送门:https://codeforces.com/problemset/problem/1108/D
You have a garland consisting of ? lamps. Each lamp is colored red, green or blue. The color of the ?-th lamp is ?? (‘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 1) have distinct colors.

In other words, if the obtained garland is ? then for each ? from 1 to ?−1 the condition ??≠??+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 ? (1≤?≤2⋅105) — the number of lamps.

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

Output
In the first line of the output print one integer ? — 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 ? of length ? — 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

要求相邻的两个字符不能一样,更换最小数量的字母,使得相邻的两个都不能相同。采用贪心,顺序枚举从2到N的所有字符,如果与之前的字符相等,就根据前后字符修改这个字符。

为什么呢?如果碰到一对相邻且相同的元素?,?+1,后一个元素?+2可能与?+1相等,但是?不可能与?−1相等(因为前面的元素已经过修改调整),因此,修改?+1可能会更优于?(因为如果修改?的话,可能还需要修改?+1或?+2)。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define ll long long
#define ld long double
#define ull unsigned long long
#define inf 0x3f3f3f3f
#define rep(i,a,b)for(int i=a;i<=b;i++)
#define rep1(i,a,b)for(int i=a;i<b;i++)
#define rep2(i,a,b)for(int i=a;i>=b;i--)
#define rep3(i,a,b)for(int i=a;i>b;i--)
const int maxn=3e5+10;
const ld pi = acos(-1);
using namespace std;
int n;
char c[maxn];
int main(){
    scanf("%d",&n);
    scanf("%s",c+1);
    int cnt=0;
    rep(i,1,n-1){
        if(c[i]==c[i+1]){
            cnt++;
            if(c[i]!='R'&&c[i+2]!='R')
                c[i+1]='R';
            if(c[i]!='G'&&c[i+2]!='G')
                c[i+1]='G';
            if(c[i]!='B'&&c[i+2]!='B')
                c[i+1]='B';
        }
    }
    cout<<cnt<<endl;
    puts(c+1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值