3638: Cf172 k-Maximum Subsequence Sum

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 174  Solved: 92
[Submit][Status][Discuss]

Description

给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少。

Input

The first line contains integer n (1 ≤ n ≤ 105), showing how many numbers the sequence has. The next line contains n integers a1, a2, ..., an (|ai| ≤ 500).

The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The next m lines contain the queries in the format, given in the statement.

All changing queries fit into limits: 1 ≤ i ≤ n, |val| ≤ 500.

All queries to count the maximum sum of at most k non-intersecting subsegments fit into limits: 1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 20. It is guaranteed that the number of the queries to count the maximum sum of at mostk non-intersecting subsegments doesn't exceed 10000.

Output

For each query to count the maximum sum of at most k non-intersecting subsegments print the reply — the maximum sum. Print the answers to the queries in the order, in which the queries follow in the input.

Sample Input

9
9 -8 9 -1 -1 -1 9 -8 9
3
1 1 9 1
1 1 9 2
1 4 6 3

Sample Output

17
25
0

HINT

In the first query of the first example you can select a single pair (1, 9). So the described sum will be 17.

Look at the second query of the first example. How to choose two subsegments? (1, 3) and (7, 9)? Definitely not, the sum we could get from (1, 3) and (7, 9) is 20, against the optimal configuration (1, 7) and (9, 9) with 25.

The answer to the third query is 0, we prefer select nothing if all of the numbers in the given interval are negative.

Source

By xiaodao

3272: Zgg吃东西

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 93  Solved: 59
[Submit][Status][Discuss]

Description

       Zgg是一个吃货。他面前有一堆的食品。不妨把它看成一个序列,序列从1~N编号,第i个位置上有美味度为A[i]的食品。Zgg有很多只手(为什么他有这么多手?我也不知道)。他的手的作用,就是抓起一段连续序列中的食物,并且不限长度。总共有M个时刻,在每个时刻,可能会有一些奇怪的人将他面前的某一个位置的食物美味度改变。或者zgg突然心血来潮,想要用他的其中k只手来抓取某段区间中的食物。这时候,他就需要你告诉他,在用不超过k只手的情况下,他能获得的最大美味度是多少。

Input

       第一行一个整数N,表示食品的个数,即序列长度。
       第2行有N个数依次表示A[i]。
第三行一个整数M,表示有M个时刻。
接下来的M行,每行有形如0 i val
来表示有人将第i位置的食物的美味度改成了val
或者1 l r k,来表示zgg想知道,他只用k只手,在[l,r]这段区间内抓取,能获得的最大美味度。

Output

       对于每次形如1 l r k的询问你都要输出一个数,表示最大美味度。

Sample Input

9
9 -8 9 -1 -1 -1 9 -8 9
5
1 1 9 1
1 1 9 2
1 4 6 3
0 3 -8
1 1 9 1

Sample Output

17
25
0
10

HINT

对于100%的数据 N,M<=100000 ,1<=k<=20.l<=l<=r<=n.数值的绝对值不会超过500.
       Zgg是可以有手空闲的…

Source

3267: KC采花

Time Limit: 30 Sec  Memory Limit: 256 MB
Submit: 65  Solved: 51
[Submit][Status][Discuss]

Description

KC在公园里种了一排花,一共有n朵,游手好闲的KC常常在公园里采花。他对每朵花都有一个美丽度鉴赏。由于对花的喜好不同,有的花分数很高,有的花分数很低,甚至会是负数。
KC很忙,每次采花的时候,不可能从第一朵花,走到第n朵。所以他会先选定一个区间[l,r](1<=l<=r<=n),作为当天的采花范围。同时为了方便采花,他总是从[l,r]中最多选出k个互不相交的子区间,将这些子区间的花全部采光。当然,他希望美丽度总和最大。
KC对花的鉴赏随着他对世界观人生观的改变而改变,他会不时地对每朵花的美丽度进行修改,可能改低,也可能提高。
KC的行为持续m天,每天的行为要么是采花,要么是改变花的美丽度。
注:(1)[l,r]的最多k个互不相同子区间可以表示成:[x1,y1],[x2,y2],...,[xt,yt],满足l<=x1<=y1<x2<=y2<...<xt<=yt<=r,且0<=t<=k。
(2)由于是KC种的花,一朵花采掉第二天会立刻生出来。

Input

第一行一个正整数n,n<=100000。
第二行n个整数a1,a2...an,表示n朵花的美丽度。|ai|<=500。
第三行一个正整数m,m<=100000。
第四行开始,接下来m行,每行表示该天KC的行为。
修改美丽度的行为用0 i val描述,表示将ai修改为val,|val|<=500。
采花行为用1 l r k描述,k<=20意义如题面。

Output

对于每个采花行为,每行一个整数表示最大的美丽度总和。

Sample Input

9
9 -8 9 -1 -1 -1 9 -8 9
3
1 1 9 1
1 1 9 2
1 4 6 3

Sample Output

17
25
0

HINT

100%的数据,满足n,m<=50000,k<=20,ai以及修改的val的绝对值不超过500。

Source

最大费用最大流

Solution

这道题,首先想到最大费用最大流,这类的对序列上的问题,是比较经典的问题,详见 《网络流与线性规划24题》最长k可重区间集问题

那么这道题,最标准的做法就是:

把每个数拆成两个点$I_{1}$和$I_{2}$  $I_{1}-->I_{2}$,$cap=1$,$cost=a[i]$

然后$S-->I_{1}$,$cap=INF$,$cost=0$;以及$I_{2}-->T$,$cap=INF$,$cost=0$;

并且相邻的两位连$I'_{2}-->I''_{1}$,$cap=1$,$cost=0$

那么只需要跑$K$次最大费用最大流,累加答案即可

但是很显然这种数据范围,这么做会TLE,那么不妨考虑对这种做法进行优化:

每次增广的过程,实质上就是取一段和最大的子序列,并将其反转

很显然对于这类序列上的操作,可以用线段树去实现,那么这道题的正确做法就明确了

费用流的构图,用线段树去手动模拟增广路程

线段树维护的方法,只需要维护一段区间的最大和子序列,涉及取反,所以还需要维护最小和子序列

但是发现,每进行一次取反,当前最大和子序列一定变成最小和子序列,最小和子序列一定变成最大,那么直接swap一下就好

鉴于一次询问需要增广K次,也就是一次询问要取反,所以显然需要开一个栈记录一下当前询问所反转的所有区间,在结束时还原

修改操作直接修改即可

总的时间复杂度是$O(knlogn)$

Code

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cstdio>
  6. using namespace std;
  7. inline int read()
  8. {
  9. int x=,f=; char ch=getchar();
  10. while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
  11. while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
  12. return x*f;
  13. }
  14. #define maxn 101000
  15. int n,m,a[maxn],ans;
  16. struct DataNode
  17. {
  18. int lx,rx,mx,sum,pl,pr,p1,p2;
  19. void add(int pos,int x) {p1=p2=pl=pr=pos,mx=sum=lx=rx=x;}
  20. };
  21. struct SegmentTreeNode
  22. {
  23. int l,r,tag;
  24. DataNode maxx,minn;
  25. void add(int x) {maxx.add(l,x); minn.add(l,-x);}
  26. }tree[maxn<<];
  27. inline DataNode Merge(DataNode ls,DataNode rs)
  28. {
  29. DataNode rt;
  30. rt.sum=ls.sum+rs.sum;
  31. rt.lx=ls.sum+rs.lx>ls.lx? ls.sum+rs.lx : ls.lx;
  32. rt.pl=ls.sum+rs.lx>ls.lx? rs.pl : ls.pl;
  33. rt.rx=rs.sum+ls.rx>rs.rx? rs.sum+ls.rx : rs.rx;
  34. rt.pr=rs.sum+ls.rx>rs.rx? ls.pr : rs.pr;
  35. rt.mx=ls.rx+rs.lx; rt.p1=ls.pr; rt.p2=rs.pl;
  36. if (rt.mx<ls.mx) rt.mx=ls.mx,rt.p1=ls.p1,rt.p2=ls.p2;
  37. if (rt.mx<rs.mx) rt.mx=rs.mx,rt.p1=rs.p1,rt.p2=rs.p2;
  38. return rt;
  39. }
  40. inline void Update(int now)
  41. {
  42. tree[now].minn=Merge(tree[now<<].minn,tree[now<<|].minn);
  43. tree[now].maxx=Merge(tree[now<<].maxx,tree[now<<|].maxx);
  44. }
  45. inline void Pushdown(int now)
  46. {
  47. if (!tree[now].tag || tree[now].l==tree[now].r) return;
  48. tree[now].tag^=;
  49. swap(tree[now<<].maxx,tree[now<<].minn);
  50. swap(tree[now<<|].minn,tree[now<<|].maxx);
  51. tree[now<<].tag^=; tree[now<<|].tag^=;
  52. }
  53. inline void BuildTree(int now,int l,int r)
  54. {
  55. tree[now].l=l; tree[now].r=r; tree[now].tag=;
  56. if (l==r) {tree[now].add(a[l]); return;}
  57. int mid=(l+r)>>;
  58. BuildTree(now<<,l,mid); BuildTree(now<<|,mid+,r);
  59. Update(now);
  60. }
  61. inline void Change(int now,int pos,int x)
  62. {
  63. Pushdown(now);
  64. if (tree[now].l==tree[now].r) {tree[now].add(x); return;}
  65. int mid=(tree[now].l+tree[now].r)>>;
  66. if (pos<=mid) Change(now<<,pos,x); else Change(now<<|,pos,x);
  67. Update(now);
  68. }
  69. inline DataNode Query(int now,int L,int R)
  70. {
  71. Pushdown(now);
  72. if (L==tree[now].l && R==tree[now].r) return tree[now].maxx;
  73. int mid=(tree[now].l+tree[now].r)>>;
  74. if (R<=mid) return Query(now<<,L,R);
  75. if (L>mid) return Query(now<<|,L,R);
  76. return Merge(Query(now<<,L,mid),Query(now<<|,mid+,R));
  77. }
  78. inline void Reverse(int now,int L,int R)
  79. {
  80. Pushdown(now);
  81. if (L<=tree[now].l && R>=tree[now].r)
  82. {swap(tree[now].maxx,tree[now].minn); tree[now].tag^=; return;}
  83. int mid=(tree[now].l+tree[now].r)>>;
  84. if (L<=mid) Reverse(now<<,L,R);
  85. if (R>mid) Reverse(now<<|,L,R);
  86. Update(now);
  87. }
  88. struct StackNode
  89. {
  90. int l,r;
  91. void Push(int L,int R) {l=L,r=R;}
  92. }stack[];
  93. int top;
  94. inline void SolveAsk(int L,int R,int K)
  95. {
  96. ans=; top=;
  97. while (K--)
  98. {
  99. DataNode tmp=Query(,L,R);
  100. if (tmp.mx>) ans+=tmp.mx; else break;
  101. Reverse(,tmp.p1,tmp.p2);
  102. stack[++top].Push(tmp.p1,tmp.p2);
  103. }
  104. for (int i=top; i>=; i--)
  105. Reverse(,stack[i].l,stack[i].r);
  106. printf("%d\n",ans);
  107. }
  108. inline void SolveChange(int pos,int x) {Change(,pos,x);}
  109. void Freopen() {freopen("data.in","r",stdin);freopen("data.out","w",stdout);}
  110. void Fclose() {fclose(stdin);fclose(stdout);}
  111. int main()
  112. {
  113. // Freopen();
  114. n=read();
  115. for (int i=; i<=n; i++) a[i]=read();
  116. BuildTree(,,n);
  117. m=read();
  118. while (m--)
  119. {
  120. int opt=read(),L,R,K,x,d;
  121. switch (opt)
  122. {
  123. case : L=read(),R=read(),K=read(); SolveAsk(L,R,K); break;
  124. case : x=read(),d=read(); SolveChange(x,d); break;
  125. }
  126. }
  127. // Fclose();
  128. return ;
  129. }

3502: PA2012 Tanie linie

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 181  Solved: 36
[Submit][Status][Discuss]

Description

n个数字,求不相交的总和最大的最多k个连续子序列。
 1<= k<= N<= 1000000。

Input

Output

Sample Input

5 2
7 -3 4 -9 5

Sample Output

13

HINT

Source

Solution

和刚刚一样,但是范围扩大了,需要开longlong,所以内存很卡,时间也很卡..

Code

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cstdio>
  6. using namespace std;
  7. inline int read()
  8. {
  9. int x=,f=; char ch=getchar();
  10. while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
  11. while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
  12. return x*f;
  13. }
  14. #define maxn 1000010
  15. int n,m,a[maxn];long long ans;
  16. struct DataNode
  17. {
  18. long long lx,rx,mx,sum;int pl,pr,p1,p2;
  19. void add(int pos,int x) {p1=p2=pl=pr=pos,mx=sum=lx=rx=x;}
  20. };
  21. struct SegmentTreeNode
  22. {
  23. int l,r,tag;
  24. DataNode maxx,minn;
  25. void add(long long x) {maxx.add(l,x); minn.add(l,-x);}
  26. }tree[maxn*+];
  27. inline DataNode Merge(DataNode ls,DataNode rs)
  28. {
  29. DataNode rt;
  30. rt.sum=ls.sum+rs.sum;
  31. rt.lx=ls.sum+rs.lx>ls.lx? ls.sum+rs.lx : ls.lx;
  32. rt.pl=ls.sum+rs.lx>ls.lx? rs.pl : ls.pl;
  33. rt.rx=rs.sum+ls.rx>rs.rx? rs.sum+ls.rx : rs.rx;
  34. rt.pr=rs.sum+ls.rx>rs.rx? ls.pr : rs.pr;
  35. rt.mx=ls.rx+rs.lx; rt.p1=ls.pr; rt.p2=rs.pl;
  36. if (rt.mx<ls.mx) rt.mx=ls.mx,rt.p1=ls.p1,rt.p2=ls.p2;
  37. if (rt.mx<rs.mx) rt.mx=rs.mx,rt.p1=rs.p1,rt.p2=rs.p2;
  38. return rt;
  39. }
  40. inline void Update(int now)
  41. {
  42. tree[now].minn=Merge(tree[now<<].minn,tree[now<<|].minn);
  43. tree[now].maxx=Merge(tree[now<<].maxx,tree[now<<|].maxx);
  44. }
  45. inline void Pushdown(int now)
  46. {
  47. if (!tree[now].tag || tree[now].l==tree[now].r) return;
  48. tree[now].tag^=;
  49. swap(tree[now<<].maxx,tree[now<<].minn);
  50. swap(tree[now<<|].minn,tree[now<<|].maxx);
  51. tree[now<<].tag^=; tree[now<<|].tag^=;
  52. }
  53. inline void BuildTree(int now,int l,int r)
  54. {
  55. tree[now].l=l; tree[now].r=r; tree[now].tag=;
  56. if (l==r) {tree[now].add(a[l]); return;}
  57. int mid=(l+r)>>;
  58. BuildTree(now<<,l,mid); BuildTree(now<<|,mid+,r);
  59. Update(now);
  60. }
  61. inline DataNode Query(int now,int L,int R)
  62. {
  63. Pushdown(now);
  64. if (L==tree[now].l && R==tree[now].r) return tree[now].maxx;
  65. int mid=(tree[now].l+tree[now].r)>>;
  66. if (R<=mid) return Query(now<<,L,R);
  67. if (L>mid) return Query(now<<|,L,R);
  68. return Merge(Query(now<<,L,mid),Query(now<<|,mid+,R));
  69. }
  70. inline void Reverse(int now,int L,int R)
  71. {
  72. Pushdown(now);
  73. if (L<=tree[now].l && R>=tree[now].r)
  74. {swap(tree[now].maxx,tree[now].minn); tree[now].tag^=; return;}
  75. int mid=(tree[now].l+tree[now].r)>>;
  76. if (L<=mid) Reverse(now<<,L,R);
  77. if (R>mid) Reverse(now<<|,L,R);
  78. Update(now);
  79. }
  80. inline void SolveAsk(int L,int R,int K)
  81. {
  82. while (K--)
  83. {
  84. DataNode tmp=Query(,L,R);
  85. if (tmp.mx>) ans+=tmp.mx; else break;
  86. Reverse(,tmp.p1,tmp.p2);
  87. }
  88. printf("%lld\n",ans);
  89. }
  90. int main()
  91. {
  92. n=read(); int K=read();
  93. for (int i=; i<=n; i++) a[i]=read();
  94. BuildTree(,,n);
  95. SolveAsk(,n,K);
  96. return ;
  97. }

【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广的更多相关文章

  1. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  2. BZOJ 5326 [JSOI2017]博弈 (模拟费用流、线段树)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5326 题解 终于成为第8个A掉这题的人--orz tzw神仙早我6小时 本以为这东西常数 ...

  3. Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]

    洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...

  4. BZOJ 1920 Luogu P4217 [CTSC2010]产品销售 (模拟费用流、线段树)

    题目链接 (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1920 (luogu) https://www.luogu.org/prob ...

  5. Maximum Subsequence Sum【最大连续子序列+树状数组解决】

    Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i < ...

  6. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  7. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  9. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

随机推荐

  1. 夯实基础之php学习-1基础篇

    1,单引号和双引号的区别 单引号表示字符串,双引号能解析字符串中的变量,所以,如果没有变量,尽量用单引号,加快解析速度 当字符串需要单引号或者双引号的时候,可以用转义字符代替 2,类型转换 通过(bo ...

  2. java多线程系类:基础篇:06线程让步

    本系类的知识点全部来源于http://www.cnblogs.com/skywang12345/p/3479243.html,我只是复制粘贴一下,特在此说明. 概要 本章,会对Thread中的线程让步 ...

  3. 字符串匹配(hash算法)

    hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...

  4. 高性能JavaScript 重排与重绘

    先回顾下前文高性能JavaScript DOM编程,主要提了两点优化,一是尽量减少DOM的访问,而把运算放在ECMAScript这一端,二是尽量缓存局部变量,比如length等等,最后介绍了两个新的A ...

  5. List<T>与Dictionary<string,T>频繁检索的性能差距

    一直对LINQ简洁高效的语法青睐有加,对于经常和资料库,SQL语法打交道的C#开发者来说,LINQ无疑是一个非常不错的选择,当要在List<T>(T为一个普通对象)集合中查找满足某些条件的 ...

  6. Python 3 与 MySQL 5.6

    主要简单说下Python 3.3搭配MySQL Community Server 5.6的使用.在Python 3系列和MySQL 5.0系列里面下面的代码应该都通用.(没有验证) 准备 python ...

  7. nodepad++快捷键收集

    Notepad++ 快捷键 大全Ctrl+C 复制Ctrl+X 剪切Ctrl+V 粘贴Ctrl+Z 撤消Ctrl+Y 恢复Ctrl+A 全选Ctrl+F 键查找对话框启动Ctrl+H 查找/替换对话框 ...

  8. Win7宽带一键创建

    简化创建宽带连接步骤,为简便而生. 不断分享,不断进步. 免费下载:                  http://yunpan.cn/cmZesi2jpJk9E  访问密码 9444

  9. ipython又一方便的调试和应用工具!!!

    控制台下://ipython 命令丰富 比如:ls 显示目录  ipython --pylab %run -p *.py quit关闭     示例: In []: %run -p test.py H ...

  10. MAC OS上Nginx安装

    admin@admindeMac:local]$ brew install nginx ==> Installing dependencies for nginx: pcre, openssl ...