BZOJ4410

本文介绍了一种利用最小生成树思想解决特定问题的方法。通过对给定数据进行预处理,找到最优解来降低整体成本。文章详细阐述了算法的实现过程,并通过实例说明了如何运用该算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:

首先数据规模连扫一遍都不可以
但是可以发现是同构的 也就是每一行每一列的间隔长度之类都相同
那就大胆猜想一定是整行整列地开门
用最小生成树的思想 先开代价小的
一些细节问题最好画一下

/**************************************************************
    Problem: 4410
    User: DtenSherlock
    Language: C++
    Result: Accepted
    Time:116 ms
    Memory:1672 kb
****************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
typedef long long LL;
const int imax=25000+229; 
const LL inf=1LL<<60;
LL A,B; int n,m;
LL x[imax],y[imax]; 

void iread()
{
    scanf("%lld%lld%d%d",&A,&B,&n,&m);
    for(int i=1;i<=n;i++) scanf("%lld",&x[i]); x[++n]=A;
    sort(x+1,x+n+1);
    for(int i=n;i>=1;i--) x[i]=x[i]-x[i-1]; 
    sort(x+1,x+n+1);
    for(int i=1;i<=m;i++) scanf("%lld",&y[i]); y[++m]=B;
    sort(y+1,y+m+1);
    for(int i=m;i>=1;i--) y[i]=y[i]-y[i-1];
    sort(y+1,y+m+1);
}

void iwork()
{
    int Max=n*m-1; LL ans=0;
    int nowx=1; int nowy=1; 
    int countx=0; int county=0; 
    int i=0;
    bool havex=0; bool havey=0;
    for(i;i<=Max;)
    {   

        if(x[nowx]<y[nowy])
        {
            if(!havex||!havey){ 
                ans+=x[nowx]*(LL)(m-1);
                i+=m-1;
            }
            else{
                ans+=x[nowx]*(LL)(m-countx);
                i+=m-countx;    
            }
            havex=1;
            county++; 
            nowx++;
        }
        else
        {
            if(!havex||!havey){
                ans+=(LL)y[nowy]*(n-1);
                i+=n-1;
            }
            else{
                ans+=(LL)y[nowy]*(n-county);
                i+=n-county;
            }
            havey=1;
            countx++;
            nowy++;
        } 

    }   
    printf("%lld\n",ans);
}

int main()
{
    iread();
    iwork(); 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值