• 1175C.Electrification(尺取)


    在OX轴上给您n个点a1,a2,…,an。现在,要求您在OX轴上找到这样一个整数点x,使得fk(x)最小可能。

    函数fk(x)可以用以下方式描述:

    形成距离列表d1,d2,…,dn,其中di = | ai-x | (ai和x之间的距离);
    以降序对列表d进行排序;
    结果是dk + 1。
    如果有多个最佳答案,则可以打印其中的任何一个。

    题解:

    从左到右遍历每组连续的K个点,每组顶点的最左和最右元素的距离除2就是第K大的最小值,取最小即可。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+100;
    int T;
    int a[maxn];
    int main () {
        scanf("%d",&T);
        int N,K;
        while (T--) {
            scanf("%d%d",&N,&K);
            for (int i=1;i<=N;i++) scanf("%d",&a[i]);
            int l=1;
            int r=K+1;
            int ans=0;
            int Min=1e9;
            while (l<=N-K) {
                int mid=(a[l]+a[r])>>1;
                int nowX=a[r]-mid;
                if (nowX<Min) {
                    Min=nowX;
                    ans=mid;
                }
                l++,r++;
            }
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    NSIS制作安装程序
    poj_1011木棒
    hdoj_1312Red and Black
    搜索题目推荐及解题报告
    应届生就职前要读的几本书
    poj_1564Sum It Up
    priority_queue用法
    hdoj_2952Counting Sheep
    poj_1154LETTERS
    poj_2362
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/12693137.html
Copyright © 2020-2023  润新知