Codeforces Gym 101190 NEERC 16 .D Delight for a Cat (上下界的费用流)
题意:就是N天,每天可以选择S或者E,每一天S或者E有自己的收益,求最大收益,且满足每个连续K天,只是有Ms天选择了S,Me天选择了E。
输出最大收益,以及对应的方案。
思路:和上一题有点像,不过加了下界,问题不大,直接去看题解就好了。
只需要把相邻的边容量改为最大-最小即可,费用为,表示最多流出去mx-mn这么多,留在这里的流量保证了下界。 对于输出方案,只需要看对应的流是否饱和即可。

#include<bits/stdc++.h>
#define ll long long
#define maxn 1010
using namespace std;
int To[maxn*],Laxt[maxn],Next[maxn*],cap[maxn*],cost[maxn*],tag[maxn];
int S,T,cnt=; ll dis[maxn],ans;//建边的时候注意开对应大小的空间
bool inq[maxn],vis[maxn];
deque<int>q;
void add(int u,int v,int c,int cc)
{ Next[++cnt]=Laxt[u];Laxt[u]=cnt;To[cnt]=v;cap[cnt]=c;cost[cnt]=cc; }
bool spfa()
{
for(int i=;i<=T;i++) inq[i]=;
for(int i=;i<=T;i++) dis[i]=1LL<<; //这样更新,必须保证T的编号最大
inq[T]=; dis[T]=; q.push_back(T);
while(!q.empty())
{
int u=q.front(); q.pop_front();
inq[u]=;
for(int i=Laxt[u];i;i=Next[i])
{
int v=To[i];
if(cap[i^]&&dis[v]>dis[u]-cost[i])
{
dis[v]=dis[u]-cost[i];
if(!inq[u]){
inq[v]=;
if(q.empty()||dis[v]>dis[q.front()]) q.push_back(v);
else q.push_front(v);
}
}
}
}
return dis[S]<(1LL<<);
}
int dfs(int u,int flow)
{
vis[u]=;
if(u==T||flow==) return flow;
int tmp,delta=;
for(int i=Laxt[u];i;i=Next[i])
{
int v=To[i];
if((!vis[v])&&cap[i]&&dis[v]==dis[u]-cost[i])
{
tmp=dfs(v,min(cap[i],flow-delta));
delta+=tmp; cap[i]-=tmp; cap[i^]+=tmp;
}
}
return delta;
}
int s[maxn],e[maxn];
int main()
{
int N,K,mn,mx,i;
scanf("%d%d%d%d",&N,&K,&mx,&mn);
for(i=;i<=N;i++) scanf("%d",&s[i]),ans+=s[i];
for(i=;i<=N;i++) scanf("%d",&e[i]),e[i]-=s[i];
mx=K-mx; S=; T=N+; int SS=N+;
add(S,SS,mx,); add(SS,S,,);
for(i=;i<=K;i++){ //先约定前面K个数,先选。
add(SS,i,mx,); add(i,SS,,);
}
for(i=;i<=N;i++) add(i,i+>N?T:i+,mx-mn,),add(i+>N?T:i+,i,,);
for(i=;i<=N;i++) add(i,i+K>N?T:i+K,,-e[i]),tag[i]=cnt,add(i+K>N?T:i+K,i,,e[i]);
while(spfa()){
vis[T]=;
while(vis[T]){
for(i=;i<=T;i++) vis[i]=;
int tmp=dfs(S,mx);
ans-=(ll)tmp*dis[S];
}
}
printf("%I64d\n",ans);
for(i=;i<=N;i++)
if(cap[tag[i]]) putchar('S');
else putchar('E');
return ;
}
Codeforces Gym 101190 NEERC 16 .D Delight for a Cat (上下界的费用流)的更多相关文章
- Codeforces Gym 101190 NEERC 16 .L List of Primes(递归)
ls特别喜欢素数,他总是喜欢把素数集合的所有子集写下来,并按照一定的顺序和格式.对于每一个子集,集合内 的元素在写下来时是按照升序排序的,对于若干个集合,则以集合元素之和作为第一关键字,集合的字典序作 ...
- Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)
Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
随机推荐
- SQL Server常用系统表
1.查询当前数据库中的用户表 select *from sysobjects where xtype='U'; 2.获取SQL Server允许同时用户连接的最大数 SELECT @@MAX_CONN ...
- Leetcode Array 16 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- WebApi基础
1:当Controller中有相同参数的方法时,请求调用会报错 [HttpGet] public IEnumerable<string> Resturn() { return new st ...
- S2S4H整合注意问题
整合过程中出现问题记录: 1.The import javax.servlet.http.HttpServletRequest cannot be resolved 解决办法:在tomcat的lib目 ...
- 微信URL有效性验证
1.填写配置项:填写 URL 和 Token 点击提交按钮 微信服务器会以get方式请求到所指定的URL,在此URL中进行URL的有效性验证 2.URL有效性的验证: if ("get&q ...
- 引用变量的类型强转以及InstanceOf方法的使用
引用到的类: class Person{ String name; } class Student extends Person{ String sut_no; } class ClassMate e ...
- dom 显示 与否 的对 ecmascript 变量的 监听
dom 显示 与否 的对 ecmascript 变量的 监听
- mysql 查看或者修改数据库密码
首先启动命令行 1.在命令行运行:taskkill /f /im mysqld-nt.exe 下面的操作是操作mysql中bin目录下的一些程序,如果没有配置环境变量的话,需要切换到mysql的bin ...
- Java for LeetCode 081 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- LeetCode:划分字母区间【763】
LeetCode:划分字母区间[763] 题目描述 字符串 S 由小写字母组成.我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段.返回一个表示每个字符串片段的长度的列表. 示 ...