Codeforces Round #706 (Div. 2) 题解(贪心场)

题目链接https://codeforces.ml/contest/1496

A. Split it!

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Kawashiro Nitori is a girl who loves competitive programming.
One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.
Given a string s and a parameter k, you need to check if there exist k+1 non-empty strings a1,a2…,ak+1, such that
s=a1+a2+…+ak+ak+1+R(ak)+R(ak−1)+…+R(a1).
Here + represents concatenation. We define R(x) as a reversed string x. For example R(abcd)=dcba. Note that in the formula above the part R(ak+1) is intentionally skipped.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case description contains two integers n, k (1≤n≤100, 0≤k≤⌊n2⌋) — the length of the string s and the parameter k.

The second line of each test case description contains a single string s of length n, consisting of lowercase English letters.

Output
For each test case, print “YES” (without quotes), if it is possible to find a1,a2,…,ak+1, and “NO” (without quotes) otherwise.

You can print letters in any case (upper or lower).

Example
inputCopy
7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa
outputCopy
YES
NO
YES
NO
YES
NO
NO
Note
In the first test case, one possible solution is a1=qw and a2=q.

In the third test case, one possible solution is a1=i and a2=o.

In the fifth test case, one possible solution is a1=dokidokiliteratureclub.

思路:左右对称的回文串,贪心使每个回文串都是一个字符,字串中间串为ak+1
首先特判,如果k*2==n的话,分不出2k+1个字串,直接输出NO
之后讨论

参考代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
        if(k==0)puts("YES");
        else if(n==k*2)puts("NO");
        else
        {
    
    
            int cnt=0;
            for(int i = 0; i<n/2; i++)
            {
    
    
                if(s[i]==s[n-i-1])
                {
    
    
                    cnt++;
                }
                else
                {
    
    
                    break;
                }
            }
            if(cnt<k)puts("NO");
            else puts("YES");
        }
    }
    return 0;
}

B. Max and Mex

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given a multiset S initially consisting of n distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.

You will perform the following operation k times:
Add the element ⌈a+b2⌉ (rounded up) into S, where a=mex(S) and b=max(S). If this number is already in the set, it is added again.
Here max of a multiset denotes the maximum integer in the multiset, and mex of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example:
mex({1,4,0,2})=3;
mex({2,5,1})=0.
Your task is to calculate the number of distinct elements in S after k operations will be done.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n, k (1≤n≤105, 0≤k≤109) — the initial size of the multiset S and how many operations you need to perform.

The second line of each test case contains n distinct integers a1,a2,…,an (0≤ai≤109) — the numbers in the initial multiset.

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

Output
For each test case, print the number of distinct elements in S after k operations will be done.

Example
inputCopy
5
4 1
0 1 3 4
3 1
0 1 4
3 0
0 1 4
3 2
0 1 2
3 2
1 2 3
outputCopy
4
4
3
5
3
Note
In the first test case, S={0,1,3,4}, a=mex(S)=2, b=max(S)=4, ⌈a+b2⌉=3. So 3 is added into S, and S becomes {0,1,3,3,4}. The answer is 4.

In the second test case, S={0,1,4}, a=mex(S)=2, b=max(S)=4, ⌈a+b2⌉=3. So 3 is added into S, and S becomes {0,1,3,4}. The answer is 4.

思路:unordered_map存出现过的数,并找到最大值,之后遍历找集合中第一个未出现的非负数,计算得出需要添加的数add。
有三种情况直接退出操作:
1.k已经变为0
2.add已经在集合中
3.add比n大,则答案直接是n+当前的k(举个栗子,{0,1,2}的mex是3,max是2,add便是3,也就是之后的每一步的add都是max+1)

参考代码:

#include <bits/stdc++.h>

using namespace std;

int a[100010];
int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        unordered_map<int,int>mp;
        int n,k;
        cin>>n>>k;
        int maxn=-1;
        for(int i = 1; i<=n; i++)
        {
    
    
            cin>>a[i];
            maxn=max(maxn,a[i]);
            mp[a[i]]++;
        }
        int ans=n,mex;
        for(int i = 0;; i++)
        {
    
    
            if(!mp[i])
            {
    
    
                mex=i;
                break;
            }
        }
        while(1)
        {
    
    

            if(k==0)break;
            for(int i = mex;; i++)
            {
    
    
                if(!mp[i])
                {
    
    
                    mex=i;
                    break;
                }
            }
            if(mex>=n)
            {
    
    
                ans=n+k;
                break;
            }
            if(mp[(mex+maxn+1)/2])
            {
    
    
                break;
            }
            else
            {
    
    
                k--;
                ans++;
                mp[(mex+maxn+1)/2]=1;
                maxn=max(maxn,(mex+maxn+1)/2);
                //cout<<"add="<<(mex+maxn+1)/2<<endl;
            }
        }
        cout<<ans<<endl;

    }
    return 0;
}

C. Diamond Miner

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Diamond Miner is a game that is similar to Gold Miner, but there are n miners instead of 1 in this game.

The mining area can be described as a plane. The n miners can be regarded as n points on the y-axis. There are n diamond mines in the mining area. We can regard them as n points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point (0,0)).

Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point (a,b) uses his hook to mine a diamond mine at the point (c,d), he will spend (a−c)2+(b−d)2−−−−−−−−−−−−−−−√ energy to mine it (the distance between these points). The miners can’t move or help each other.
The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤10) — the number of test cases. The description of the test cases follows.

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

Each of the next 2n lines contains two space-separated integers x (−108≤x≤108) and y (−108≤y≤108), which represent the point (x,y) to describe a miner’s or a diamond mine’s position. Either x=0, meaning there is a miner at the point (0,y), or y=0, meaning there is a diamond mine at the point (x,0). There can be multiple miners or diamond mines at the same point.

It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to n and the number of points on the y-axis is equal to n.

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

Output
For each test case, print a single real number — the minimal sum of energy that should be spent.

Your answer is considered correct if its absolute or relative error does not exceed 10−9.

Formally, let your answer be a, and the jury’s answer be b. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−9.

Example
inputCopy
3
2
0 1
1 0
0 -1
-2 0
4
1 0
3 0
-5 0
6 0
0 3
0 1
0 2
0 4
5
3 0
0 4
0 -3
4 0
2 0
1 0
-3 0
0 -10
0 -2
0 -10
outputCopy
3.650281539872885
18.061819283610362
32.052255376143336
Note
In the first test case, the miners are at (0,1) and (0,−1), while the diamond mines are at (1,0) and (−2,0). If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy 2–√+5–√.

思路:贪心,矿工和矿分别用两个数组存位置的绝对值,然后排序对应计算即可

参考代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll r[100010],m[100010];

int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int c1=0,c2=0;
        int n;
        cin>>n;
        n*=2;
        while(n--)
        {
    
    
            ll x,y;
            cin>>x>>y;
            if(x==0)
            {
    
    
                r[c1++]=abs(y);
            }else
            {
    
    
                m[c2++]=abs(x);
            }
        }
        sort(r,r+c1);
        sort(m,m+c2);
        double ans=0;
        for(int i = 0;i<c1;i++)
        {
    
    
            ans+=sqrt(r[i]*r[i]+m[i]*m[i]);
        }
        printf("%.10f\n",ans);
    }
    return 0;
}

D. Let’s Go Hiking

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

On a weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper.

A permutation p is written from left to right on the paper. First Qingshan chooses an integer index x (1≤x≤n) and tells it to Daniel. After that, Daniel chooses another integer index y (1≤y≤n, y≠x).

The game progresses turn by turn and as usual, Qingshan moves first. The rules follow:

If it is Qingshan’s turn, Qingshan must change x to such an index x′ that 1≤x′≤n, |x′−x|=1, x′≠y, and px′<px at the same time.
If it is Daniel’s turn, Daniel must change y to such an index y′ that 1≤y′≤n, |y′−y|=1, y′≠x, and py′>py at the same time.
The person who can’t make her or his move loses, and the other wins. You, as Qingshan’s fan, are asked to calculate the number of possible x to make Qingshan win in the case both players play optimally.

Input
The first line contains a single integer n (2≤n≤105) — the length of the permutation.

The second line contains n distinct integers p1,p2,…,pn (1≤pi≤n) — the permutation.

Output
Print the number of possible values of x that Qingshan can choose to make her win.

Examples
inputCopy
5
1 2 5 4 3
outputCopy
1
inputCopy
7
1 2 4 6 5 3 7
outputCopy
0
Note
In the first test case, Qingshan can only choose x=3 to win, so the answer is 1.

In the second test case, if Qingshan will choose x=4, Daniel can choose y=1. In the first turn (Qingshan’s) Qingshan chooses x′=3 and changes x to 3. In the second turn (Daniel’s) Daniel chooses y′=2 and changes y to 2. Qingshan can’t choose x′=2 because y=2 at this time. Then Qingshan loses.

思路:博弈

1 如果所有的山中存在三条及以上的最长边,则先手必输,因为对手肯定会选择另一座山的山脚上山,两人能走的步数相同,但是先手会先无法行动,所以有一个结论:答案不是0就是1
用随机数的奇偶定结果
2 如果两条最长边在同一座山上(左右各一边),则判断边长是否为奇数并且长度大于等于3,若满足,则先手必赢,否则先手必输
理由:二者都可以采取卡先手和步数多获胜,若最长边为偶数,则后手可以选择一侧的山脚,无论先手采取走另一侧(和对手步数一样而输),走同侧(会被卡)都会输;若边长为奇数并且长度大于等于3,则先手可以选择走对手一侧去卡他,必赢。
3 其余情况先手都会输

参考代码:

#include <bits/stdc++.h>

using namespace std;
int a[100010];
int main()
{
    
    
    int n;
    cin>>n;
    for(int i = 1; i<=n; i++)
    {
    
    
        cin>>a[i];
    }
    int posz=0,posy=n,maxz=-1,maxy=-1;
    int cnt=1;
    for(int i = 2; i<=n; i++)
    {
    
    
        if(a[i]>a[i-1])cnt++;
        else cnt=1;
        if(cnt>maxz)
        {
    
    
            posz=i;
            maxz=cnt;
        }
    }
    cnt=1;
    for(int i = 2; i<=n; i++)
    {
    
    
        if(a[i]>a[i-1])cnt++;
        else cnt=1;
        if(cnt==maxz&&i!=posz)
        {
    
    
           cout<<0;
           return 0;
        }
    }
    cnt=1;
    for(int i = n-1; i>=1; i--)
    {
    
    
        if(a[i]>a[i+1])cnt++;
        else cnt=1;
        if(cnt>maxy)
        {
    
    
            posy=i;
            maxy=cnt;
        }
    }
    cnt=1;
    for(int i = n-1; i>=1; i--)
    {
    
    
        if(a[i]>a[i+1])cnt++;
        else cnt=1;
        if(maxy==cnt&&i!=posy)
        {
    
    
            cout<<0;
            return 0;
        }
    }
    if(maxz==maxy&&posz==posy)
    {
    
    
        if(maxz>=3&&maxz%2==1)
        cout<<1;
        else
        cout<<0;
        return 0;
    }
    else if(maxz==maxy)
    {
    
    
        cout<<0;
        return 0;
    }else
    {
    
    
        cout<<0;
    }
    return 0;
}

其余题目之后补充,谢谢支持

猜你喜欢

转载自blog.csdn.net/qq_51152918/article/details/114663908