题解:
当然上来就离散咯。
先建一条数轴,每个点向后一个点建容量为$k$的边;
然后对于每一个区间,左端点向右端点建容量为$1$的边,费用为离散之前的$l-r$。代表可以跳过这段区间。
代码:
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 1050 const int inf = 0x3f3f3f3f; inline int rd() { int f=1,c=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();} return f*c; } int n,m,hed[N],cnt=-1,S,T,tot=0; struct seg { int l,r,w; }s[N]; struct Pair { int x,id,k; Pair(){} Pair(int x,int i,int k):x(x),id(i),k(k){} }p[N]; bool cmp(Pair a,Pair b) { return a.x<b.x; } struct EG { int to,nxt,w,c; }e[N*N*4]; void ae(int f,int t,int w,int c) { e[++cnt].to = t; e[cnt].nxt = hed[f]; e[cnt].w = w; e[cnt].c = c; hed[f] = cnt; } void AE(int f,int t,int w,int c) { ae(f,t,w,c); ae(t,f,0,-c); } int dep[N],fl[N]; int pre[N],fa[N]; bool vis[N]; queue<int>q; bool spfa() { memset(dep,0x3f,sizeof(dep)); dep[S] = 0,fl[S] = inf,vis[S] = 1; q.push(S); while(!q.empty()) { int u = q.front(); q.pop(); for(int j=hed[u];~j;j=e[j].nxt) { int to = e[j].to; if(e[j].w&&dep[to]>dep[u]+e[j].c) { dep[to] = dep[u]+e[j].c; fl[to] = min(fl[u],e[j].w); pre[to] = j,fa[to] = u; if(!vis[to]) { vis[to] = 1; q.push(to); } } } vis[u] = 0; } return dep[T]!=inf; } int mcmf() { int ret = 0; while(spfa()) { ret+=fl[T]*dep[T]; int u = T; while(u!=S) { e[pre[u]].w-=fl[T]; e[pre[u]^1].w+=fl[T]; u = fa[u]; } } return ret; } int main() { n = rd(),m = rd(); S = 0,T = 1030; memset(hed,-1,sizeof(hed)); int k = 0; for(int i=1;i<=n;i++) { s[i].l=rd(),s[i].r=rd(),s[i].w=s[i].r-s[i].l; p[++tot] = Pair(s[i].l,i,0); p[++tot] = Pair(s[i].r,i,1); } sort(p+1,p+1+tot,cmp); for(int las=-1,i=1;i<=tot;i++) { if(las!=p[i].x) { las = p[i].x; k++; } if(!p[i].k)s[p[i].id].l = k; else AE(s[p[i].id].l,k,1,-s[p[i].id].w); } AE(S,1,m,0);AE(k,T,m,0); for(int i=1;i<k;i++) AE(i,i+1,m,0); printf("%d ",-mcmf()); return 0; }