Educational Codeforces Round 78 (Rated for Div. 2)

47846 단어 codeforces
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
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
"
;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
"
;return ;} for(int i=len1;i<len2;i++) {int k=i-len1;sum2[change(s1[k])]--;sum2[change(s1[i])]++;if(eq()){cout<<"YES
"
;return ;} } cout<<"NO
"
; } 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
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. 아직 30분 정도 방법이 떠올랐는데 아쉽게도 어딘가를 잘못 두드렸어요. 마지막 코드는 고칠수록 어지러워요. 다행이에요. 마지막으로 대략적인 생각을 해볼게요. n부터 1까지 접두사와 빨간색, 파란색 하나씩. - 1부터 1까지. - n+1에서 2n까지 접두사 하나를 유지하고 n에서 1,2,3...의 위치와 n+1에서 2,n123까지 처음 나타난 위치를 기록해요. 몇 번을 수정해야 돼요? 바로 k=max(빨간색, 파란색 개수)-min입니다.(빨간색 개수, 파란색 개수) 방금 기록된 접두사와 n에서 k까지의 위치를 n+1에서 2n까지 k까지의 위치를 마지막으로 k-1개마다 반복해서 최소값 출력을 찾으면 혼란스러울 수 있습니다. 구체적으로 코드를 보십시오.
#include
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;
}

좋은 웹페이지 즐겨찾기