1 条题解

  • 0
    @ 2025-4-12 21:43:14

    C++ :

    //524K	188MS
    #include<stdio.h>
    #include<algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    int d[5007];
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n,m,len;
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d",&len,&n,&m);
            d[0]=0;//起点
            for(int i=1; i<=n; i++)
                scanf("%d",&d[i]);
            d[n+1]=len;//终点
            n+=2;
            sort(d,d+n);
            int l=inf,r=d[n-1],mid;
            for(int i=1; i<n; i++)//求两块石头之间最短距离
                l=min(l,d[i]-d[i-1]);
            while(l<=r)
            {
                mid=(l+r)/2;
                long long s=0,e=1,count=0;
                while(e<n)//枚举所有石头
                {
                    if(d[e]-d[s]>=mid)//如果这两块石头的距离大于mid,则不需要移除,更新相邻的石头
                    {
                        s=e;
                        e++;
                    }
                    else//如果这两块石头的距离小于mid,那么需要移除一块石头,count++,然后右面的石头往后移
                    {
                        e++;
                        count++;
                    }
                }
                if(count>m)r=mid-1;//如果需要移除的石头的数量大于要求的数量,取下限
                else l=mid+1;//否则取上限
            }
            printf("%d\n",r);
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    1329
    时间
    2000ms
    内存
    2048MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者