题目链接:矩形分割 - 洛谷
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;
}