C. Pursuit Cf #733 (Div.1+Div. 2)

https://codeforces.com/contest/1530/problem/C

题目描述:

You and your friend Ilya are participating in an individual programming contest consisting of multiple stages. A contestant can get between 0 and 100 points, inclusive, for each stage, independently of other contestants.

Points received by contestants in different stages are used for forming overall contest results. Suppose that k stages of the contest are completed. For each contestant, k−⌊k/4⌋ stages with the highest scores are selected, and these scores are added up. This sum is the overall result of the contestant. (Here ⌊t⌋ denotes rounding t down.)

For example, suppose 9 stages are completed, and your scores are 50,30,50,50,100,10,30,100,50. First, 7 stages with the highest scores are chosen — for example, all stages except for the 22-nd and the 66-th can be chosen. Then your overall result is equal to 50+50+50+100+30+100+50=430.

As of now, nn stages are completed, and you know the points you and Ilya got for these stages. However, it is unknown how many more stages will be held. You wonder what the smallest number of additional stages is, after which your result might become greater than or equal to Ilya's result, at least in theory. Find this number!

输入:

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤10^5) — the number of completed stages.

The second line contains nn integers a1,a2,…,an (0≤ai≤100) — your points for the completed stages.

The third line contains nn integers b1,b2,…,bn (0≤bi≤1000≤bi≤10) — Ilya's points for the completed stages.

It is guaranteed that the sum of nn over all test cases does not exceed 10^5.

输出:

For each test case print a single integer — the smallest number of additional stages required for your result to be able to become greater than or equal to Ilya's result.

If your result is already not less than Ilya's result, print 0.

样例:

input

5
1
100
0
1
0
100
4
20 30 40 50
100 100 100 100
4
10 20 30 40
100 100 100 100
7
7 59 62 52 27 31 55
33 35 50 98 83 80 64

output

0
1
3
4
2

Note

In the first test case, you have scored 100 points for the first stage, while Ilya has scored 0. Thus, your overall result (100) is already not less than Ilya's result (0).

In the second test case, you have scored 0 points for the first stage, while Ilya has scored 100. A single stage with an opposite result is enough for both your and Ilya's overall scores to become equal to 100.

In the third test case, your overall result is 30+40+50=120, while Ilya's result is 100+100+100=300. After three additional stages your result might become equal to 420, while Ilya's result might become equal to 400.

In the fourth test case, your overall result after four additional stages might become equal to 470, while Ilya's result might become equal to 400. Three stages are not enough.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1e6+5;    //数组范围开1e5+5会越界 
int a[N],b[N];  //a数组记录your points;b数组记录llya's points
int cmp(int a,int b){
    return a > b;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int m = n-floor(1.0*n/4);   //需要计算的前m个最高分 
        memset(a,0,sizeof(a));      //清空数组a 
        memset(b,0,sizeof(b));      //清空数组b
        for(int i=0;i<n;i++) cin>>a[i];
        for(int i=0;i<n;i++) cin>>b[i];
        sort(a,a+n);                //a数组从小到大排序 
        sort(b,b+n,cmp);            //b数组从大到小排序
        int res = 0;                //res记录加赛数量 
        int suma = 0,sumb = 0;      //suma记录a总分 sumb记录b总分 
        for(int i=n-1;i>n-1-m;i--) suma += a[i];
        for(int i=0;i<m;i++) sumb += b[i];
        int pos = n-m;
        if(suma >= sumb) res = 0;
        else{
            while(suma < sumb){ //当你的分低于llya的分时 
                res++;          //加赛 
                n++;
                a[n-1] = 100;   //a数组末尾加100,因为每场分数的数据范围不大于100,所以更新后的a数组仍然有序
                b[n-1] = 0;     //b数组末尾加0,因为每场分数的数据范围不小于0,同理,b数组仍然有序
                suma += 100;    //更新a的总分,b的总分不变
                int p = m;              //保留原m数据 
                m = n-floor(1.0*n/4);   //更新n++后需要计算总分的前m个最高分 
                if(p == m) suma -= a[pos++];//更新你的总分,去掉前一个最低分 
                else sumb += b[p++];    //更新llya的总分 
            }
        }
        cout<<res<<'\n';
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值