原文链接Problem - 1530C - Codeforces
C. Pursuit
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You and your friend Ilya are participating in an individual programming contest consisting of multiple stages. A contestant can get between 00 and 100100 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 kk stages of the contest are completed. For each contestant, k−⌊k4⌋k−⌊k4⌋ stages with the highest scores are selected, and these scores are added up. This sum is the overall result of the contestant. (Here ⌊t⌋⌊t⌋ denotes rounding tt down.)
For example, suppose 99 stages are completed, and your scores are 50,30,50,50,100,10,30,100,5050,30,50,50,100,10,30,100,50. First, 77 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=43050+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!
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000). Description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the number of completed stages.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1000≤ai≤100) — your points for the completed stages.
The third line contains nn integers b1,b2,…,bnb1,b2,…,bn (0≤bi≤1000≤bi≤100) — Ilya's points for the completed stages.
It is guaranteed that the sum of nn over all test cases does not exceed 105105.
Output
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 00.
Example
input
Copy
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
Copy
0 1 3 4 2
Note
In the first test case, you have scored 100100 points for the first stage, while Ilya has scored 00. Thus, your overall result (100100) is already not less than Ilya's result (00).
In the second test case, you have scored 00 points for the first stage, while Ilya has scored 100100. A single stage with an opposite result is enough for both your and Ilya's overall scores to become equal to 100100.
In the third test case, your overall result is 30+40+50=12030+40+50=120, while Ilya's result is 100+100+100=300100+100+100=300. After three additional stages your result might become equal to 420420, while Ilya's result might become equal to 400400.
In the fourth test case, your overall result after four additional stages might become equal to 470470, while Ilya's result might become equal to 400400. Three stages are not enough.
--------------------------------------------------------------------------------------------------------------------------------直接进行模拟就可以
# include<iostream>
# include<algorithm>
# include<iomanip>
# include<vector>
# include<map>
# include<math.h>
# include<cstring>
using namespace std;
typedef long long int ll;
int a[100000+10],b[100000+10];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i];
sort(a+1,a+1+n,cmp);
for(int i=1; i<=n; i++)
cin>>b[i];
sort(b+1,b+1+n,cmp);
int m=n-n/4,sum1=0,sum2=0;
for(int i=1; i<=m; i++)
sum1+=a[i];
for(int i=1; i<=m; i++)
sum2+=b[i];
if(sum1>=sum2)
{
cout<<0<<'\n';
continue;
}
int left1=m,left2=m+1,nowm=m,cnt=0,prem=m,nown=n;
while(1)
{
prem=nowm;
nown++;
cnt++;
nowm=nown-nown/4;
if(nowm>prem)
{
sum1+=100;
while(left2<=n)
{
if(b[left2]==0)
{
left2++;
continue;
}
sum2+=b[left2];
left2++;
break;
}
if(sum1>=sum2)
{
cout<<cnt<<'\n';
break;
}
continue;
}
int flag=0;
while(left1>=1)
{
if(a[left1]==100)
{
left1--;
continue;
}
flag=1;
sum1-=a[left1];
sum1+=100;
left1--;
break;
}
if(!flag)
{
sum1+=100;
}
if(sum1>=sum2)
{
cout<<cnt<<'\n';
break;
}
}
}
return 0;
}