hdu1024 Max Sum Plus Plus的另一种解法
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1053
【题解】
原来的解法:http://www.cnblogs.com/galaxies/p/hdu1024.html
原来的解法思路是正着,我们加数,现在考虑删数
我们先特判几种情况(可以全选正,全选正还不够)
现在只剩下一种情况,段数<正数段数
我们可以把数字合并变成+/-/+/-...这个情况
那我们考虑的是合并。
每次我们选出+的段,意为舍弃这一段
选出-的段,意为把这一段和旁边两个正段合并
所以选出代表要把这段绝对值从答案里减去,我们希望越小越好,所以维护一个小根堆(或set)
那么合并的时候把选出的段和旁边两段直接相加即可,由于这个绝对值是最小的,所以加起来还是旁边两段的符号。
特别注意如果在边界上出现了负数那么这个负数是没有任何用的,因为我们选他不能合并来使段数减小,所以直接删掉即可。
在进队列的时候如果边界上出现了0,那么一样把它删掉,因为选0一定是出现这种情况
10000 -10000 -10000 ....
然后你选了第一个,变成了0 -10000 ...
很明显我们第一次选了10000,意为舍弃它,并合并,所以和周围合并变成了0,再选0,没有任何意义,不能使段数减小,反而增加。
至于为什么不能在取top的时候判,因为会有这种情况
m=1,n=4,a[]={0,-1,-1,0}
原本是0,就已经合法,是有实际意义的,所以不能判掉。
然后就过了
# include <set>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 1e6 + ;
const int mod = 1e9+; # define RG register
# define ST static ll a[M], ans = ;
int m, n;
int p[M], pn;
int tot1, tot2;
set< pair<ll, int> > s; inline int sgn(int x) {return x<?:;}
inline ll myabs(ll x) {return x>?x:-x;} inline void transform() {
n = ;
ll t = p[]; int s = sgn(t);
for (int i=; i<=pn; ++i) {
if(sgn(p[i]) == s)
t += p[i];
else {
a[++n] = t;
t = p[i];
s = sgn(t);
}
}
a[++n] = t;
// for (int i=1; i<=n; ++i) printf("%d ", a[i]);
} int Minus[M];
inline void solve_minus() {
int tn = , tem = m-tot1;
for (int i=; i<=pn; ++i) if(p[i] < ) Minus[++tn] = p[i];
sort(Minus+, Minus+tn+);
while(tem--) ans += Minus[tn--];
} int L[M], R[M]; inline void del(int x) {
L[R[x]] = L[x];
R[L[x]] = R[x];
} inline void solve() {
s.clear();
int T = tot2 - m;
for (int i=; i<=n+; ++i) L[i] = R[i] = ;
for (int i=; i<n; ++i) L[i] = i-, R[i] = i+;
L[n] = n-;
for (int i=; i<=n; ++i) s.insert(make_pair(myabs(a[i]), i));
// cout << T << endl;
while(T--) {
pair<ll, int> top = *s.begin();
s.erase(top);
int x = top.second, y;
if(a[x] < && (!L[x] || !R[x])) {
del(x);
++T;
continue;
}
ll sum = a[x];
ans -= myabs(sum);
if(L[x]) {
y = L[x];
sum += a[y];
s.erase(make_pair(myabs(a[y]), y));
del(L[x]);
}
if(R[x]) {
y = R[x];
sum += a[y];
s.erase(make_pair(myabs(a[y]), y));
del(R[x]);
}
a[x] = sum;
if(sum == ) del(x);
else s.insert(make_pair(myabs(a[x]), x));
}
} int main() {
while(cin >> m >> pn) {
tot1 = tot2 = ;
for (int i=; i<=pn; ++i) scanf("%d", p+i);
transform();
for (int i=; i<=pn; ++i) tot1 += (p[i] >= );
for (int i=; i<=n; ++i) tot2 += (a[i] >= );
ans = ;
for (int i=; i<=n; ++i)
if(a[i] >= ) ans += a[i];
// for (int i=1; i<=n; ++i) cout << a[i] << ' ';
// cout << endl;
// cout << "ans = " << ans << endl;
if(tot2 <= m && m <= tot1) cout << ans << endl;
else if(m > tot1) {
solve_minus();
cout << ans << endl;
} else {
solve();
cout << ans << endl;
}
}
return ;
}
hdu1024 Max Sum Plus Plus的另一种解法的更多相关文章
- HDU1024 Max Sum Plus Plus 【DP】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu1024 Max Sum Plus Plus 滚动dp
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1024 Max Sum Plus Plus —— DP + 滚动数组
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...
- HDU1024 Max Sum Plus Plus (优化线性dp)
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化
题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...
- hdu1024 Max Sum Plus Plus
动态规划,给定长度为n(≤1e6)的整数数组和整数m,选取m个连续且两两无交集的子区间,求所有方案中使得区间和最大的最大值. dp[i][j]表示结束位置(最后一个区间最后一个元素的位置)为i且选取区 ...
- HDU1024 Max Sum Plus Plus(DP)
状态:d(i,j)它代表前j划分数i部并且包括第一j最佳结果时的数.g(i,j)表示前j划分数i最好的结果时,段,g(m,n)结果,需要. 本题数据较大.需採用滚动数组.注意:这题int类型就够用了, ...
- HDU1024 Max Sum Plus Plus(dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 #include<iostream> #include<vector> #i ...
随机推荐
- 特殊sql查询方法实例
一.if条件查询:SELECT sum(if(is_buy > 0 ,1,0)) AS friend_count_all_cj, sum(if(is_buy = 0 ,1,0)) AS frie ...
- iOS-修改modal出来的控制器的大小
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ static BOOL sh ...
- POJ 2217 LCS(后缀数组)
Secretary Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1655 Accepted: 671 Descript ...
- B1091 N-自守数 (15分)
B1091 N-自守数 (15分) 如果某个数 \(K\)的平方乘以\(N\) 以后,结果的末尾几位数等于 \(K\),那么就称这个数为"\(N\)-自守数".例如 \(3×92 ...
- [Link-Cut-Tree][BZOJ2631]Tree
题面 Description: 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的 ...
- spark streaming的应用
今天我们讲spark streaming的应用,这个是实时处理的,类似于Storm以及Flink相关的知识点, 说来也巧,今天的自己也去听了关于Flink的相关的讲座,可惜自己没有听得特别清楚,好像是 ...
- Windows GitLab使用全过程
1.首先安装Git 1.1.下载网站: https://git-for-windows.github.io/ 1.2.安装Git参考网站 http://blog.csdn.net/u012614287 ...
- Hibernate---数据操作示例BY实体映射文件
创建一个Student.java类:该类需要一个无参的构造函数,以及属性的get/set方法 public class Student implements Serializable { privat ...
- unbantu安装wmvare
最新评论 wsmyyjie:写的太好了!!! zhangmin92:回复 wopapa523: 这个是你用另一.. wopapa523:请问i11是在哪里输入的? myh65013:挺深入的 andk ...
- Android通过用代码画虚线椭圆边框背景来学习一下shape的用法
在Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的形状,shape可以绘制矩形环形以及椭圆,所以只需要用椭圆即可,在使用的时候将控件比如imageview或textview ...