bzoj 1283: 序列(费用流)

1283: 序列

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 255   Solved: 141
[ Submit][ Status][ Discuss]

Description

给出一个长度为 的正整数序列Ci,求一个子序列,使得原序列中任意长度为 的子串中被选出的元素不超过K(K,M<=100) 个,并且选出的元素之和最大。

Input

第1行三个数N,m,k。 接下来N行,每行一个字符串表示Ci。

Output

最大和。

Sample Input

10 5 3
4 4 4 6 6 6 6 6 4 4

Sample Output

30

HINT

20%的数据:n<=10。
100%的数据:N<=1000,k,m<=100。Ci<=20000。

Source

By YM

[ Submit][ Status][ Discuss]

题解:费用流

这个题学长出互测的时候数据范围较小用一种比较智障的做法A掉了。但是这道题那么建图的时间复杂度是不科学的。

正确的做法是:

bzoj 1283: 序列(费用流)_第1张图片

bzoj 1283: 序列(费用流)_第2张图片

#include
#include
#include
#include
#include
#include
#define N 20033
#define inf 1000000000
using namespace std;
int n,m,k,mx;
int tot,point[N],next[N],v[N],remain[N],c[N];
int last[N],dis[N],can[N],a[N],ans;
void add(int x,int y,int z,int k)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; c[tot]=k;
	tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; c[tot]=-k;
}
int addflow(int s,int t)
{
	int now=t; int ans=inf;
	while (now!=s){
		ans=min(ans,remain[last[now]]);
		now=v[last[now]^1];
	}
	now=t;
	while (now!=s){
		remain[last[now]]-=ans;
		remain[last[now]^1]+=ans;
		now=v[last[now]^1];
	}
	return ans;
}
bool spfa(int s,int t)
{
	queue p;
	for (int i=1;i<=t;i++) dis[i]=-inf,can[i]=0;
	dis[s]=0; can[s]=1;
	p.push(s);
	while (!p.empty()){
		int x=p.front(); p.pop();
		for (int i=point[x];i!=-1;i=next[i])
		 if (dis[v[i]]n) add(i+1,t,1,a[i]);
		else add(i+1,i+m+1,1,a[i]);
	}
	solve(s,t);
	printf("%d\n",mx);
}



你可能感兴趣的:(网络流)