Codeforces Round #658 (Div. 2)(补题)

A. Common Subsequence time limit per test1 second memory limit per
test256 megabytes inputstandard input outputstandard output You are
given two arrays of integers a1,…,an and b1,…,bm.

Your task is to find a non-empty array c1,…,ck that is a subsequence
of a1,…,an, and also a subsequence of b1,…,bm. If there are multiple
answers, find one of the smallest possible length. If there are still
multiple of the smallest possible length, find any. If there are no
such arrays, you should report about it.

A sequence a is a subsequence of a sequence b if a can be obtained
from b by deletion of several (possibly, zero) elements. For example,
[3,1] is a subsequence of [3,2,1] and [4,3,1], but not a subsequence
of [1,3,3,7] and [3,10,4].

Input The first line contains a single integer t (1≤t≤1000) — the
number of test cases. Next 3t lines contain descriptions of test
cases.

The first line of each test case contains two integers n and m
(1≤n,m≤1000) — the lengths of the two arrays.

The second line of each test case contains n integers a1,…,an
(1≤ai≤1000) — the elements of the first array.

The third line of each test case contains m integers b1,…,bm
(1≤bi≤1000) — the elements of the second array.

It is guaranteed that the sum of n and the sum of m across all test
cases does not exceed 1000 (∑i=1tni,∑i=1tmi≤1000).

Output For each test case, output “YES” if a solution exists, or “NO”
otherwise.

If the answer is “YES”, on the next line output an integer k
(1≤k≤1000) — the length of the array, followed by k integers c1,…,ck
(1≤ci≤1000) — the elements of the array.

If there are multiple solutions with the smallest possible k, output
any.

题意:大概是判断a,b数组是否有中最小长度相同子序列,说白了就是找相同的数。

void solve()
{
    int num[2001];
    int n,m;
    cin>>n>>m;
    fors(i,1001)num[i]=0;
    fors(i,n)
    {
        int k;
        cin>>k;
        num[k]=1;
    }
    int y=-1;
    fors(i,m)
    {
        int ms;
        cin>>ms;
        if(num[ms]){
            y=ms;
        }
 
    }
    if(y!=-1){
        cout<<"YES\n"<<1<<' '<<y<<endl;
    }
    else cout<<"NO"<<endl;
}

B. Sequential Nim time limit per test1 second memory limit per test256
megabytes inputstandard input outputstandard output There are n piles
of stones, where the i-th pile has ai stones. Two people play a game,
where they take alternating turns removing stones.

In a move, a player may remove a positive number of stones from the
first non-empty pile (the pile with the minimal index, that has at
least one stone). The first player who cannot make a move (because all
piles are empty) loses the game. If both players play optimally,
determine the winner of the game.

Input The first line contains a single integer t (1≤t≤1000) — the
number of test cases. Next 2t lines contain descriptions of test
cases.

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

The second line of each test case contains n integers a1,…,an
(1≤ai≤109) — ai is equal to the number of stones in the i-th pile.

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

Output For each test case, if the player who makes the first move will
win, output “First”. Otherwise, output “Second”.

题意:A,B两人分别从几堆石头中抽取石头,最少抽取一个。最后没有石头抽的一方为输,判断哪一方胜利。
思路:当堆的石头数>1时,先下手的一方可以任意改变自己的抽取石头数使自己胜利,所以这就是判断在谁先抽到石头数>1的堆,所以只要判断前缀1的个数。

int main()
{
int t;
cin >> t;
while(t--){
    int n;
    cin >>n;
    for(int i=0;i<n;i++)
    cin >> mm[i];
    int rrr=0;
    for(rrr;mm[rrr]==1&&rrr<n-1;rrr++);
    if(rrr&1)cout << "Second" << endl;
    else cout << "First" << endl;
    }
}

C1&C2. Prefix Flip (Easy Version) time limit per test1 second memory
limit per test256 megabytes inputstandard input outputstandard output
This is the easy version of the problem. The difference between the
versions is the constraint on n and the required number of operations.
You can make hacks only if all versions of the problem are solved.

There are two binary strings a and b of length n (a binary string is a
string consisting of symbols 0 and 1). In an operation, you select a
prefix of a, and simultaneously invert the bits in the prefix (0
changes to 1 and 1 changes to 0) and reverse the order of the bits in
the prefix.

For example, if a=001011 and you select the prefix of length 3, it
becomes 011011. Then if you select the entire string, it becomes
001001.

Your task is to transform the string a into b in at most 3n
operations. It can be proved that it is always possible.

Input The first line contains a single integer t (1≤t≤1000) — the
number of test cases. Next 3t lines contain descriptions of test
cases.

The first line of each test case contains a single integer n
(1≤n≤1000) — the length of the binary strings.

The next two lines contain two binary strings a and b of length n.

It is guaranteed that the sum of n across all test cases does not
exceed 1000.

Output For each test case, output an integer k (0≤k≤3n), followed by k
integers p1,…,pk (1≤pi≤n). Here k is the number of operations you use
and pi is the length of the prefix you flip in the i-th operation.

题目大意为:把二进制字符串s进行反转小于总长度的前缀并对前缀元素进行亦或操作改造为字符串ss,输出操作数及每次操作的前缀长度。
思路:双指针l,r。在反转数为偶数时从s尾部匹配与ss相同的字符,然后将l处的字符亦或为与r处相反的字符,将剩下的长度前缀反转并取亦或操作。反转数为奇数时,从s头部匹配与ss不同的字符,然后将r处的字符亦或为相同的字符,然后反转并亦或。

void solve()
{
    int len;
    cin>>len;
    string s,ss;
    cin>>s;
    cin>>ss;
    int n=0;
    int l=0,r=len-1;
    len--;
    vector<int>ans;
    while(l<=r)
    {
        if((n&1)==0)
        {
            while(s[r]==ss[len]&&l<=r){/*cout<<r<<'<'<<len<<endl;*/r--,len--;}
            if(r<l)break;
            if(s[l]==ss[len]){
                if(l!=r)
                ans.push_back(1);
            }
            ans.push_back(r-l+1);
            n++;l++; len--;
        }
        else
        {
        while(s[l]!=ss[len]&&l<=r){/*cout<<l<<'>'<<len<<endl;*/l++,len--;}
        if(l>r)break;
        if(s[r]!=ss[len]){
            if(l!=r)
            ans.push_back(1);
        }ans.push_back(r-l+1);
        n++;r--;len--;
        }
    }
    cout<<ans.size()<<' ';
    showvectorT(ans,0,int(ans.size()));
}

D. Unmerge time limit per test1 second memory limit per test256
megabytes inputstandard input outputstandard output Let a and b be two
arrays of lengths n and m, respectively, with no elements in common.
We can define a new array merge(a,b) of length n+m recursively as
follows:

If one of the arrays is empty, the result is the other array. That is,
merge(∅,b)=b and merge(a,∅)=a. In particular, merge(∅,∅)=∅. If both
arrays are non-empty, and a1<b1, then
merge(a,b)=[a1]+merge([a2,…,an],b). That is, we delete the first
element a1 of a, merge the remaining arrays, then add a1 to the
beginning of the result. If both arrays are non-empty, and a1>b1, then
merge(a,b)=[b1]+merge(a,[b2,…,bm]). That is, we delete the first
element b1 of b, merge the remaining arrays, then add b1 to the
beginning of the result. This algorithm has the nice property that if
a and b are sorted, then merge(a,b) will also be sorted. For example,
it is used as a subroutine in merge-sort. For this problem, however,
we will consider the same procedure acting on non-sorted arrays as
well. For example, if a=[3,1] and b=[2,4], then merge(a,b)=[2,3,1,4].

A permutation is an array consisting of n distinct integers from 1 to
n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but
[1,2,2] is not a permutation (2 appears twice in the array) and
[1,3,4] is also not a permutation (n=3 but there is 4 in the array).

There is a permutation p of length 2n. Determine if there exist two
arrays a and b, each of length n and with no elements in common, so
that p=merge(a,b).

Input The first line contains a single integer t (1≤t≤1000) — the
number of test cases. Next 2t lines contain descriptions of test
cases.

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

The second line of each test case contains 2n integers p1,…,p2n
(1≤pi≤2n). It is guaranteed that p is a permutation.

It is guaranteed that the sum of n across all test cases does not
exceed 2000.

Output For each test case, output “YES” if there exist arrays a, b,
each of length n and with no common elements, so that p=merge(a,b).
Otherwise, output “NO”.

题意:merge操作从两个数组索引值为1的位置开始,将其中的较小值加入空数组,并从原数组中删除,将其中的较大者与较小者数组剩下的数比较。当一个数组已经遍历完将未遍历完数组元素存入新数组中。现在给你这个新数组,求这两个数组。
思路:当新数组一个大的数后面跟着若干个比它小的数只能说明他们是在同一个数组中,但这样的序列之间是独立的。将这样序列长度储存然后用完全背包处理。

void solve()
{
   int half;
   cin>>half;
   vector<int>a;
   initvectorT(a,half*2);
   vector<int>ele;
   int minn=-0x3f3f3f3f;int rt=0;
   //showvectorT(a,0,int(a.size()));
   for(int i=0;i<half*2;i++)
   {
       if(a[i]>minn){
 
        minn=a[i];  if(i)ele.push_back(rt);
        rt=1;
 
       }
       else rt++;
   }
   ele.push_back(rt);

   vector<int>dp(half+1);
   for(int i=1;i<=half;i++)
   dp[i]=-0x3f3f3f3f;
   for(int i=0;i<ele.size();i++)
   {
       for(int j=half;j>=ele[i];j--)
       {
           if(dp[j-ele[i]]!=-0x3f3f3f3f)dp[j]=1;
       }
   }
   if(dp[half]>=1)
   {
       cout<<"YES"<<endl;
   }
   else cout<<"NO"<<endl;
 
}

E. Mastermind time limit per test1 second memory limit per test256
megabytes inputstandard input outputstandard output In the game of
Mastermind, there are two players — Alice and Bob. Alice has a secret
code, which Bob tries to guess. Here, a code is defined as a sequence
of n colors. There are exactly n+1 colors in the entire universe,
numbered from 1 to n+1 inclusive.

When Bob guesses a code, Alice tells him some information about how
good of a guess it is, in the form of two integers x and y.

The first integer x is the number of indices where Bob’s guess
correctly matches Alice’s code. The second integer y is the size of
the intersection of the two codes as multisets. That is, if Bob were
to change the order of the colors in his guess, y is the maximum
number of indices he could get correct.

For example, suppose n=5, Alice’s code is [3,1,6,1,2], and Bob’s guess
is [3,1,1,2,5]. At indices 1 and 2 colors are equal, while in the
other indices they are not equal. So x=2. And the two codes have the
four colors 1,1,2,3 in common, so y=4.

Solid lines denote a matched color for the same index. Dashed lines
denote a matched color at a different index. x is the number of solid
lines, and y is the total number of lines. You are given Bob’s guess
and two values x and y. Can you find one possibility of Alice’s code
so that the values of x and y are correct?

Input The first line contains a single integer t (1≤t≤1000) — the
number of test cases. Next 2t lines contain descriptions of test
cases.

The first line of each test case contains three integers n,x,y
(1≤n≤105,0≤x≤y≤n) — the length of the codes, and two values Alice
responds with.

The second line of each test case contains n integers b1,…,bn
(1≤bi≤n+1) — Bob’s guess, where bi is the i-th color of the guess.

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

Output For each test case, on the first line, output “YES” if there is
a solution, or “NO” if there is no possible secret code consistent
with the described situation. You can print each character in any case
(upper or lower).

If the answer is “YES”, on the next line output n integers a1,…,an
(1≤ai≤n+1) — Alice’s secret code, where ai is the i-th color of the
code.

If there are multiple solutions, output any.

题意:n个小球有着不同的颜色,Bob给出了一个颜色序列,Alice看了后给出了两个数x,y。x代表按照每个小球的循序猜对的颜色,y代表不按照循序猜对的颜色,现在给你bob的序列和alice的x,y。输出一个小球颜色的序列。
待补。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值