组队赛-12(Team match – 12)

L-candy machine

用sort过不了,用手写快排过的。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
ll a[N],sum[N];
void qp(int l,int r,ll a[])
{
    if (l>=r)return;
    int i=l-1;
    int j=r+1;
    ll x=a[(l+r)/2];
    while (i<j)
    {
        do i++;while (a[i]<x);
        do j--;while (a[j]>x);
        if (i<j)swap(a[i],a[j]);
    }
    qp(l,j,a);
    qp(j+1,r,a);
}
signed main()
{
    int n;
    scanf("%d",&n);
    for (int i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
    }
    qp(1,n,a);
    sum[1]=a[1];
    for (int i=1; i<=n; i++)
    {
        sum[i]=sum[i-1]+a[i];
        //cout<<sum[i]<<"\n";
    }
    int ans=0;
    int cnt=0;
    for (int i=2; i<=n; i++)
    {
        int t=sum[i]/i;
        //cout<<t<<"\n";
        if (a[i]>t)
        {
            cnt++;
            int w=i-cnt+1;
            while (a[w++]<=t)cnt--;
        }
        ans=max(ans,cnt);
    }
    printf("%d\n",ans);
    return 0;
}
————————

L-candy machine

It can’t be done with sort. It’s almost done with handwriting.

#include <iostream>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
ll a[N],sum[N];
void qp(int l,int r,ll a[])
{
    if (l>=r)return;
    int i=l-1;
    int j=r+1;
    ll x=a[(l+r)/2];
    while (i<j)
    {
        do i++;while (a[i]<x);
        do j--;while (a[j]>x);
        if (i<j)swap(a[i],a[j]);
    }
    qp(l,j,a);
    qp(j+1,r,a);
}
signed main()
{
    int n;
    scanf("%d",&n);
    for (int i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
    }
    qp(1,n,a);
    sum[1]=a[1];
    for (int i=1; i<=n; i++)
    {
        sum[i]=sum[i-1]+a[i];
        //cout<<sum[i]<<"\n";
    }
    int ans=0;
    int cnt=0;
    for (int i=2; i<=n; i++)
    {
        int t=sum[i]/i;
        //cout<<t<<"\n";
        if (a[i]>t)
        {
            cnt++;
            int w=i-cnt+1;
            while (a[w++]<=t)cnt--;
        }
        ans=max(ans,cnt);
    }
    printf("%d\n",ans);
    return 0;
}