当前位置:网站首页>Children play games (greed, prefix and) - Niuke winter vacation training camp

Children play games (greed, prefix and) - Niuke winter vacation training camp

2022-06-26 07:20:00 Sss_ xxh、

Original link

subject

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-20xD2fDG-1644926053066)(https://hbn31pkbuk.feishu.cn/space/api/box/stream/download/asynccode/?code=YjcxMjIxNTYwYTg3Y2E4MGNiZWFjMDBhMDFiMmRmZGFfazBraWlyWHd0WklBUjBFTVVWbWJjU21mOUpnTEVIWkZfVG9rZW46Ym94Y243bW9PbEVab1BkN1hrelRSdGxkNGlnXzE2NDQ5MjI5MzE6MTY0NDkyNjUzMV9WNA)]

Ideas

  1. First, meet the requirements ( Every two noisy children need a quiet child ), Well, at most there can only be n / 2 n/2 n/2 A noisy child
  2. If the number of quiet children is insufficient n / 2 + n   m o d   2 n/2+n\ mod\ 2 n/2+n mod 2 , It outputs -1.
  3. For the rest, let the quiet children and the noisy children rank in descending order of happiness .
  4. Then find the prefix sum
  5. The last comparison is in the least use n / 2 n/2 n/2 What is the greatest happiness of a quiet child .

Code

//
// Created by saber on 2022/2/15.
//

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int suma[N] = {
    0};
int sumb[N] = {
    0};
bool cmp(int a, int b)
{
    
        return a > b;
}
int main()
{
    
    int t; cin >> t;
    while (t -- )
    {
    
        int a, b, n;
        cin >> a >> b >> n;
        memset(suma, 0, sizeof suma);
        memset(sumb, 0, sizeof sumb);
        for (int i = 1; i <= a; i ++ )
        {
    
            cin >> suma[i];
        }
        for (int i = 1; i <= b; i ++ )
        {
    
            cin >> sumb[i];
        }
        sort(suma + 1, suma + 1 + a, cmp);
        sort(sumb + 1, sumb + 1 + b, cmp);
    
        for (int i = 1; i <= a; i ++ )
        {
    
                suma[i] += suma[i - 1];
        }
        for (int i = 1; i <= b; i ++ )
        {
    
                sumb[i] += sumb[i - 1];
        }
    
        int maxn = 0;
        int f = n/2 + (n % 2);
        if (a < f)
        {
    
                cout << -1 << endl;
                continue;
                }
            
        for (int i = f; i <= min(a, n); i ++ )
        {
    
                maxn = max(suma[i] + sumb[n - i], maxn);
                }
                cout << maxn << endl;
    }

    return 0;
}

summary

I don't understand , Why such a simple topic , But I couldn't write it out during the game .

原网站

版权声明
本文为[Sss_ xxh、]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202171130079605.html