G - Postman ZOJ - 4096 【 思维题--送信只需要判断最后的最长路径 】

版权声明:转载请注明出处,谢谢。 https://blog.csdn.net/Mercury_Lc/article/details/89363264

G - Postman

 ZOJ - 4096 

&:其他的地方无论怎么送,都要有来一次回到 0 点去拿剩余的信件,所以路程都是这个点的坐标乘以二,只要判断最后一次的 k 封往同一侧【 这里是指在 0 的同一侧】的那边送就可以了,题目中说最后一次可以不必回到 0 ,所以最后一次肯定选最长的那个点,那样就不必再返回,省去的路就是最大的了。

&:答案 = 所有按 k 个一组送信的点 * 2  - 单侧最远的那个点。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll k;
ll solve(ll a[], ll n)
{
    ll p = n;
    ll res = 0;
    while(p > 0)
    {
        res += a[p]*2ll;
        p -= k;
    }
    return res;
}
ll a[100005];
ll b[100005];
int main()
{
    ll t,n;
    scanf("%lld", &t);
    while(t--)
    {
        scanf("%lld %lld", &n, &k);
        ll p1,p2,x;
        p1=0;p2=0;
        for(ll i = 0; i < n; i ++)
        {
            scanf("%lld", &x);
            if(x>0)a[++p1] = x;
            else if(x < 0) b[++p2] = -x;
        }
        ll res = 0;
        res = 0;
        sort(a+1,a+1+p1);
        res += solve(a,p1);
        sort(b+1,b+1+p2);
        res += solve(b,p2);
        ll tmp = max(a[p1],b[p2]);
        res -= tmp;
        printf("%lld\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/89363264
ZOJ