Codeforces Round 63 (Div. 2)

A. Reverse a Substring

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string ss consisting of nn lowercase Latin letters.

Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position 33and ends in position 66), but "aa" or "d" aren't substrings of this string. So the substring of the string ss from position ll to position rr is s[l;r]=slsl+1…srs[l;r]=slsl+1…sr.

You have to choose exactly one of the substrings of the given string and reverse it (i. e. make s[l;r]=srsr−1…sls[l;r]=srsr−1…sl) to obtain a string that is less lexicographically. Note that it is not necessary to obtain the minimum possible string.

If it is impossible to reverse some substring of the given string to obtain a string that is less, print "NO". Otherwise print "YES" and anysuitable substring.

String xx is lexicographically less than string yy, if either xx is a prefix of yy (and x≠yx≠y), or there exists such ii (1≤i≤min(|x|,|y|)1≤i≤min(|x|,|y|)), that xi<yixi<yi, and for any jj (1≤j<i1≤j<i) xj=yjxj=yj. Here |a||a| denotes the length of the string aa. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​.

Input

The first line of the input contains one integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the length of ss.

The second line of the input contains the string ss of length nn consisting only of lowercase Latin letters.

Output

If it is impossible to reverse some substring of the given string to obtain a string which is lexicographically less, print "NO". Otherwise print "YES" and two indices ll and rr (1≤l<r≤n1≤l<r≤n) denoting the substring you have to reverse. If there are multiple answers, you can print any.

Examples

input

Copy

7
abacaba

output

Copy

YES
2 5

input

Copy

6
aabcfg

output

Copy

NO

Note

In the first testcase the resulting string is "aacabba".

这个题一开始理解错题意惹,傻傻的wa了

大体题意是在字符串S1中找到一个子串s,使这个子串逆过来,当前字符串变成S2,如果S2的字典序小于S1,那么就输出这个子串的始末位置。

在这里只要求字典序变小就可以,其实可以再延展出字典序最小的题,emmm貌似难度就上去了...

字典序变小,只要找到前一个字母的字典序大于后一个字母,然后始末位置就是i和i+1

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+5;
string s;

int main()
{
    int n;
    cin>>n;
    cin>>s;
    bool flag=true;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]>s[i+1])
        {
            cout<<"YES"<<endl;
            cout<<i+1<<' '<<i+2<<endl;//位置从1开始
            flag=false;
            break;
        }
    }

    if(flag)
        cout<<"NO"<<endl;
    return 0;
}

B. Game with Telephone Numbers

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A telephone number is a sequence of exactly 1111 digits such that its first digit is 8.

Vasya and Petya are playing a game. Initially they have a string ss of length nn (nn is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player's move it may be 112, 111 or 121. The game ends when the length of string ss becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.

You have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves).

Input

The first line contains one integer nn (13≤n<10513≤n<105, nn is odd) — the length of string ss.

The second line contains the string ss (|s|=n|s|=n) consisting only of decimal digits.

Output

If Vasya has a strategy that guarantees him victory, print YES.

Otherwise print NO.

Examples

input

Copy

13
8380011223344

output

Copy

YES

input

Copy

15
807345619350641

output

Copy

NO

Note

In the first example Vasya needs to erase the second character. Then Petya cannot erase a character from the remaining string 880011223344 so that it does not become a telephone number.

In the second example after Vasya's turn Petya can erase one character character 8. The resulting string can't be a telephone number, because there is no digit 8 at all.

电话号码是首位为8的11位数

Vasya先可以消去任意一个数, 然后再Petya消去任意一个数,轮流知道位数为11,若是一个电话号码则Vasya赢,否则Vasya输

长度为n,我们可以消去n-11个数,如果可以消去的数是奇数的话,那么Vasya就比Petya多消去一个数

Petya想赢的话,就会尽量的消去8,如果8的数目小于Petya的机会的话,Vasya肯定输

因为8在第一位才能使电话号码,所以Vasya想尽量让8在第一位,也就是想尽量消去8前面的不是8的数,如果在Vasya有的机会内,没有把剩余的最前面的8的前面的不是8的数消去,那么8就是不第一位,必然Vasya会输

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+5;
int n;
string s;
int flag1[maxn];

int main()
{
    cin>>n;
    cin>>s;
    int Count=0;
    int cur=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='8')
        {
            Count++;
            flag1[Count]=i+1;//每个8的位置
        }
    }
    int num=(n-11)/2; //P的机会数
    int numm=(n-11)&1?num+1:num;// M的机会数

    bool flag=true;
    if(Count<=num)//8的个数<P的机会数
         flag=false;
    if(Count>num)
    {
        for(int i=1;i<=num+1;i++)
        {
            if(flag1[i]-i>numm)//8的前面不是8的数>M的机会数,也就是消完后8不是第一位
            {
                flag=false;
                break;
            }
        }
    }

    if(flag)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
}

C. Alarm Clocks Everywhere

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the ii-th of them will start during the xixi-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes x1,x2,…,xnx1,x2,…,xn, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute).

Ivan can choose two properties for the alarm clock — the first minute it will ring (let's denote it as yy) and the interval between two consecutive signals (let's denote it by pp). After the clock is set, it will ring during minutes y,y+p,y+2p,y+3py,y+p,y+2p,y+3p and so on.

Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of pp. He has to pick it among the given values p1,p2,…,pmp1,p2,…,pm (his phone does not support any other options for this setting).

So Ivan has to choose the first minute yy when the alarm clock should start ringing and the interval between two consecutive signals pjpj in such a way that it will ring during all given minutes x1,x2,…,xnx1,x2,…,xn (and it does not matter if his alarm clock will ring in any other minutes).

Your task is to tell the first minute yy and the index jj such that if Ivan sets his alarm clock with properties yy and pjpj it will ring during all given minutes x1,x2,…,xnx1,x2,…,xn or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any.

Input

The first line of the input contains two integers nn and mm (2≤n≤3⋅105,1≤m≤3⋅1052≤n≤3⋅105,1≤m≤3⋅105) — the number of events and the number of possible settings for the interval between signals.

The second line of the input contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤10181≤xi≤1018), where xixi is the minute when ii-th event starts. It is guaranteed that all xixi are given in increasing order (i. e. the condition x1<x2<⋯<xnx1<x2<⋯<xn holds).

The third line of the input contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pj≤10181≤pj≤1018), where pjpj is the jj-th option for the interval between two consecutive signals.

Output

If it's impossible to choose such values yy and jj so all constraints are satisfied, print "NO" in the first line.

Otherwise print "YES" in the first line. Then print two integers yy (1≤y≤10181≤y≤1018) and jj (1≤j≤m1≤j≤m) in the second line, where yy is the first minute Ivan's alarm clock should start ringing and jj is the index of the option for the interval between two consecutive signals (options are numbered from 11 to mm in the order they are given input). These values should be chosen in such a way that the alarm clock will ring during all given minutes x1,x2,…,xnx1,x2,…,xn. If there are multiple answers, you can print any.

Examples

input

Copy

3 5
3 12 18
2 6 5 3 3

output

Copy

YES
3 4

input

Copy

4 2
1 5 17 19
4 5

output

Copy

NO

input

Copy

4 2
1 5 17 19
2 1

output

Copy

YES
1 1

大体题意是从第一个时间a1开始,后面的a2,a3......an 都是a1+xp(x是正整数)

所以找相邻两个活动时间之间的时间差,然后求gcd,就是最大的P,但是p数组里不一定存在。比如P=4,而p1=3,p2=2;那么可以选p=2,所以,当pi是P的因子的时候就可以

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
#define ll long long
int n,m;
ll a[maxn],b[maxn];
ll aa[maxn];

ll gcd(ll x,ll y)
{
    return y==0?x:gcd(y,x%y);
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(i>1)
            aa[i]=a[i]-a[i-1];//两个之间的时间差
    }

    for(int i=1;i<=m;i++)
        cin>>b[i];

    bool flag=false;

    ll x=gcd(aa[1],aa[2]);//开始求时间差的最大gcd
    for(int j=1;j<=n;j++)
        x=gcd(x,aa[j]);
        
    for(int i=1;i<=m;i++)
    {
        if(x%b[i]==0)//如果pi是P的因子
        {
            cout<<"YES"<<endl<<a[1]<<' '<<i<<endl;
            flag=true;
            break;
        }
    }
    if(!flag)//没找到
        cout<<"NO"<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值