[hdoj5192] 树状数组
枚举所有的区间。对于确定的区间,假设最终的高度为h,
代价是max(∑(Hi−h),∑(h−Hj))(Hi>h,Hj≤h)
等价于max(∑Hi−cnt(i)∗h,cnt(j)∗h−∑Hj)
(cnt(i)表示满足Hi>h的堆数, cnt(j)表示满足Hj≤h 的堆数)。∑Hi−cnt(i)∗h关于h呈递减,cnt(j)∗h−∑Hj关于h呈递增。一个递减到0,一个从0开始递增,所以代价与h的函数图像是V字形的,交点处代价最小。此时 ∑Hi−cnt(i)∗h=cnt(j)∗h−∑Hj,h=∑Hi+∑Hjcnt(i)+cnt(j),分母是总堆数W,分子是这个区间积木的总个数。h实际上就是这个区间的平均高度aver。考虑到四舍五入,答案是aver或者aver+1,当然还需要与题目给定的H做下比较,最终的方案是这3个数之一。确定高度之后,把高的变矮需要知道比当前高度大的个数以及高度总和,把矮的变高类似。因此添加一堆和删除一堆时,需要维护个数和总和。可以通过树状数组维护,整个问题的复杂度O((n+W)logn).
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define Rep(a, b) for(int a = 0; a < b; a++)
#define lowbit(x) ((x) & (-(x)))
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} typedef double db;
typedef long long LL; const int dx[] = {, , -, };
const int dy[] = {, -, , };
const int maxn = 1e4 + ;
const int maxm = 1e5 + ;
const int MD = 1e9 +; struct Point {
int x, y;
bool operator < (const Point &opt) const {
return x < opt.x || x == opt.x && y < opt.y;
}
Point operator - (const Point &opt) const {
return Point(x - opt.x, y - opt.y);
}
constructInt2(Point, x, y);
void inp() {
scanf("%d %d", &x, &y);
}
void outp() {
printf("(%d, %d), ", x, y);
}
}; struct Trie {
const static int char_size = ;
int cc;
int cht[][char_size];
int mark[];
Trie() { cc = ; mem0(mark); mem0(cht); }
int Idex(char ch) { return ch - ''; }
void Insert(char s[], int v) {
int pos = ;
for(int i = ; s[i]; i++) {
int id = Idex(s[i]);
if(!cht[pos][id]) cht[pos][id] = ++cc;
pos = cht[pos][id];
}
mark[pos] = v;
}
bool Find(char s[]) {
int pos = ;
for(int i = ; s[i]; i++) {
int id = Idex(s[i]);
if(!cht[pos][id]) return ;
pos = cht[pos][id];
}
return mark[pos];
}
}; struct KMP {
int next[];
void GetNext(char s[]) {
mem0(next);
next[] = next[] = ;
for(int i = ; s[i]; i++) {
int j = next[i];
while(j && s[i] != s[j]) j = next[j];
next[i + ] = s[j] == s[i]? j + : ;
}
}
void Match(char s[], char t[]) {
int j = , len = strlen(t);
for(int i = ; s[i]; i++) {
while(j && s[i] != t[j]) j = next[j];
if(s[i] == t[j]) j++;
if(j == len) printf("%d\n", i - len + );
}
}
}; struct Matrix {
int a[][];
Matrix operator * (const Matrix &_A) const {
Matrix tmp;
mem0(tmp.a);
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++) {
tmp.a[i][j] = ((LL)a[i][k] * _A.a[k][j] + tmp.a[i][j]) % MD;
}
}
}
return tmp;
}
}; struct Edge {
int u, v;
constructInt2(Edge, u, v);
}; struct Segment {
Point a, b;
void inp() {
scanf("%d%d%d%d", &a.x, &a.y, &b.x, &b.y);
if(a.x > b.x) {
swap(a.x, b.x);
swap(a.y, b.y);
}
}
}; Matrix CalcMatrix(Matrix a, int n) {
if(n == ) return a;
Matrix tmp = CalcMatrix(a, n >> );
tmp = tmp * tmp;
if(n & ) tmp = tmp * a;
return tmp;
} inline int ReadInt() {
char c = getchar();
while(!isdigit(c)) c = getchar(); int x = ;
while(isdigit(c)) {
x = x * + c - '';
c = getchar();
}
return x;
} inline void WriteInt(int i) {
int p = ;
static int buf[];
if(i == ) p++;
else while(i) {
buf[p++] = i % ;
i /= ;
}
for(int j = p - ; j; j--) putchar('' + buf[j]);
} int Cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
} int Dist2(Point a, Point b) {
int x = a.x - b.x, y = a.y - b.y;
return x * x + y * y;
}
int ConvexHull(Point *p, int n, Point *ch) {
sort(p, p + n);
int m = ;
for (int i = ; i < n; i++) {
while (m > && Cross(ch[m - ] - ch[m - ], p[i] - ch[m - ]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for (int i = n - ; i >= ; i--) {
while (m > k && Cross(ch[m - ] - ch[m - ], p[i] - ch[m - ]) <= ) m--;
ch[m++] = p[i];
}
if (n > ) m--;
return m;
} template<class edge> struct Graph {
vector<vector<edge> > adj;
Graph(int n) { adj.clear(); adj.resize(n + ); }
Graph() { adj.clear(); }
void resize(int n) { adj.resize(n + ); }
void add(int s, edge e){ adj[s].push_back(e); }
void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
void clear() { adj.clear(); }
vector<edge>& operator [](int t) { return adj[t]; }
}; template<class T> struct TreeArray {
vector<T> c;
int maxn;
TreeArray(int n) { c.resize(n + ); maxn = n; }
TreeArray() { c.clear(); maxn = ; }
void clear() { memset(&c[], , sizeof(T) * maxn); }
void resize(int n) { c.resize(n + ); maxn = n; }
void add(int p, T x) { while (p < maxn) { c[p] += x; p += lowbit(p); } }
T get(int p) { T res = ; while (p) { res += c[p]; p -= lowbit(p); } return res; }
T range(int a, int b) { return get(b) - get(a - ); }
}; int n, W, H, a[];
LL sum[], step, maxh;
TreeArray<LL> ta(), ta0();
void Check(int h, int r) {
if (sum[n + * W - ] < (LL)h * W) return ;
LL sum1 = ta0.get(h + ), sumall = sum[r] - sum[r - W], c = ta.get(h + );
LL newsum1 = h * c - sum1, newsum2 = sumall - h * W + newsum1;
LL res = max(newsum1, newsum2);
if (res < step || res == step && h > maxh) {
step = res;
maxh = h;
}
}
int main() {
//freopen("in.txt", "r", stdin);
while (cin >> n >> W >> H) {
mem0(a);
for (int i = ; i < n; i++) {
scanf("%d", a + i + W);
}
int total = n + * W;
for (int i = ; i < total; i++) sum[i] = sum[i - ] + a[i]; if (sum[total - ] < (LL)H * W) {
puts("-1");
continue;
} ta.clear();
ta0.clear();
step = H * W;
maxh = H;
ta.add(, W );
ta0.add(, ); for (int i = W; i < total; i++) {
int num = sum[i] - sum[i - W], ave = num / W;
if (ave < H) ave = H;
ta.add(a[i - W] + , -);
ta0.add(a[i - W] + , -a[i - W]);
ta.add(a[i] + , );
ta0.add(a[i] + , a[i]);
Check(ave, i);
Check(ave + , i);
}
cout << maxh << " " << step << endl;
}
return ;
}
[hdoj5192] 树状数组的更多相关文章
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
随机推荐
- PHP函数:json_last_error
json_last_error() - 返回 JSON 编码解码时最后发生的错误.. 说明: json_last_error ( void ) : int 参数: 无 返回值: 返回一个整型(int ...
- abp(net core)+easyui+efcore实现仓储管理系统——入库管理之九(四十五)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- Java环境下 selenium webDriver + chrome浏览器搭建与调试
一.首先下载selenium webDriver jar包,下载地址如下: http://selenium-release.storage.googleapis.com/index.html 二.下载 ...
- 从零开始学习docker之docker的安装
一.Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 OverlayFS 类的 Union FS 等技术,对进程 ...
- python3如何不生成pyc文件
使用-B参数 即 python3 -B test.py 设置环境变量 export PYTHONDONTWRITEBYTECODE=1 在导入的地方增加 import sys sys.dont_wri ...
- shell脚本之awk(一)
运维必备技能 概述: 1.awk是一种编程语言,用于linux/unix下对文本和数据进行扫描.处理数据来源:标准输入.文件.管道. 2.linux中常用的awk编译器版本有mawk,gawk.R ...
- zookeeper笔记(一)
title: zookeeper笔记(一) zookeeper 安装简记 解压文件 $ tar -zxvf zookeeper-3.4.10.tar.gz -C 安装目录 创建软连接(进入安装目录) ...
- Python爬虫之记录一次下载验证码的尝试
好久没有写过爬虫的文章了,今天在尝试着做验证码相关的研究时,遇到了验证码的收集问题. 一般,验证码的加载都有着比较复杂的算法和加密在里边,但是笔者今天碰到的验证码却比较幸运,有迹可循.在此,给 ...
- T-SQL字符串函数
整理下MSSQL中有关时间的函数,博客记录之. ASCII 原型:ASCII ( character_expression ) 返回值:int 类型值 功能:返回输入字符串最左边的一个字符的ASCII ...
- 移动App性能测评与优化1.4.4 多进程应用
1.4.4 多进程应用 根据上一节中的描述,当一个进程结束后,它所占用的共享库内存将会被其他仍然使用该共享库的进程所分担,共享库消耗的物理内存并不会减少.实际上,对于所有共享使用了这个库的应用,Pss ...