1. 枚举所有的区间。对于确定的区间,假设最终的高度为h
  2. 代价是max(∑(Hih),∑(hHj))(Hih,Hjh)
  3. 等价于max(∑Hicnt(i)∗h,cnt(j)∗h−∑Hj)
  4. (cnt(i)表示满足Hih的堆数, cnt(j)表示满足Hjh 的堆数)。∑Hicnt(i)∗h关于h呈递减,cnt(j)∗h−∑Hj关于h呈递增。一个递减到0,一个从0开始递增,所以代价与h的函数图像是V字形的,交点处代价最小。此时 Hicnt(i)∗h=cnt(j)∗h−∑Hjh=∑Hi+∑Hjcnt(i)+cnt(j),分母是总堆数W,分子是这个区间积木的总个数。h实际上就是这个区间的平均高度aver。考虑到四舍五入,答案是aver或者aver+1,当然还需要与题目给定的H做下比较,最终的方案是这3个数之一。确定高度之后,把高的变矮需要知道比当前高度大的个数以及高度总和,把矮的变高类似。因此添加一堆和删除一堆时,需要维护个数和总和。可以通过树状数组维护,整个问题的复杂度O((n+W)logn).
    代码如下:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <map>
  6. #include <queue>
  7. #include <cmath>
  8. #include <vector>
  9. #include <ctime>
  10. #include <cctype>
  11.  
  12. using namespace std;
  13.  
  14. #define mem0(a) memset(a, 0, sizeof(a))
  15. #define lson l, m, rt << 1
  16. #define rson m + 1, r, rt << 1 | 1
  17. #define define_m int m = (l + r) >> 1
  18. #define Rep(a, b) for(int a = 0; a < b; a++)
  19. #define lowbit(x) ((x) & (-(x)))
  20. #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
  21. #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
  22.  
  23. typedef double db;
  24. typedef long long LL;
  25.  
  26. const int dx[] = {, , -, };
  27. const int dy[] = {, -, , };
  28. const int maxn = 1e4 + ;
  29. const int maxm = 1e5 + ;
  30. const int MD = 1e9 +;
  31.  
  32. struct Point {
  33. int x, y;
  34. bool operator < (const Point &opt) const {
  35. return x < opt.x || x == opt.x && y < opt.y;
  36. }
  37. Point operator - (const Point &opt) const {
  38. return Point(x - opt.x, y - opt.y);
  39. }
  40. constructInt2(Point, x, y);
  41. void inp() {
  42. scanf("%d %d", &x, &y);
  43. }
  44. void outp() {
  45. printf("(%d, %d), ", x, y);
  46. }
  47. };
  48.  
  49. struct Trie {
  50. const static int char_size = ;
  51. int cc;
  52. int cht[][char_size];
  53. int mark[];
  54. Trie() { cc = ; mem0(mark); mem0(cht); }
  55. int Idex(char ch) { return ch - ''; }
  56. void Insert(char s[], int v) {
  57. int pos = ;
  58. for(int i = ; s[i]; i++) {
  59. int id = Idex(s[i]);
  60. if(!cht[pos][id]) cht[pos][id] = ++cc;
  61. pos = cht[pos][id];
  62. }
  63. mark[pos] = v;
  64. }
  65. bool Find(char s[]) {
  66. int pos = ;
  67. for(int i = ; s[i]; i++) {
  68. int id = Idex(s[i]);
  69. if(!cht[pos][id]) return ;
  70. pos = cht[pos][id];
  71. }
  72. return mark[pos];
  73. }
  74. };
  75.  
  76. struct KMP {
  77. int next[];
  78. void GetNext(char s[]) {
  79. mem0(next);
  80. next[] = next[] = ;
  81. for(int i = ; s[i]; i++) {
  82. int j = next[i];
  83. while(j && s[i] != s[j]) j = next[j];
  84. next[i + ] = s[j] == s[i]? j + : ;
  85. }
  86. }
  87. void Match(char s[], char t[]) {
  88. int j = , len = strlen(t);
  89. for(int i = ; s[i]; i++) {
  90. while(j && s[i] != t[j]) j = next[j];
  91. if(s[i] == t[j]) j++;
  92. if(j == len) printf("%d\n", i - len + );
  93. }
  94. }
  95. };
  96.  
  97. struct Matrix {
  98. int a[][];
  99. Matrix operator * (const Matrix &_A) const {
  100. Matrix tmp;
  101. mem0(tmp.a);
  102. for(int i = ; i < ; i++) {
  103. for(int j = ; j < ; j++) {
  104. for(int k = ; k < ; k++) {
  105. tmp.a[i][j] = ((LL)a[i][k] * _A.a[k][j] + tmp.a[i][j]) % MD;
  106. }
  107. }
  108. }
  109. return tmp;
  110. }
  111. };
  112.  
  113. struct Edge {
  114. int u, v;
  115. constructInt2(Edge, u, v);
  116. };
  117.  
  118. struct Segment {
  119. Point a, b;
  120. void inp() {
  121. scanf("%d%d%d%d", &a.x, &a.y, &b.x, &b.y);
  122. if(a.x > b.x) {
  123. swap(a.x, b.x);
  124. swap(a.y, b.y);
  125. }
  126. }
  127. };
  128.  
  129. Matrix CalcMatrix(Matrix a, int n) {
  130. if(n == ) return a;
  131. Matrix tmp = CalcMatrix(a, n >> );
  132. tmp = tmp * tmp;
  133. if(n & ) tmp = tmp * a;
  134. return tmp;
  135. }
  136.  
  137. inline int ReadInt() {
  138. char c = getchar();
  139. while(!isdigit(c)) c = getchar();
  140.  
  141. int x = ;
  142. while(isdigit(c)) {
  143. x = x * + c - '';
  144. c = getchar();
  145. }
  146. return x;
  147. }
  148.  
  149. inline void WriteInt(int i) {
  150. int p = ;
  151. static int buf[];
  152. if(i == ) p++;
  153. else while(i) {
  154. buf[p++] = i % ;
  155. i /= ;
  156. }
  157. for(int j = p - ; j; j--) putchar('' + buf[j]);
  158. }
  159.  
  160. int Cross(Point a, Point b) {
  161. return a.x * b.y - a.y * b.x;
  162. }
  163.  
  164. int Dist2(Point a, Point b) {
  165. int x = a.x - b.x, y = a.y - b.y;
  166. return x * x + y * y;
  167. }
  168. int ConvexHull(Point *p, int n, Point *ch) {
  169. sort(p, p + n);
  170. int m = ;
  171. for (int i = ; i < n; i++) {
  172. while (m > && Cross(ch[m - ] - ch[m - ], p[i] - ch[m - ]) <= ) m--;
  173. ch[m++] = p[i];
  174. }
  175. int k = m;
  176. for (int i = n - ; i >= ; i--) {
  177. while (m > k && Cross(ch[m - ] - ch[m - ], p[i] - ch[m - ]) <= ) m--;
  178. ch[m++] = p[i];
  179. }
  180. if (n > ) m--;
  181. return m;
  182. }
  183.  
  184. template<class edge> struct Graph {
  185. vector<vector<edge> > adj;
  186. Graph(int n) { adj.clear(); adj.resize(n + ); }
  187. Graph() { adj.clear(); }
  188. void resize(int n) { adj.resize(n + ); }
  189. void add(int s, edge e){ adj[s].push_back(e); }
  190. void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
  191. void clear() { adj.clear(); }
  192. vector<edge>& operator [](int t) { return adj[t]; }
  193. };
  194.  
  195. template<class T> struct TreeArray {
  196. vector<T> c;
  197. int maxn;
  198. TreeArray(int n) { c.resize(n + ); maxn = n; }
  199. TreeArray() { c.clear(); maxn = ; }
  200. void clear() { memset(&c[], , sizeof(T) * maxn); }
  201. void resize(int n) { c.resize(n + ); maxn = n; }
  202. void add(int p, T x) { while (p < maxn) { c[p] += x; p += lowbit(p); } }
  203. T get(int p) { T res = ; while (p) { res += c[p]; p -= lowbit(p); } return res; }
  204. T range(int a, int b) { return get(b) - get(a - ); }
  205. };
  206.  
  207. int n, W, H, a[];
  208. LL sum[], step, maxh;
  209. TreeArray<LL> ta(), ta0();
  210. void Check(int h, int r) {
  211. if (sum[n + * W - ] < (LL)h * W) return ;
  212. LL sum1 = ta0.get(h + ), sumall = sum[r] - sum[r - W], c = ta.get(h + );
  213. LL newsum1 = h * c - sum1, newsum2 = sumall - h * W + newsum1;
  214. LL res = max(newsum1, newsum2);
  215. if (res < step || res == step && h > maxh) {
  216. step = res;
  217. maxh = h;
  218. }
  219. }
  220. int main() {
  221. //freopen("in.txt", "r", stdin);
  222. while (cin >> n >> W >> H) {
  223. mem0(a);
  224. for (int i = ; i < n; i++) {
  225. scanf("%d", a + i + W);
  226. }
  227. int total = n + * W;
  228. for (int i = ; i < total; i++) sum[i] = sum[i - ] + a[i];
  229.  
  230. if (sum[total - ] < (LL)H * W) {
  231. puts("-1");
  232. continue;
  233. }
  234.  
  235. ta.clear();
  236. ta0.clear();
  237. step = H * W;
  238. maxh = H;
  239. ta.add(, W );
  240. ta0.add(, );
  241.  
  242. for (int i = W; i < total; i++) {
  243. int num = sum[i] - sum[i - W], ave = num / W;
  244. if (ave < H) ave = H;
  245. ta.add(a[i - W] + , -);
  246. ta0.add(a[i - W] + , -a[i - W]);
  247. ta.add(a[i] + , );
  248. ta0.add(a[i] + , a[i]);
  249. Check(ave, i);
  250. Check(ave + , i);
  251. }
  252. cout << maxh << " " << step << endl;
  253. }
  254. return ;
  255. }

[hdoj5192] 树状数组的更多相关文章

  1. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  2. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  3. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  6. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  7. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  9. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. 参数化parameterized

    pip install parameterized 注意:之前的nose-parameterized已经更新为parameterized库了 模块下测试方法直接使用parameterized impo ...

  2. google无法播放mp4 chrome无法播放h264

    写在前面 我在chrome上无法播放h264+Acc的mp4,在firefox.ie都可以播放,而且此mp4在vlc终可以正常播放. 视频链接:http://106.14.221.185:7001/p ...

  3. [PHP]$_SERVER参数详情

    来源:PHP中$_SERVER的详细参数与说明 $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关.$_SERVER['argv'] #传递给该脚 ...

  4. react: typescript system params optimize

    1.system-params-service import paramCache from "../common/param-cache" import RequestPromi ...

  5. pytorch实现手动线性回归

    import torch import matplotlib.pyplot as plt learning_rate = 0.1 #准备数据 #y = 3x +0.8 x = torch.randn( ...

  6. python爬虫(1)requests库

    在pycharm中安装requests库的一种方法 首先找到设置 搜索然后安装,蓝色代表已经安装 requests库中的get请求 与HTTP协议相对应,requests库也有七种请求方式. 获取ur ...

  7. 2019-2020-1 20199308《Linux内核原理与分析》第七周作业

    <Linux内核分析> 第六章 进程的描述和进程的创建 6.1 进程的描述 操作系统内核实现操作系统的三大管理功能: 进程管理(进程)-核心 内存管理(虚拟内存) 文件系统(文件) 为了管 ...

  8. Gym 101194F Mr. Panda and Fantastic Beasts

    #include<bits/stdc++.h> using namespace std; #define ms(arr,a) memset(arr,a,sizeof arr) #defin ...

  9. 被@ResponseBoby注释的方法在拦截器的posthandle方法中设置cookie失效的问题

    文章标题可能有点绕口.先来解释下遇到的问题. 我写了一个拦截器,希望能够实现保存特定方法的请求参数到cookie中. public class SaveParamInterceptor extends ...

  10. 常用的CSS小技巧

    实际开发过程中会遇到一些需要用CSS小技巧处理的布局问题,现在分享几个个人工作中遇到的小问题和解决方案. 1.inline元素间的空白间隙 这里要介绍一个神器font-size:0. 如果你写了个列表 ...