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 ...
随机推荐
- Python函数及参数
## 函数 - 函数是代码的一种组织形式,一般一个函数完成一个特定功能 - 函数需要先定义后使用 - 函数的定义 def func_name(参数): func_body ... return fun ...
- linux 安装nginx yum
本分类下有一个环境一键安装.那这背后发生了什么呢?咱们手动使用源码进行安装.1.首先保证有一个能联网的centos.2.百度 ningx 官网 点download http://nginx.or ...
- nginx+php整合(是让nginx可以运行php,以及下载地址)
下载地址: nginx:http://nginx.org/en/download.html PHP: https://windows.php.net/download/ 都是官网的自己选择版本 安装文 ...
- Elasticsearch和Head插件安装
环境: CentOS7 Elasticsearch-6.3.2 JDK8 准备: JDK8 下载地址:http://www.oracle.com/technetwork/java/javase/do ...
- 理解canvas路径
canvas路径和ps里面的路径差不多,在进行图形绘制时,先绘制出来图形的路径,然后再描边或者填充. canvas路径还有子路径的概念,在某一时刻,canvas之中只能有一条路径存在,Canvas规范 ...
- PHP.36-TP框架商城应用实例-后台12-商品管理-主分类添加、修改、搜索(连表查询)
需求:一个商品必须有一个主分类,一个主分类可以有多个商品 [一对多] 修改表p39_goods,增加外键约束,增加索引 主分类添加[控制器->页面] 1.在控制器GoodsController. ...
- luogu3317 [SDOI2014]重建
原来矩阵树定理对于边是概率的情况也是适用的qwqwq. ref #include <iostream> #include <cstdio> #include <cmath ...
- C# Json 序列化大全--任我行
public class JsonHelper { /// <summary> /// 将Model转换为Json字符串 /// </summary> /// <type ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目1
2014-03-21 20:35 题目:给定已升序排列的数组A和数组B,如果A有足够的额外空间容纳A和B,请讲B数组合入到A中. 解法:由后往前进行归并. 代码: // 11.1 Given two ...
- 【APUE】Chapter11 Threads
看完了APUE第三版的Chapter11 Threads,跟着书上的demo走了一遍,并且参考了这个blog(http://www.cnblogs.com/chuyuhuashi/p/4447817. ...