[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]表示不 ...
随机推荐
- flutter和react native如何选择
[关于性能]跨平台开发第一个考虑的就是性能问题RN的效率由于是将View编译成了原生View,所以效率上要比基于Cordova的HTML5高很多,但是它也有效率问题,RN的渲染机制是基于前端框架的考虑 ...
- 在vue中使用ztree树插件
插件资源及api:树官网 本事例是在vue3.0+中演示,事例是实际项目中正在用的组件所以部分打了马赛克. 1.插件准备(提前准备好插件文件) 可以直接在官网下载,搭建好脚手架后将准备好的文件放在li ...
- Springboot:员工管理之登录、拦截器(十(4))
1:构建登录javaBean com\springboot\vo\LoginUser.java package com.springboot.vo; import lombok.Data; @Data ...
- pytorch 中HWC转CHW
import torch import numpy as np from torchvision.transforms import ToTensor t = torch.tensor(np.aran ...
- Java 多线程 -- 理解锁:手动实现可重入锁和不可重入锁
JDK提供的大多数内置锁都是可重入的,也就是 说,如果某个线程试图获取一个已经由它自己持有的锁时,那么这个请求会立 刻成功,并且会将这个锁的计数值加1,而当线程退出同步代码块时,计数器 将会递减,当计 ...
- PHP如何实现判断提交的是什么方式
function get_request_method() { // $_SERVER包含了诸多头信息.路径.以及脚本位置等等信息的数组,这个数组中的项目有web服务器创建. if (isset($_ ...
- shell脚本之awk(一)
运维必备技能 概述: 1.awk是一种编程语言,用于linux/unix下对文本和数据进行扫描.处理数据来源:标准输入.文件.管道. 2.linux中常用的awk编译器版本有mawk,gawk.R ...
- apt 安装 版本
1. 通过apt-get安装指定版本软件 apt-get install package=version 2. 查询指定软件有多少个版本 2.1 通过网站搜索https://packages.ubun ...
- Redis持久化存储(二)
redis多实例介绍 接上一篇redis.创建数据存放的目录 vim redis.conf +187 dir /application/data/ 重新启动 mkdir /application/da ...
- React Native 在 Airbnb 的起起落落
写在前面 Airbnb 早在 2016 年就上了 React Native 大船,是很具代表性的先驱布道者: In 2016, we took a big bet on React Native. T ...