Codeforces Round #535 (Div. 3) (B,C,D,E1)题解

比赛地址

B. Divisors of Two Integers

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 yyat 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

input

10
10 2 8 1 2 4 1 20 4 5

output

20 8

题意:就是给定一个数组,里面有两个数的所有因子和本身,现在要求找出这两个数。

题解:首先我们将数组排序,最大的那个数一定答案之一,因为两个数有相同因子时,该因子在数组会有两个,所以我们先用最大那个数去除以数组里面的数,如果能够除的尽,就将该数标记一下,这里要特别注意,要是有两个相同的数都能被除尽,只能标记其中一个,然后第二个答案就是数组里未标记的最大的那个数。

#include <bits/stdc++.h>
const int maxn=2e2+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
using namespace std;
int main()
{
    int n,a[maxn]= {0};
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    int ans1=a[n],ans2=0;
    int vis[10005];me(vis,0);
    for(int i=1; i<n;i++)
        if(ans1%a[i]==0&&a[i]!=a[i+1])
            vis[i]=1;
    for(int i=n-1;i>=1;i--)
    {
        if(!vis[i])
        {
            ans2=a[i];
            break ;
        }
    }
    printf("%d %d\n",ans1,ans2);
    return 0;
}

C. Nice Garland

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 minimumnumber of recolors. If there are multiple optimal solutions, print any of them.

Examples

input

3
BRB

output

1
GRB

input

7
RGBGRBB

output

3
RGBRGBR

题意:就是要将给定字符串对三取余的余数相同的每一位变成相同的字符,现在要求我们换最少的字符。

题解:其实我们只要确定了前三个字符,然后后面的就确定了,三中颜色一共6中组合,枚举每一种,然后记录哪一种字符变得最少。

#include <bits/stdc++.h>
const int maxn=2e5+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
using namespace std;
int n;
char str[maxn];
int mi(char *s)
{
    int sum=0,len=0;
    for(int i=0;i<n;i++)
    {
        if(str[i]!=s[len])
            sum++;
        len=(len+1)%3;
    }
    return sum;
}
int main()
{

    char s[10][4]={"RGB","RBG","GRB","GBR","BGR","BRG"};
    scanf("%d%s",&n,str);
    int ans,flog=inf;
    for(int i=0;i<6;i++)
    {
        int temp=mi(s[i]);
        if(temp<flog)
            flog=temp,ans=i;
    }
    printf("%d\n",flog);
    int len=0;
    for(int i=0;i<n;i++)
    {
        printf("%c",s[ans][len]);
        len=(len+1)%3;
    }
    printf("\n");
    return 0;
}

D. Diverse Garland

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 minimumnumber of recolors. If there are multiple optimal solutions, print any of them.

Examples

input

9
RBGRRBRGG

output

2
RBGRGBRGR

input

8
BBBGBRRR

output

2
BRBGBRGR

input

13
BBRRRRGGGGGRR

output

6
BGRBRBGBGBGRG

题意:将给定字符串换成相邻位的字符都不一样,现在要求换最少的字符。

题解:现在这个字符串只要有相同的,我们都要换,只是要选择换成哪个字符,如果i位字符与i+1位字符相等,与i+2位字符不相等,我们就只需要将i+1位字符换成除去这两个剩下的字符(例如:GGR换成GBR),另一种情况就是连续超过三个位的字符都相等,我们只需要换中间那位就好了。

#include <bits/stdc++.h>
const int maxn=2e5+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
using namespace std;
int main()
{
    int n,sum=0;
    char s[maxn];
    scanf("%d%s",&n,s);
    for(int i=0; i<n-1; i++)
    {
        if(s[i]==s[i+1])
        {
            sum++;
            if(s[i]=='R')
            {
                s[i+1]='B';
                if(i+2<n && s[i+2]=='B')
                    s[i+1]='G';
            }
            else if(s[i]=='B')
            {
                s[i+1]='G';
                if(i+2<n && s[i+2]=='G')
                    s[i+1]='R';
            }
            else if(s[i]=='G')
            {
                s[i+1]='R';
                if(i+2<n && s[i+2]=='R')
                    s[i+1]='B';
            }
        }
    }
    printf("%d\n%s\n",sum,s);
    return 0;
}

E1. Array and Segments (Easy version)

The only difference between easy and hard versions is a number of elements in the array.

You are given an array aa consisting of nn integers. The value of the ii-th element of the array is aiai.

You are also given a set of mm segments. The jj-th segment is [lj;rj][lj;rj], where 1≤lj≤rj≤n1≤lj≤rj≤n.

You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array a=[0,0,0,0,0]a=[0,0,0,0,0] and the given segments are [1;3][1;3] and [2;4][2;4] then you can choose both of them and the array will become b=[−1,−2,−2,−1,0]b=[−1,−2,−2,−1,0].

You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array aa and obtain the array bb then the value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi will be maximum possible.

Note that you can choose the empty set.

If there are multiple answers, you can print any.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains two integers nn and mm (1≤n≤300,0≤m≤3001≤n≤300,0≤m≤300) — the length of the array aa and the number of segments, respectively.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106), where aiai is the value of the ii-th element of the array aa.

The next mm lines are contain two integers each. The jj-th of them contains two integers ljlj and rjrj (1≤lj≤rj≤n1≤lj≤rj≤n), where ljljand rjrj are the ends of the jj-th segment.

Output

In the first line of the output print one integer dd — the maximum possible value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi if bb is the array obtained by applying some subset of the given segments to the array aa.

In the second line of the output print one integer qq (0≤q≤m0≤q≤m) — the number of segments you apply.

In the third line print qq distinct integers c1,c2,…,cqc1,c2,…,cq in any order (1≤ck≤m1≤ck≤m) — indices of segments you apply to the array aa in such a way that the value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi of the obtained array bb is maximum possible.

If there are multiple answers, you can print any.

Examples

input

5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3

output

6
2
1 4 

input

5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5

output

7
2
3 2 

input

1 0
1000000

output

0
0

Note

In the first example the obtained array bb will be [0,−4,1,1,2][0,−4,1,1,2] so the answer is 66.

In the second example the obtained array bb will be [2,−3,1,−1,4][2,−3,1,−1,4] so the answer is 77.

In the third example you cannot do anything so the answer is 00.

题意:首先有一个长度为n序列,然后给出m对l,r。每次操作可将区间l,r。里面的数都减一,也可以不选这个区间,问选哪些区间操作,最后这个序列的极值会变大(极值=数列里面的最大值-最小值)。

题解:这个题可以用线段树,但是在这里因为数据比较小,所以我们可以暴力来跑,因为操作只能是减一,所以我们的极值只用来维护最小值,要是最小值与当前最大值都在修改区间之内,极值不变。要是最小值在,而最大值不在,极值增大。所以我们枚举每一位,以当前位的数为最小值,然后只要修改区间包含当前位置,都修改,修改过后只会极值不变或变大。修改完了维护一下最大极值就行了。

#include <bits/stdc++.h>
const int maxn=1e3+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
using namespace std;
struct node
{
    int l,r;
}opt[maxn];
int main()
{
    int a[maxn],b[maxn],n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)
        scanf("%d%d",&opt[i].l,&opt[i].r);
    vector<int>q;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        vector<int>temp;
        for(int j=1;j<=n;j++)
            b[j]=a[j];
        for(int j=1;j<=m;j++)
            if(opt[j].l<=i&&opt[j].r>=i)
            {
                temp.push_back(j);
                for(int k=opt[j].l;k<=opt[j].r;k++)
                    b[k]--;
            }
        int ma=*max_element(b+1,b+n+1)-*min_element(b+1,b+n+1);///求区间最大值最小值函数
        if(ma>ans)
            ans=ma,q=temp;
    }
    printf("%d\n",ans);
    printf("%d\n",q.size());
    for(int i=0;i<q.size();i++)
        printf("%d ",q[i]);
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41292370/article/details/86632003