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 ...
随机推荐
- 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)
本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...
- PHP提取奇数或偶数下标元素
该功能主要用到 array_filter() 函数,这个函数可以用回调函数过滤数组中的单元.用法: array array_filter ( array $array [, callable $cal ...
- css 菱形写法
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Mysql:case when then end 的用法
0.创建一张数据表 表名为 test_when_case CREATE TABLE `test_when_case` ( `id` int(11) unsigned NOT NULL AUTO_INC ...
- 多通道CNN
在读Convolutional Neural Networks for Sentence Classification 这个文章的时候,它在论文中提出一种模型变种就是 CNN-multichannel ...
- storm实时计算实例(socket实时接入)
介绍 实现了一个简单的从实时日志文件监听,写入socket服务器,再接入Storm计算的一个流程. 源码 日志监听实时写入socket服务器 package socket; import java ...
- 腾讯云分析MTA HTML5接入方法
从微信服务号或订阅号里跳转到自己项目后,想在项目中统计出实时数据.历史趋势.实时访客.新老访客比.访客画像.地域信息.运营商.终端信息.页面排行.性能监控.访问深度.外部链接.入口页面.离开页面.渠道 ...
- imageX.exe
imageX 编辑ImageX 是一个命令行工具,原始设备制造商 (OEM) 和公司可以使用它来捕获.修改和应用基于文件的磁盘映像以进行快速部署.ImageX 可以使用 Windows 映像 (.wi ...
- 这个写法会出什么问题: @property (copy) NSMutableArray *array;
因为copy策略拷贝出来的是一个不可变对象,然而却把它当成可变对象使用,很容易造成程序奔溃 //如:-[__NSArrayI removeObjectAtIndex:]: unrecognized s ...
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目5
2014-03-18 01:40 题目:对字符串进行类似游程编码的压缩,如果压缩完了长度更长,则返回不压缩的结果.比如:aabcccccaaa->a2b1c5a3,abc->abc. 解法 ...