2020年10月第一次周赛题解

A - Contest for Robots

Polycarp is preparing the first programming contest for robots. There are n problems in it, and a lot of robots are going to participate in it. Each robot solving the problem i gets pi points, and the score of each robot in the competition is calculated as the sum of pi over all problems i solved by it. For each problem, pi is an integer not less than 1

.

Two corporations specializing in problem-solving robot manufacturing, "Robo-Coder Inc." and "BionicSolver Industries", are going to register two robots (one for each corporation) for participation as well. Polycarp knows the advantages and flaws of robots produced by these companies, so, for each problem, he knows precisely whether each robot will solve it during the competition. Knowing this, he can try predicting the results — or manipulating them.

For some reason (which absolutely cannot involve bribing), Polycarp wants the "Robo-Coder Inc." robot to outperform the "BionicSolver Industries" robot in the competition. Polycarp wants to set the values of pi

in such a way that the "Robo-Coder Inc." robot gets strictly more points than the "BionicSolver Industries" robot. However, if the values of pi will be large, it may look very suspicious — so Polycarp wants to minimize the maximum value of pi

over all problems. Can you help Polycarp to determine the minimum possible upper bound on the number of points given for solving the problems?

Input

The first line contains one integer n

(1≤n≤100

) — the number of problems.

The second line contains n

integers r1, r2, ..., rn (0≤ri≤1). ri=1 means that the "Robo-Coder Inc." robot will solve the i-th problem, ri=0 means that it won't solve the i

-th problem.

The third line contains n

integers b1, b2, ..., bn (0≤bi≤1). bi=1 means that the "BionicSolver Industries" robot will solve the i-th problem, bi=0 means that it won't solve the i

-th problem.

Output

If "Robo-Coder Inc." robot cannot outperform the "BionicSolver Industries" robot by any means, print one integer −1

.

Otherwise, print the minimum possible value of maxi=1npi

, if all values of pi

are set in such a way that the "Robo-Coder Inc." robot gets strictly more points than the "BionicSolver Industries" robot.

Examples

Input

5

1 1 1 0 0

0 1 1 1 1

Output

3

Input

3

0 0 0

0 0 0

Output

-1

Input

4

1 1 1 1

1 1 1 1

Output

-1

Input

9

1 0 0 0 0 0 0 0 1

0 1 1 0 1 1 1 1 0

Output

4

Note

In the first example, one of the valid score assignments is p=[3,1,3,1,1]

. Then the "Robo-Coder" gets 7 points, the "BionicSolver" — 6

points.

In the second example, both robots get 0

points, and the score distribution does not matter.

In the third example, both robots solve all problems, so their points are equal.

题目思路:

比较两个机器人,保证死一个机器人一定获胜,每次输入的第一列数据为甲的结果,第二列为乙的结果,当两者相同即平局的时候不用考虑,遍历后求出甲获胜的次数x,乙获胜的次数y,要让甲获胜,必须得让每局的分数=(x+y)/y,但是如果x=0,无论如何甲也不会获胜了,输出-1;

代码如下

#include<iomanip>

#include<iostream>

using namespace std;

const int maxn = 1e3+10;

int main(){

       int n;

       cin>>n;

       int r[maxn];

       int b[maxn];

       for(int i = 0;i<n;i++){

              cin>>r[i];

       }

       for(int i =0;i<n;i++){

              cin>>b[i];

       }

       int x = 0;

       int y= 0;

       for(int i = 0;i<n;i++){

              if(r[i] == b[i]){

                     continue;

              }

              else if(r[i] == 1){

                     x+=1;

              }

              else{

                     y+=1;

              }

       }

       if(x==0){

              cout<<-1<<endl;

       }

       else{

              cout<<(y+x)/x<<endl;

       }

       return 0;

}

 

B - Journey Planning

Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n

.

Tanya plans her journey as follows. First of all, she will choose some city c1

to start her journey. She will visit it, and after that go to some other city c2>c1, then to some other city c3>c2, and so on, until she chooses to end her journey in some city ck>ck−1. So, the sequence of visited cities [c1,c2,…,ck]

should be strictly increasing.

There are some additional constraints on the sequence of cities Tanya visits. Each city i

has a beauty value bi associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities ci and ci+1, the condition ci+1−ci=bci+1−bci

must hold.

For example, if n=8

and b=[3,4,4,6,6,7,8,9]

, there are several three possible ways to plan a journey:

  • c=[1,2,4]

·  ;

·  c=[3,5,6,8]

·  ;

·  c=[7]

  • (a journey consisting of one city is also valid).

There are some additional ways to plan a journey that are not listed above.

Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?

Input

The first line contains one integer n

(1≤n≤2⋅105

) — the number of cities in Berland.

The second line contains n

integers b1, b2, ..., bn (1≤bi≤4⋅105), where bi is the beauty value of the i

-th city.

Output

Print one integer — the maximum beauty of a journey Tanya can choose.

Examples

Input

6

10 7 1 9 10 15

Output

26

Input

1

400000

Output

400000

Input

7

8 9 26 11 12 29 14

Output

55

Note

The optimal journey plan in the first example is c=[2,4,5]

.

The optimal journey plan in the second example is c=[1]

.

The optimal journey plan in the third example is c=[3,6]

变换一下:

b[i]-i=b[j]-j (i>j)

这样等式左右两边就没有直接的关系了,因为两边里面的数都是某一项的,不用和其他项扯上关系,那么时间复杂度O(n)是可以确定下来了!

变换后的等式是什么意思呢?就是每个“城市的美丽值和编号的差值”相同的城市我们都可以并入一个序列,然后比较每个序列的美丽值,取最大的即可!这里我们使用map s记录,s[i]表示差值为i的序列总的美丽值

代码如下:

需要注意的是map的查找复杂度为logn,所以总的时间复杂度其实为O(nlogn)

代码如下:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<map>

using namespace std;

typedef long long int ll;

const int mod = 1e9+7;

ll b[200005];

int n;

ll ans = 0ll;

map<ll,ll> s;

int main(){

       scanf("%lld",&n);

       for(int i =1;i<=n;i++){

              scanf("%lld",&b[i]);

       }

       for(int i =1;i<=n;i++){

              s[b[i]-i] += b[i];

              ans = max(ans,s[b[i]-i]);

       }

       printf("%lld",ans);

       return 0;

}

C - War of the Corporations

A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000.

This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol's artificial intelligence.

Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol's director decided to replace some characters in AI name with "#". As this operation is pretty expensive, you should find the minimum number of characters to replace with "#", such that the name of AI doesn't contain the name of the phone as a substring.

Substring is a continuous subsequence of a string.

Input

The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100 000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters.

Output

Print the minimum number of characters that must be replaced with "#" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring.

Examples

Input

intellect
tell

Output

1

Input

google
apple

Output

0

Input

sirisiri
sir

Output

2

Note

In the first sample AI's name may be replaced with "int#llect".

In the second sample Gogol can just keep things as they are.

In the third sample one of the new possible names of AI may be

.题目解答:

从一个长字符串str1中找出有几个子字符串str2

解答解答:首先对两个字符串进行循环比较,用子串的第一个字母去与长串的每个字母进行匹配。当子串的第一个字母匹配到相同时,再进行接下来的字母的匹配,如果相同,继续下一个,直至子串的所有字母匹配完成,这代表查找到一个子串,如果不相同则继续用子串的第一个字母接着往后面匹配。查找到一个子串后,用子串的第一个字母接着往后面匹配,查找是否有第二个子串。

例如:长串intellect,子串tell先用字母‘t’去与长串的每一个字母进行比较,当匹配到长串的第三个的时候,发现相同了,这时再拿子串的‘e’去与长串的‘t’的下一个比较,发现也相同,接下来继续,‘l’‘l’也相同,比较完成,在长串中找到了一个子串。再用子串的首字母‘t’与长串的此处的下一个字母进行比较,也就是‘e’,发现不相同,那就再比较下一个,循环比较,直至结束。

思路如下:
用KMP的方法进行字符串比较(暴力也可)
每次搜索完一个单词后将指针归零


#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<queue>

using namespace std;

const int N = 1000005;

int F[N];

char a[N],b[N];

int sum;

void getF(char *p,int m){

       F[1]= 0;

       for(int i = 2;i<=m;i++){

              int k = F[i-1];

              while(k&&p[k+1]!= p[i]) k = F[k];

              if(p[k+1] == p[i]) k++;

              F[i]= k;

             

       }

}

void findMatch(char *p,int m,char *T,int n){

       sum = 0;

       getF(T,n);

       int k = 0;

       for(int i =1;i<=m;i++){

              while(p[i] != T[k+1]&&k) k = F[k];

              if(p[i] == T[k+1]) k++;

              if(n == k){

                     sum++;

                     k =0;

              }

       }

       cout<<sum<<endl;

      

}

 

int main(){

       scanf("%s %s",a+1,b+1);

       findMatch(a,strlen(a+1),b,strlen(b+1));

       return 0;

}

D - Blown Garland

Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland.

Now he has a goal to replace dead light bulbs, however he doesn't know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is working.

It is known that the garland contains light bulbs of four colors: red, blue, yellow and green. The garland is made as follows: if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them. For example, the garland can look like "RYBGRYBGRY", "YBGRYBGRYBG", "BGRYB", but can not look like "BGRYG", "YBGRYBYGR" or "BGYBGY". Letters denote colors: 'R' — red, 'B' — blue, 'Y' — yellow, 'G' — green.

Using the information that for each color at least one light bulb still works count the number of dead light bulbs of each four colors.

Input

The first and the only line contains the string s (4 ≤ |s| ≤ 100), which describes the garland, the i-th symbol of which describes the color of the i-th light bulb in the order from the beginning of garland:

  • 'R' — the light bulb is red,
  • 'B' — the light bulb is blue,
  • 'Y' — the light bulb is yellow,
  • 'G' — the light bulb is green,
  • '!' — the light bulb is dead.

The string s can not contain other symbols except those five which were described.

It is guaranteed that in the given string at least once there is each of four letters 'R', 'B', 'Y' and 'G'.

It is guaranteed that the string s is correct garland with some blown light bulbs, it means that for example the line "GRBY!!!B" can not be in the input data.

Output

In the only line print four integers kr, kb, ky, kg — the number of dead light bulbs of red, blue, yellow and green colors accordingly.

Examples

Input

RYBGRYBGR

Output

0 0 0 0

Input

!RGYB

Output

0 1 0 0

Input

!!!!YGRB

Output

1 1 1 1

Input

!GB!RG!Y!

Output

2 1 1 0

Note

In the first example there are no dead light bulbs.

In the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements.

思路:

仔细观察,你会发现,这四个灯泡的出现顺序是一定的,只要每次遍历一次,让他们的下标对4取余,用变量接收,就可以记录这四种颜色的灯光出现的顺序,在遍历一遍,每次在这个顺序上比较是不是缺少了,并记录缺少的个数;

代码如下:

#include<iostream>

#include<iomanip>

using namespace std;

int main(){*/

       /*当四个灯泡都正常的时候,这四个灯泡的输入顺序是一定的

       只要找到每个的出场次序,在计算少了多少个,输出就行了*/

/*   

       char  str[999];

       int indexr,indexb,indexy,indexg;

       gets(str);

       for(int i = 0;str[i]!='\0';i++){

              if(str[i] == 'R'){

                     indexr = i%4;

              }

              else if(str[i] == 'B'){

                     indexb = i %4;

              }

              else if(str[i] == 'Y'){

                     indexy = i % 4;

              }

              else if(str[i] == 'G'){

                     indexg = i %4;

              }

       }

       //在遍历一边,找到他们谁少了多少个

       int numr=0,numb=0,numy=0,numg=0;

       for(int i =0;str[i] != '\0';i++){

              if(str[i] == '!'){

                     if(i%4 == indexr){

                            numr ++;

                     }

                     else if(i%4 == indexb){

                            numb++;

                     }

                     else if(i%4 == indexy){

                            numy ++;

                     }

                     else if(i %4 == indexg){

                            numg ++;

                     }

              }

       }

       printf("%d %d %d %d\n",numr,numb,numy,numg);

      

      

}

E - Perform the Combo

You want to perform the combo on your opponent in one popular fighting game. The combo is the string s consisting of n lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in s. I.e. if s=

"abca" then you have to press 'a', then 'b', 'c' and 'a' again.

You know that you will spend m

wrong tries to perform the combo and during the i-th try you will make a mistake right after pi-th button (1≤pi<n) (i.e. you will press first pi buttons right and start performing the combo from the beginning). It is guaranteed that during the m+1

-th try you press all buttons right and finally perform the combo.

I.e. if s=

"abca", m=2 and p=[1,3]

then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.

Your task is to calculate for each button (letter) the number of times you'll press it.

You have to answer t

independent test cases.

Input

The first line of the input contains one integer t

(1≤t≤104

) — the number of test cases.

Then t

test cases follow.

The first line of each test case contains two integers n

and m (2≤n≤2⋅105, 1≤m≤2⋅105) — the length of s

and the number of tries correspondingly.

The second line of each test case contains the string s

consisting of n

lowercase Latin letters.

The third line of each test case contains m

integers p1,p2,…,pm (1≤pi<n) — the number of characters pressed right during the i

-th try.

It is guaranteed that the sum of n

and the sum of m both does not exceed 2⋅105 (∑n≤2⋅105, ∑m≤2⋅105

).

It is guaranteed that the answer for each letter does not exceed 2⋅109

.

Output

For each test case, print the answer — 26

integers: the number of times you press the button 'a', the number of times you press the button 'b', …

, the number of times you press the button 'z'.

Example

Input

3

4 2

abca

1 3

10 5

codeforces

2 8 3 2 9

26 10

qwertyuioplkjhgfdsazxcvbnm

20 10 1 2 3 5 10 5 9 4

Output

4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0

2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2

Note

The first test case is described in the problem statement. Wrong tries are "a", "abc" and the final try is "abca". The number of times you press 'a' is 4

, 'b' is 2 and 'c' is 2

.

In the second test case, there are five wrong tries: "co", "codeforc", "cod", "co", "codeforce" and the final try is "codeforces". The number of times you press 'c' is 9

, 'd' is 4, 'e' is 5, 'f' is 3, 'o' is 9, 'r' is 3 and 's' is 1.

题意:

长度为n的字符串组合,(仅有:a~z)长度为m的数集a,表示共按错了m次。按完第ai个按钮后,再从头开始。问所有字母被按了几次。

思路:

对b数组排序,优化重复计算的部分即可。
例:

10 5
codeforces 存入字符数组a
2 8 3 2 9 存入整形数组b

对b中m个数进行排序得:2 2 3 8 9
所以:

这样就可以省下重复计算的次数,注意,从b[0]=0开始,i 取[0~m-1]

代码如下:

#include<stdio.h>

#include<string.h>

#include<algorithm>

#define N 200010

using namespace std;

int b[N],s[30],n;

char a[N];

int main()

{

    int t,m;

    scanf("%d",&t);

    while(t--){

        memset(s,0,sizeof(s));

        scanf("%d%d%s",&n,&m,a+1);

        for(int i=1;i<=m;i++)

            scanf("%d",&b[i]);

        sort(b+1,b+1+m);

        b[0]=0;

        for(int i=0;i<m;i++)

            for(int j=b[i]+1;j<=b[i+1];j++)

               s[a[j]-'a']+=(m-i); //重复的直接加m-i次

        for(int i=1;i<=n;i++)

            s[a[i]-'a']++; //正确按一遍组合字母增加的次数

        for(int i=0;i<26;i++){

            if(i) printf(" ");

            printf("%d",s[i]);

        }

        printf("\n");

    }

    return 0;

}

F - Ability To Convert

Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·162 + 13·161 + 11·160). Alexander lived calmly until he tried to convert the number back to the decimal number system.

Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k.

Input

The first line contains the integer n (2 ≤ n ≤ 109). The second line contains the integer k (0 ≤ k < 1060), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n.

Alexander guarantees that the answer exists and does not exceed 1018.

The number k doesn't contain leading zeros.

Output

Print the number x (0 ≤ x ≤ 1018) — the answer to the problem.

Examples

Input

13
12

Output

12

Input

16
11311

Output

475

Input

20
999

Output

3789

Input

17
2016

Output

594

Note

In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·130 or 15 = 1·131 + 2·130.

博文链接:https://blog.csdn.net/x_ring/article/details/68925081

进制转换 将N进制的数转化为十进制,让你求最小的数

贪心 每次都取可能大的数,这样最后的结果最小

要注意前导零

#include <iostream>

#include <cstdio>

#include <cmath>

#include <cstring>

using namespace std;

long long n,ans;

int l;

string k;

 

long long powx(long long x,int y)

{

    if(y==0)return 1;

    long long s=powx(x,y/2);

if(y%2)return s*s*x;

    else return s*s;

}

 

int main()

{

    scanf("%lld",&n);

    long long t=n;

    while(t>0)

    {

        t/=10;

        l++;

    }

    cin>>k;

 

    int p=0,v=0,last=k.size()-1;

    long long sum=0;

    for(int i=k.size()-1;i>=0;i--)

    {

        long long t=sum;

        sum+=powx(10,v)*(k[i]-48);

        if(sum<n && v<l)v++;

        else

        {

            int kkk=i;

            if(k[i+1]=='0')

            {

                int ss=0;

                while(k[++i]=='0')

                {

                    ss++;

                    if(i==last)break;

                }

                i--;

            }

            ans+=powx(n,p)*t;

            sum=k[i]-48;

            v=1;

            p++;

            last=i;

        }

    }

    ans+=powx(n,p)*sum;

    printf("%lld\n",ans);

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值