跳到正文

剪贴板

剪贴板 b1prsixu

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#include"fish.h"
#define nm(x) x.begin(),x.end()
const int N=220000;
using namespace std;
using ll=long long;
using van=vector<int>;
struct fish
{
	int y,w;
	bool operator < (const fish &b) const
	{
		return y<b.y;
	}
};
vector<fish>pol[N];
vector<ll>f[2],g[2];
ll max_weights(int n,int m,van x,van y,van w)
{
	for(int i=0;i<m;++i)
		pol[x[i]].push_back({y[i],w[i]});
	for(int i=0;i<n;++i)pol[i].push_back({n,0});
	{
		sort(nm(pol[0]));
		int x=pol[0].size();
		f[0].resize(x),f[1].resize(x);
	}
	for(int i=1;i<n;++i)
	{
		sort(nm(pol[i]));
		int ps=pol[i-1].size(),nw=pol[i].size();
		g[0].clear(),g[1].clear(),
		g[0].resize(nw),g[1].resize(ps);
		ll tot=0;
		for(int z=0,t=0;z<ps;++z)
		{
			while(t<nw&&pol[i][t].y<pol[i-1][z].y)
				tot+=pol[i][t].w,++t;
			g[1][z]=tot;
		}
		tot=0;
		for(int z=0,t=0;z<nw;++z)
		{
			while(t<ps&&pol[i-1][t].y<pol[i][z].y)
				tot+=pol[i-1][t].w,++t;
			g[0][z]=tot;
		}
		ll mx1,mx2,mx3;mx1=mx2=mx3=tot=0;
		for(int z=0;z<ps;++z)
			mx1=max(f[0][z]-tot,mx1),
			mx2=max(f[1][z],mx2),
			mx3=max(f[1][z]+g[1][z],mx3),
			tot+=pol[i-1][z].w;
		f[0].clear(),f[1].clear(),
		f[0].resize(nw),f[1].resize(nw),tot=0;
		for(int z=0;z<nw;++z)
			f[0][z]=max(mx1+g[0][z],mx2),
			f[1][z]=max(f[0][z],mx3-tot),
			tot+=pol[i][z].w;
	}
	return *max_element(nm(f[1]));
}