洛谷P1324 矩形分割

题目链接:矩形分割 - 洛谷

2022/12/7 day3

思路:贪心 每次都切权最大的边 若切的是横边,则所有竖边要多切一刀,权值增加一个初始值,反之同理。

期间的错误:由于每切一条边,其他边的权值都要增大,所以要保证每次切得都是当前最大权值的边。可以将切后的矩形看成两块,分别求两块矩形的最小分割,所以比大小的值应选用矩形切割边权值的初始值,而不是修改后的值。

因为思路有问题,所以代码写比较恶心....

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef long long ll;

struct Edge
{
    ll w;
    ll pre;
    bool hs;

    bool operator<(const Edge x)const{
        return x.w<w;
    }
};

int main()
{
    ll n,m,temp;
    cin>>n>>m;
    vector<Edge>v;
    ll sum=0;

    for(int i=1;i<n;++i)
    {
        cin>>temp;
        v.push_back(Edge{temp,temp,true});
    }
    for(int i=1;i<m;++i)
    {
        cin>>temp;
        v.push_back(Edge{temp,temp,false});
    }

    sort(v.begin(),v.end());

    while(!v.empty())
    {
        //sort(v.begin(),v.end());//错误,应用初始权值排序
        sum+=v.begin()->w;
        for(vector<Edge>::iterator it=v.begin()+1;it!=v.end();it++)
        {
            if(v.begin()->hs!=it->hs)
            {
                it->w+=it->pre;
            }
        }
        v.erase(v.begin());
    }

    cout<<sum<<endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值