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

[ Submit][ Status][ Discuss]

题解:费用流

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

正确的做法是:



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#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<int> 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]]<dis[x]+c[i]&&remain[i]){
			dis[v[i]]=dis[x]+c[i];
			last[v[i]]=i;
			if (!can[v[i]]){
				can[v[i]]=1; 
				p.push(v[i]);
			}
		}
		can[x]=0;
	}
	if (dis[t]<0) return false;
	int flow=addflow(s,t);
    mx+=flow*dis[t];
    return true;
}
void solve(int s,int t)
{
	while (spfa(s,t));
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d%d%d",&n,&m,&k);
	tot=-1;
	memset(point,-1,sizeof(point));
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	int s=1; int t=n+2;
	add(s,2,k,0);
	for (int i=1;i<=n;i++){
		add(i+1,i+2,k,0);
		if (i+m>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);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值