Educational Codeforces Round 78 (Rated for Div. 2)

A. Shuffle Hashing

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.

Polycarp decided to store the hash of the password, generated by the following algorithm:

take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p′ (p′ can still be equal to p);
generate two random strings, consisting of lowercase Latin letters, s1 and s2 (any of these strings can be empty);
the resulting hash h=s1+p′+s2, where addition is string concatenation.
For example, let the password p= “abacaba”. Then p′ can be equal to “aabcaab”. Random strings s1= “zyx” and s2= “kjh”. Then h= “zyxaabcaabkjh”.

Note that no letters could be deleted or added to p to obtain p′, only the order could be changed.

Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.

Your program should answer t independent test cases.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.

The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.

Output
For each test case print the answer to it — “YES” if the given hash h could be obtained from the given password p or “NO” otherwise.

Example
inputCopy
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
outputCopy
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.

In the second test case both s1 and s2 are empty and p′= “threetwoone” is p shuffled.

In the third test case the hash could not be obtained from the password.

In the fourth test case s1= “n”, s2 is empty and p′= “one” is p shuffled (even thought it stayed the same).

In the fifth test case the hash could not be obtained from the password.

#include<bits/stdc++.h>
using namespace std;
int n,t;
string s,s1;
int sum1[30];
int sum2[30];
bool eq()
{
    for(int i=0;i<=26;i++)
    {if(sum1[i]!=sum2[i])return false;}
    return true;
}
int change(char c)
{
    int ans=c-'a';
    return ans;
}
void solve()
{
    memset(sum1,0,sizeof(sum1));memset(sum2,0,sizeof(sum2));
    int len1=s.length();
    int len2=s1.length();
    if(len1>len2){cout<<"NO\n";return ;}
    for(int i=0;i<len1;i++)sum1[change(s[i])]++;
    for(int i=0;i<len1;i++)
    sum2[change(s1[i])]++;
    if(eq()){cout<<"YES\n";return ;}
    for(int i=len1;i<len2;i++)
    {int k=i-len1;sum2[change(s1[k])]--;sum2[change(s1[i])]++;if(eq()){cout<<"YES\n";return ;} }
    cout<<"NO\n";
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>s>>s1;
        solve();
    }
    return 0;
}

B. A and B

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two integers a and b. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 1; during the second operation you choose one of these numbers and increase it by 2, and so on. You choose the number of these operations yourself.

For example, if a=1 and b=3, you can perform the following sequence of three operations:

add 1 to a, then a=2 and b=3;
add 2 to b, then a=2 and b=5;
add 3 to a, then a=5 and b=5.
Calculate the minimum number of operations required to make a and b equal.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

The only line of each test case contains two integers a and b (1≤a,b≤109).

Output
For each test case print one integer — the minimum numbers of operations required to make a and b equal.

Example
inputCopy
3
1 3
11 11
30 20
outputCopy
3
0
4
Note
First test case considered in the statement.

In the second test case integers a and b are already equal, so you don’t need to perform any operations.

In the third test case you have to apply the first, the second, the third and the fourth operation to b (b turns into 20+1+2+3+4=30).

#include<bits/stdc++.h>
using namespace std;
int n,t;
long long a,b;
long long s[200010];
int main()
{
    cin>>t;
    for(int i=1;i<=200000;i++){s[i]=s[i-1]+i;}
    while(t--)
    {
        cin>>a>>b;
        if(a>b)swap(a,b);
        int k=b-a;
        int ans=lower_bound(s+1,s+200000,b-a)-s;
        if(k%2==0)
        {
            if(ans%4==2)ans+=1;
            if(ans%4==1)ans+=2;
        }
        else
        {
            if(ans%4==3)ans+=2;
            if(ans%4==0)ans+=1;
        }
        if(k==0)ans=0;
        cout<<ans<<endl;
    }
    return 0;
}

C. Berry Jam

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n jars of strawberry and blueberry jam.

All the 2n jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly n jars to his left and n jars to his right.

For example, the basement might look like this:

Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right.

Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same.

For example, this might be the result:

He has eaten 1 jar to his left and then 5 jars to his right. There remained exactly 3 full jars of both strawberry and blueberry jam.
Jars are numbered from 1 to 2n from left to right, so Karlsson initially stands between jars n and n+1.

What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left?

Your program should answer t independent test cases.

Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤105).

The second line of each test case contains 2n integers a1,a2,…,a2n (1≤ai≤2) — ai=1 means that the i-th jar from the left is a strawberry jam jar and ai=2 means that it is a blueberry jam jar.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case print the answer to it — the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left.

Example
inputCopy
4
6
1 1 1 2 2 1 2 1 2 1 1 2
2
1 2 1 2
3
1 1 1 1 1 1
2
2 1 1 1
outputCopy
6
0
6
2
Note
The picture from the statement describes the first test case.

In the second test case the number of strawberry and blueberry jam jars is already equal.

In the third test case Karlsson is required to eat all 6 jars so that there remain 0 jars of both jams.

In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave 1 jar of both jams.
还有半个小时左右想到了方法 可惜某个地方敲错了 最后代码越改越乱 还好最后过了 说一下大概思路 从n到1维护一个前缀和 红蓝一个1一个-1 再从n+1到2 n维护一个前缀和 然后记录从n到一第一次出现的1 2 3…的位置 和从n+1到2 n1 2 3第一次出现的位置我们需要修改多少次呢 就是k=max(红个数,蓝个数)-min(红个数,蓝个数)刚刚记录的前缀和从n到一取k个位置再从n+1到2n取k个位置
最后每隔k-1个遍历一遍找到最小值输出 可能有些乱 具体看代码

#include<bits/stdc++.h>
using namespace std;
int n,t;
int a[200010];
int s1[200010],s2[200010];
vector<int>ans;
int main()
{
    cin>>t;
    while(t--)
    {
        ans.clear();
        cin>>n;
        int m=2*n,num1=0,num2=0;
        for(int i=1;i<=m;i++)
        {
            cin>>a[i];
            if(a[i]==1)num1++;else num2++;
        }
        if(num1==num2){cout<<0<<endl;continue;}
        int k=num1-num2;
        if(a[n]==1)s1[n]=1;else s1[n]=-1;
        for(int i=n-1;i>=1;i--)
        {
            if(a[i]==1)s1[i]=s1[i+1]+1;
            else s1[i]=s1[i+1]-1;
        }
        if(a[n+1]==1)s1[n+1]=1;else s1[n+1]=-1;
        for(int i=n+2;i<=m;i++)
        {
            if(a[i]==1)s1[i]=s1[i-1]+1;
            else s1[i]=s1[i-1]-1;
        }
        if(k>0)
        {
            int h=1;
            for(int i=n;i>=1;i--)
            {
            if(s1[i]==h)
            {
                ans.push_back(i);
                h++;
            }
        }
        int v=ans.size();
        reverse(ans.begin(),ans.end());
        h=1;
        for(int i=n+1;i<=m;i++)
        {
            if(s1[i]==h)
            {
                ans.push_back(i);
                h++;
            }
        }
        int len=ans.size();
        int mi=99999999;
        for(int i=max(v-k,0);i+k-1<len&&i+k-1<v+k;i++)
            {
                if(i==v-k)mi=min(mi,n-ans[v-k]+1);
                else if(i==v){mi=min(ans[v+k-1]-n,mi);break;}
                else
                mi=min(mi,ans[i+k-1]-ans[i]+1);
            }
        cout<<mi<<endl;
        }
        else
        {
            k=-k;
            int h=-1;
        for(int i=n;i>=1;i--)
        {
            if(s1[i]==h)
            {
                ans.push_back(i);
                h--;
            }
        }
        int v=ans.size();
        reverse(ans.begin(),ans.end());
        h=-1;
        for(int i=n+1;i<=m;i++)
        {
            if(s1[i]==h)
            {
                ans.push_back(i);
                h--;
            }
        }
        int mi=99999999;
        int len=ans.size();
        for(int i=max(v-k,0);i+k-1<len&&i+k-1<v+k;i++)
            {
                if(i==v-k)mi=min(mi,n-ans[v-k]+1);
                else if(i==v){mi=min(ans[v+k-1]-n,mi);break;}
                else
                mi=min(mi,ans[i+k-1]-ans[i]+1);
            }
        cout<<mi<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值