挺不错的一道题,基本思路是dp。关键点是如何求区间内的Sigma|A_i-B_i|。
线段树做TLE了,优先队列可以过。

 /* 4261 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int INF = 1e9;
const int maxn = ;
const int maxm = ;
int n, m;
int a[maxn];
int dp[maxm][maxn];
int dif[maxn][maxn]; void solve() {
int tmp; rep(i, , n+) {
priority_queue<int> q;
priority_queue<int, vi, greater<int> > Q; int ls, rs;
int ln, rn;
int x, xx;
int l = ; ln = rn = ;
ls = rs = ;
rep(j, i, n+) {
++l;
x = a[j];
if (l & ) {
++ln;
if (rn && Q.top()<x) {
xx = Q.top();
Q.pop();
q.push(xx);
ls += xx;
Q.push(x);
rs += x - xx;
} else {
q.push(x);
ls += x;
} x = q.top();
tmp = rs - ls + x;
dif[i][j] = tmp;
} else {
++rn;
if (q.top() > x) {
xx = q.top();
q.pop();
Q.push(xx);
rs += xx;
q.push(x);
ls += x - xx;
} else {
Q.push(x);
rs += x;
} x = q.top();
tmp = rs - ls;
dif[i][j] = tmp;
}
}
} rep(j, , n+) {
dp[][j] = dif[][j];
} rep(i, , m+) {
rep(j, i, n+) {
dp[i][j] = INF;
rep(k, i-, j) {
dp[i][j] = min(dp[i][j], dp[i-][k]+dif[k+][j]);
}
}
} printf("%d\n", dp[m][n]);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
rep(i, , n+) {
scanf("%d", &a[i]);
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据生成器。

 from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t =
bound = **
for tt in xrange(t):
n = randint(, )
k = randint(, )
fout.write("%d %d\n" % (n, k))
dataList = []
for i in xrange(n):
x = randint(-bound, bound)
dataList.append(x)
fout.write(" ".join(map(str, dataList)) + "\n")
fout.write("0 0\n") def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()

【HDOJ】4261 Estimation的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. WeX5是主要进行app开发吗?能开发微信App吗?

    WeX5是一款html5开发工具,可以进行app开发,做出各种H5 App,同样也可以进行主要运行在PC的html5产品,. WeX5开发的应用,不仅可以在微信上运行,也可以直接手机浏览器运行,或者打 ...

  2. encodeURIComponent与encodeURI的区别

    encodeURIComponent()   -->把字符串编码为 URI 组件. encodeURI()                   -->把字符串编码为 URI. var te ...

  3. mysql日期时间操作

    select curdate(); --获取当前日期 select last_day(curdate()); --获取当月最后一天. select DATE_ADD(curdate(),interva ...

  4. php提取字符串中的数字

    最近工作中写代码的时候需要在一串字符串中将所有的数字提取出来这么一个小功能,研究了一下发现方法还挺多,值得记录一下,于是对如何使用PHP将字符串中的数字提取出来的功能做了一个小总结,总结三种方法如下: ...

  5. linux设备驱动模型(kobject与kset)

    Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...

  6. Relay log read failure

    root@localhost > show slave status\G*************************** 1. row ************************** ...

  7. 十七、mysql 分区之 锁问题

    1.演示一个表锁,基于myisam CMD 1 CMD2 create table e1 (id int ,name char(20)); lock table e1 read; [select|in ...

  8. VC++入门精通视频教程

    1.1.Windows程序运行原理-1 上传日期:2012-03-19 09:18:50  相关摘要:  - 在关闭一个windows窗口时,也关闭另一个吗 - 对纯面向对象的PHP程序有何看法? - ...

  9. 经典好文:android和iOS平台的崩溃捕获和收集

    通过崩溃捕获和收集,可以收集到已发布应用(游戏)的异常,以便开发人员发现和修改bug,对于提高软件质量有着极大的帮助.本文介绍了iOS和android平台下崩溃捕获和收集的原理及步骤,不过如果是个人开 ...

  10. js 获取 input file 文件 附给 image src

    var a=document.querySelector('input[type=file]'); a.onchange = function (e) { //var reader = new Fil ...