离谱,Luogu100pt,LOJ T飞80pt了
查看原帖
离谱,Luogu100pt,LOJ T飞80pt了
108067
丛雨楼主2021/2/6 19:44
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
	T t=0;
	char k;
	bool vis=0;
	do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
	while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
	return vis?-t:t;
}
namespace Network{
	# define M 600000
	# define N 50010
	class Edge{
		public:
			int u,v,t;
	}G[M<<1];
	int f[N],tot,dis[N],cur[N];
	void init(){memset(f,-1,sizeof(f));tot=0;}
	void add(int u,int v,int t){
		G[tot].u=v;G[tot].v=t;G[tot].t=f[u];f[u]=tot++;
		G[tot].u=u;G[tot].v=0;G[tot].t=f[v];f[v]=tot++;
	}
	queue<int>q;
	bool bfs(int s,int t){
		memset(dis,0,sizeof(dis));
		while(q.size())q.pop();
		q.push(s);dis[s]=1;
		while(q.size()){
			int n=q.front();q.pop();
			for(int i=f[n];~i;i=G[i].t)
				if(G[i].v&&!dis[G[i].u]){
					dis[G[i].u]=dis[n]+1;
					if(G[i].u==t)return 1;
					q.push(G[i].u);
				}
		}return 0;
	}
	ll dfs(int n,ll f,int T){
		ll t=0;
		int c;
		if(n==T)return f;
		for(int &i=cur[n];~i;i=G[i].t)
			if(G[i].v&&dis[G[i].u]==dis[n]+1){
				c=dfs(G[i].u,min(f-t,0ll+G[i].v),T);
				G[i].v-=c;G[i^1].v+=c;t+=c;
				if(t==f)return t;
			}
		return t;
	}
	ll Dinic(int s,int t,int x=N-1){
		ll ans=0;
		while(bfs(s,t)){
			for(int i=0;i<=x;++i)cur[i]=f[i];
			ans+=dfs(s,1e18,t);
		}
		return ans;
	}
	# undef M
	# undef N
}
namespace Costflow{
	# define M 200005
	# define N 24005
	# define f Aduqefhwejfg
	# define q adwkjfwdjhgbv
	class Edge{
		public:
			int u,v,x,t;
	}G[M<<1];
	int f[N],tot,cur[N];
	void init(){memset(f,-1,sizeof(f));tot=0;}
	void add(int u,int v,int x,int t){
		G[tot].u=v;G[tot].v=t;G[tot].t=f[u];G[tot].x=x;f[u]=tot++;
		G[tot].u=u;G[tot].v=0;G[tot].t=f[v];G[tot].x=-x;f[v]=tot++;
	};
	ll dis[N];
	queue<int>q;
	bool vis[N];
	bool spfa(int s,int t){
		memset(dis,0x7f,sizeof(dis));
		q.push(s);dis[s]=0;
		while(q.size()){
			int n=q.front();q.pop();vis[n]=0;
			for(int i=f[n];~i;i=G[i].t)
				if(G[i].v&&dis[G[i].u]>dis[n]+G[i].x){
					dis[G[i].u]=dis[n]+G[i].x;
					if(!vis[G[i].u])q.push(G[i].u),vis[G[i].u]=1;
				}
		}
		if(dis[t]==dis[N-1])return 0;
		return 1;
	}
	int dfs(int n,int t,int f){
		if(n==t||!f)return f;
		int w=0;vis[n]=1;//cout<<n<<endl;
		for(int &i=cur[n];~i;i=G[i].t){
			int to=G[i].u;
			if(!G[i].v||dis[to]!=dis[n]+G[i].x)continue;
			if(!vis[to]){
				int x=dfs(to,t,min(f-w,G[i].v));
				G[i].v-=x;G[i^1].v+=x;w+=x;
				if(w==f)break;
			}
		}vis[n]=0;
		return w;
	}
	ll Min_cost(int s,int t,ll *w=NULL){
		ll ans=0;if(!w)w=new ll;*w=0;
		while(spfa(s,t)){
			for(int i=0;i<N;++i)cur[i]=f[i];
			int x=dfs(s,t,2e9);
			if(!x)break;
			ans+=x*dis[t];
		}
		return ans;
	}
	# undef q
	# undef f
	# undef M
	# undef N
}
# define inf 2e9
using namespace Costflow;
int a[1005],b[1005];
struct node{
	node *l,*r; 
	int v;
	node(){l=r=NULL;v=0;}
}*root,*root1;
int cnt,s;
void push(node *x,int l,int r,int v,int y){
	if(!x)return;
	if(l>v)return;
	if(r<=v){
		add(x->v,y,b[v]-b[r],1);
		return;
	}
	int mid=l+r>>1;
	push(x->l,l,mid,v,y);
	push(x->r,mid+1,r,v,y);
}
node* insert(node *x,int w,int l,int r,int v){
	node *n=new node;
	n->v=++cnt;
	if(x)add(x->v,n->v,0,inf),n->l=x->l,n->r=x->r;
	if(l==r)add(v,n->v,0,1);
	if(l==r)return n;
	int mid=l+r>>1;
	if(w<=mid)n->l=insert(n->l,w,l,mid,v),n->l&&(add(n->l->v,n->v,b[r]-b[mid],inf),1);
	else n->r=insert(n->r,w,mid+1,r,v),n->r&&(add(n->r->v,n->v,0,inf),1);
	return n; 
}
void push1(node *x,int l,int r,int v,int y){
	if(!x)return;
	if(r<=v)return;
	if(v<l){
		add(x->v,y,b[l]-b[v],1);
		return;
	}
	int mid=l+r>>1;
	push1(x->l,l,mid,v,y);
	push1(x->r,mid+1,r,v,y);
}
node* insert1(node *x,int w,int l,int r,int v){
	node *n=new node;
	n->v=++cnt;
	if(x)add(x->v,n->v,0,inf),n->l=x->l,n->r=x->r;
	if(l==r)add(v,n->v,0,1);
	if(l==r)return n;
	int mid=l+r>>1;
	if(w<=mid)n->l=insert1(n->l,w,l,mid,v),n->l&&(add(n->l->v,n->v,0,inf),1);
	else n->r=insert1(n->r,w,mid+1,r,v),n->r&&(add(n->r->v,n->v,b[mid+1]-b[l],inf),1);
	return n; 
}
int main(){
	init();s=read;
	int W=read;cnt=s<<1|1;
	for(int i=1;i<=s;++i)
		a[i]=b[i]=read+1;
	sort(b+1,b+s+1);
	b[0]=unique(b+1,b+s+1)-b-1;
	for(int i=1;i<=s;++i)
		a[i]=lower_bound(b+1,b+b[0]+1,a[i])-b;
	for(int i=1;i<=s;++i){
		push1(root1,1,b[0],a[i],i<<1);
		root1=insert1(root1,a[i],1,b[0],i<<1|1);
		push(root,1,b[0],a[i],i<<1);
		root=insert(root,a[i],1,b[0],i<<1|1);//cout<<i<<endl;
	}
	for(int i=1;i<=s;++i){
		add(i<<1,1,0,1);
		add(0,i<<1|1,0,1);
		add(0,i<<1,W,1);
	}
	cout<<Min_cost(0,1);
	return 0;
}
2021/2/6 19:44
加载中...