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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章