HackerRank - array-partition 并查集】的更多相关文章

HackerRank - journey-to-the-moon [并查集] 题意 有很多不同的宇航员,有些宇航员来自同一个国家,有些宇航员来自不同的国家,然后美国航天局想要选出两名来自不同国家的宇航员,求出最大的选法.然后 数据一对一对的给出 给出的说明这两人是来自同一个国家的. 思路 使用并查集并压缩路径,然后最后在MAP 里面存放的就是 有几个"祖先",并且这个祖先里面下属加上它一共有多少人 然后求不同的人数 就是排列组合 AC代码 #include <iostream&g…
D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Initially there was an array $$$a$$$ consisting of $$$n$$$ integers. Positions in it are numbered from $$$1$$$ to $$$n$$…
原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n non-negative integers a1, a2, ..., an. You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1…
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given an array consisting of n non-negative integers a1, a2, ..., an. You are going to destro…
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an array consisting of n non-negative integers a1, a2, ..., an. You are going to destroy integers in the array one by one. Thus, you are given the permuta…
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given an array consisting of n non-negative integers a1, a2, ..., an. You are going to destroy integers in the array…
用并查集维护线段,从后往前枚举没个删除的位置id[i] 那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的. 然后现在进行合并 int left = id[i + 1];(相当于每次都加入一个元素a[left]) 它在这个位置,如果能和左右的合并,就是左右邻居都有元素,那么当然是合并最好,因为元素都是大于0的,越长越好. 合并的时候再记录一个数组sum[pos]表示以这个为根的总和是多少.按并查集的思路更新整个并查集即可. 注意一点的就是, 要求ans[i] 那么an…
题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存在,那么就合并起来, 然后不断最大值,因为这个最大值肯定是不递减,所以我们一直更新就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <s…
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3963 题意:给出一个n个数的序列,可以在其中找一个子序列建堆,并且堆中的父亲结点j和孩子结点i满足sj ≤ si and j < i.问要分配所有的数到堆里面,最少可以建多少个堆. 思路:对于每一个数,如果前面有小于等于它的数并且那个数的左右孩子还没满,那么就可以放在它的下面.考虑最优情况,就应该是每次插入到左右孩子还没满的,并且小于等于当前枚举的数的最大的数,然后插入到…
Codeforces Round #268 (Div. 2)D Codeforces Round #268 (Div. 1)B CF468B D. Two Sets time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little X has n distinct integers: p1, p2, ..., pn. He want…