题目

展开
题目描述
Andrew skipped lessons on the subject ‘Algorithms and Data Structures’ for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment.

The teacher gave Andrew an array of nn numbers a_{1}a
1

, … , a_{n}a
n

. After that he asked Andrew for each kk from 1 to n-1n−1 to build a kk -ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent.

Andrew looked up on the Wikipedia that a kk -ary heap is a rooted tree with vertices in elements of the array. If the elements of the array are indexed from 1 to nn , then the children of element vv are elements with indices k(v-1)+2k(v−1)+2 , … , kv+1kv+1 (if some of these elements lie outside the borders of the array, the corresponding children are absent). In any kk -ary heap every element except for the first one has exactly one parent; for the element 1 the parent is absent (this element is the root of the heap). Denote p(v)p(v) as the number of the parent of the element with the number vv . Let’s say that for a non-root element vv the property of the heap is violated if a_{v}<a_{p(v)} .

Help Andrew cope with the task!

输入格式
The first line contains a single integer nn ( 2<=n<=2·10^{5}2<=n<=2⋅10
5
).

The second line contains nn space-separated integers a_{1}a
1

, … , a_{n}a
n

( -10{9}<=a_{i}<=10{9}−10
9
<=a
i

<=10
9
).

输出格式
in a single line print n-1n−1 integers, separate the consecutive numbers with a single space — the number of elements for which the property of the kk -ary heap is violated, for k=1k=1 , 22 , … , n-1n−1 .

题意翻译
有一个小根堆,但是因为奇奇怪怪的原因,这个堆变成了一个k叉堆,而且不一定是合法的(即有若干个节点小于它的父亲)。问现在这个堆有多少不合法的元素。

输入输出样例
输入 #1复制
5
1 5 4 3 2
输出 #1复制
3 2 1 0
输入 #2复制
6
2 2 2 2 2 2
输出 #2复制
0 0 0 0 0
说明/提示
Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red.

In the second sample all elements are equal, so the property holds for all pairs.

思路

我们观察一个对于一棵 jj 叉树,求他的父亲的公式: (i + j - 2) /i。

这表示啥?这不就是表示对于许多的节点,他的父亲相同吗。

对于父亲相同的节点,我们把它分成一个块,然后直接用一个差分数组实现区间加,先判断是否不合法,不合法就用差分数组搞一搞,最后做一遍前缀和,输出答案即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,a[N];
int ans[N];
int main()
{n=readint();for(int i=1; i<=n; i++) a[i]=readint();for(int i=2; i<=n; i++){int l=1;while(l<=i-2){int r=(i-2)/((i-2)/l);if(a[(i-2)/l+1]>a[i]){ans[l]++;ans[r+1]--;}l=r+1;}if(a[1]>a[i]) ans[l]++;}for(int i=2; i<n; i++) ans[i]+=ans[i-1];for(int i=1; i<n; i++) printf("%d ",ans[i]);printf("\n");return 0;
}

【CF538F】 A Heap of Heaps相关推荐

  1. 【降维解法:最大字段和-最大子矩阵和-最终版最大子长方体和】【UVA10755】Garbage Heap...

    突然感觉刷完这一套专题后 码力有了质的飞跃,fighting 努力会有结果! 最大字段和是一个很经典的问题 O(n)算法 而对于最大子矩阵和 可以思考一个这样的想法 枚举上下边界i,j把i到j这一段的 ...

  2. 【题解】CF538F:A Heap of Heaps

    原题传送门 按照权值从小到大排序 找的是儿子中小于自己的有几个,这样我们就顺着枚举,满足权值递增,用树状数组维护个数,对于一个xxx,kkk叉树,儿子区间为[xk+1−k,xk+1][xk+1-k,x ...

  3. 【Test】[20111011][数论+记忆化搜索+枚举(Heap优化)+SPFA]

    [闲话] 成绩: 第一题(Count):AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 第二题(Length):AAATTTTTTT 第三题(St ...

  4. 【Kafka】kafka OutOfMemoryError: Direct buffer memory Java heap space

    1.背景 做实验:[Kafka]Kafka如何开启SSL 控制台消费与生产 代码消费与生产 的时候,因为机器太卡导致内存溢出 java.lang.OutOfMemoryError: Direct bu ...

  5. [转]机器学习和深度学习资料汇总【01】

    本文转自:http://blog.csdn.net/sinat_34707539/article/details/52105681 <Brief History of Machine Learn ...

  6. 【github】机器学习(Machine Learning)深度学习(Deep Learning)资料

    转自:https://github.com/ty4z2008/Qix/blob/master/dl.md# <Brief History of Machine Learning> 介绍:这 ...

  7. 机器学习(Machine Learning)深度学习(Deep Learning)资料【转】

    转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...

  8. 【PAT】PAT总结《搜索、打表、分治、排序算法、队列、栈、堆、Hash》

    打表 打表这个技巧其实我们在素数部分的题已经涉及到了.还是要仔细思考,什么时候应该打表,打表有助于减少后续的计算的时候,我们应该打表, 特别是查询量特别大,即时计算无论多么快都会超时的情况.或者逆向运 ...

  9. 【JDK7】新特性(2) 语法

    2019独角兽企业重金招聘Python工程师标准>>> JDK7对Java语法有少量更新,重点是在易用性和便捷性的改进.     1.二进制字面量 JDK7开始,终于可以用二进制来表 ...

最新文章

  1. 傅里叶变换取代Transformer自注意力层,谷歌这项研究GPU上快7倍、TPU上快2倍
  2. asp.net基础复习(二)——母版页
  3. 2015 SegmentFault 黑客马拉松记录
  4. Java线程安全StampedLock
  5. uva 524(Prime Ring Problem UVA - 524 )
  6. 95-10-070-启动-SocketServer启动
  7. 2021 年高教社杯全国大学生数学建模竞赛题目(C 题 生产企业原材料的订购与运输)
  8. 任正非:华为不会拆分;以色列公司称可解锁所有 iOS 设备;Java 13 要来了! | 极客头条...
  9. linux删除5天前文件和目录,Linux Shell命令定时删除指定目录下n天前的文件
  10. Javascript常用语法 (一)
  11. 图论最短路问题和最小生成树问题的区别
  12. day73 中间件 以及模板引擎
  13. (转)高盛将“穷人”理财交给机器人,那智能投顾在中国怎么玩?
  14. 基于php技术的毕业设计管理系统的研究与实现,基于PHP的图书管理系统设计与实现毕业论文.DOC...
  15. Linux命令:md5sum
  16. java 米与厘米 转换_米转码换算(米与码的换算关系)
  17. 【Grafana】个人体验
  18. 连接共享服务器显示拒绝访问权限,无权限访问共享打印机,windows无法连接到打印机拒绝访问 怎么办?...
  19. 任正非:向谷歌军团学习,扑上去,杀出一条血路
  20. 机器学习项目实战(七) 机器学习预测股价

热门文章

  1. 【译】Flutter中的花式背景动画
  2. 如果是你话,跳还是不跳?
  3. 2020/03/14更新:获取、导出微信所有表情
  4. 算法时间复杂度和空间复杂度详解
  5. 80天80亿,寒冬下的暗流涌动——你所不了解的EOS DApp生态大爆发
  6. Java贪吃蛇游戏(简单粗暴版)
  7. ensp 三层架构配置
  8. 【数据库-MySql】[Err] 1062 - Duplicate entry '1-1' for key 'PRIMARY'
  9. Mac Navicat远程连接群晖MariaDB 10
  10. Python爬虫有什么用?