Codeforces - F. Almost Permutation

Recently Ivan noticed an array a while debugging his code. Now Ivan can’t remember this array, but the bug he was trying to fix didn’t go away, so Ivan thinks that the data from this array might help him to reproduce the bug.

Ivan clearly remembers that there were n elements in the array, and each element was not less than 1 and not greater than n. Also he remembers q facts about the array. There are two types of facts that Ivan remembers:

1 li ri vi — for each x such that li ≤ x ≤ ri ax ≥ vi;
2 li ri vi — for each x such that li ≤ x ≤ ri ax ≤ vi.
Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the q facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the cost of array as follows:

, where cnt(i) is the number of occurences of i in the array.

Help Ivan to determine minimum possible cost of the array that corresponds to the facts!

Input
The first line contains two integer numbers n and q (1 ≤ n ≤ 50, 0 ≤ q ≤ 100).

Then q lines follow, each representing a fact about the array. i-th line contains the numbers ti, li, ri and vi for i-th fact (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ vi ≤ n, ti denotes the type of the fact).

Output
If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array.

Examples
inputCopy
3 0
outputCopy
3
inputCopy
3 1
1 1 3 2
outputCopy
5
inputCopy
3 2
1 1 3 2
2 1 3 2
outputCopy
9
inputCopy
3 2
1 1 3 2
2 1 3 1
outputCopy
-1


就是给出每个位置的限制求最小费用。

每个位置的限制可以直接暴力区间修改。怎么求最小费用呢?MCMF即可。

对于每个数字单独拿出来当点,点到可以到的区间点连流量为1,费用为0的边。

区间点到T连流量为1,费用为0的边。

S到每个数字点有n条边,费用为 1 , 3 , 5 , 7 。。。。流量为1。最后MCMF即可。

AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=110,M=1e6+10;
int n,q,s,t,d[N],st[N],vis[N],low[N],up[N],res;
int head[N],nex[M],to[M],flow[M],w[M],tot=1;
inline void ade(int a,int b,int c,int d){
	to[++tot]=b; nex[tot]=head[a]; w[tot]=d; flow[tot]=c; head[a]=tot;
}
inline void add(int a,int b,int c,int d){ade(a,b,c,d);	ade(b,a,0,-d);}
inline int spfa(){
	queue<int> q; q.push(s); memset(d,inf,sizeof d); d[s]=0; memset(st,0,sizeof st);
	while(q.size()){
		int u=q.front();	q.pop();	vis[u]=0;
		for(int i=head[u];i;i=nex[i]){
			if(flow[i]&&d[to[i]]>d[u]+w[i]){
				d[to[i]]=d[u]+w[i];
				if(!vis[to[i]])	q.push(to[i]),vis[to[i]]=1;
			}
		}
	}
	return d[t]!=inf;
}
int dfs(int x,int f){
	if(x==t)	return res+=d[t]*f,f;
	int fl=0;	st[x]=1;
	for(int i=head[x];i&&f;i=nex[i]){
		if(flow[i]&&!st[to[i]]&&d[to[i]]==d[x]+w[i]){
			int mi=dfs(to[i],min(flow[i],f));
			flow[i]-=mi,flow[i^1]+=mi,fl+=mi,f-=mi;
		}
	}
	return fl;
}
signed main(){
	cin>>n>>q;	t=n*2+1;
	for(int i=1;i<=n;i++)	low[i]=1,up[i]=n;
	while(q--){
		int op,l,r,v;	cin>>op>>l>>r>>v;
		if(op==1){
			for(int i=l;i<=r;i++){
				low[i]=max(low[i],v);
				if(low[i]>up[i])	return puts("-1"),0;
			}
		}else{
			for(int i=l;i<=r;i++){
				up[i]=min(up[i],v);
				if(low[i]>up[i])	return puts("-1"),0;
			}
		}
	}
	for(int i=1;i<=n;i++){
		add(i+n,t,1,0);
		for(int j=low[i];j<=up[i];j++)	add(j,i+n,1,0);
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)	add(s,i,1,(j-1)*2+1);
	}
	while(spfa())	dfs(s,inf);
	cout<<res;
	return 0;
}
发布了479 篇原创文章 · 获赞 241 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104098220