2023牛客多校(一)

Matches

限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述 

Walk Alone has two rows of matches, each of which contains n matches. He defines the distance between them as ∣d=∑i=1n​∣ai​−bi​∣, where ai​ and bi​ represent the height of the i-th match in the first row and the second row, respectively. Walk Alone wants to beautify the matches by shortening the distance, and you are asked to find out the minimum distance after performing at most one swap within one row. Note that the height can be negative.

输入描述:

The first line contains one integer  (1≤n≤106) (1≤n≤106), denoting the number of matches in each row.

The second and the third line both contains n integers, representing the height of matches in the first row and the second row, respectively. All of these numbers satisfy ∣≤109∣ai​∣,∣bi​∣≤109.

输出描述:

Print one line containing a single integer, the minimum distance you can get.

#include<iostream>
#include<algorithm>
#include<cmath>


using namespace std;
const long long N=1e6+5;
struct node
{
    int  x,y;
    int xy;
    friend bool operator<(node n1,node n2)
    {
        return n1.xy>n2.xy;
    }
}e[N];

signed main()
{
   ios::sync_with_stdio(false);
    cin.tie(0);
   // cout.tie(0);
    long long n;
    cin>>n;
    long long ans=0,tep=0;//注意要用long long
    for(long long i=1;i<=n;i++)
    {
        cin>>e[i].x;
        //scanf("%lld",&e[i].x);
    }
    for(long long i=1;i<=n;i++)
    {
        cin>>e[i].y;
         //scanf("%lld",&e[i].y);
        e[i].xy=abs(e[i].x-e[i].y);
        ans+=e[i].xy;
    }
    sort(e+1,e+1+n);
    tep=ans;
    long long ct=0;
    for(int i=1;i<=n/2+1;i++)
    
    { //  cout<<"dsk: "<<e[i].xy<<endl;
        for(int j=i+1;j<=n/2+1;j++)
        {
            ct++;
            ans=min(tep-e[i].xy-e[j].xy+abs(e[i].x-e[j].y)+abs(e[j].x-e[i].y),ans);
             if(ct>1e7)break;
        }
        if(ct>1e7)break;
    }
    cout<<ans;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值