hdu 6133---Army Formations(启发式合并+树状数组)
2017-09-01 19:01
387 查看
题目链接
[align=left]Problem Description[/align]
> Stormtroopers were the assault/policing troops of the Galactic Empire. Dissenting citizens referred to them as bucketheads, a derogatory nickname inspired by the bucket-shaped helmets of stormtroopers. They wore white armor made from plastoid over a black body glove which, in addition to creating an imposing image, was outfitted with a wide array of survival equipment and temperature controls that allowed its wearer to survive in most environments, and were designed to disperse blaster bolt energy. As members of the Stormtrooper Corps, an independent branch that operated under the Imperial Army, stormtroopers represented the backbone of the Imperial Military—trained for total obedience to the command hierarchy, as well as absolute loyalty to Emperor Sheev Palpatine and the Imperial regime. Stormtroopers were trained at Imperial Academies, and used a variety of weapons.
>
> --- Wookieepedia
Though being cruel and merciless in the battlefields, the total obedience to the command hierarchy makes message delivering between Stormtroopers quite inefficient, which finally caused the escape of Luke Skywalker, the destroy of the Death Star, and the collapse of the Galactic Empire.
In particular, the hierarchy of Stormtroopers is defined by a *binary tree*. Everyone in the tree has at most two direct subordinates and exactly one direct leader, except the first (numbered 1) Stormtrooper, who only obey the order of the Emperor.
It has been a time-consuming task for the Stormtroopers to input messages into his own log system. Suppose that the i-th Stormtrooper has a message of length ai, which means that it costs ai time to input this message into a log system. Everyone in the hierarchy has the mission of collecting all messages from his subordinates (a direct or indirect children in the tree) and input thses messages and his own message into his own log system.
Everyone in the hierarchy wants to optimize the process of inputting. First of all, everyone collects the messages from all his subordinates. For a Stormtrooper, starting from time 0, choose a message and input it into the log system. This process proceeds until all messages from his subordinates and his own message have been input into the log system. If a message is input at time t, it will induce t units of penalty.
For every Stormtrooper in the tree, you should find the minimum penalty.
[align=left]Input[/align]
The first line of the input contains an integer T, denoting the number of test cases.
In each case, there are a number n (1≤n≤105) in the first line, denoting the size of the tree.
The next line contains n integers, the i-th integer denotes ai (0≤ai≤108), the i-th Stormtrooper’s message length.
The following n−1 lines describe the edges of the tree. Each line contains two integers u,v (1≤u,v≤n), denoting there is an edge connecting u and v.
[align=left] [/align]
[align=left]Output[/align]
For each test case, output n space-separated integers in a line representing the answer. i-th number is the minimum penalty of gathering messages for i-th Stormtrooper.
[align=left] [/align]
[align=left]Sample Input[/align]
1
3
1 2 3
1 2
2 3
[align=left] [/align]
[align=left]Sample Output[/align]
10 7 3
题意:该校出题题目太长了,而且表达的不是很清楚,太难理解了。 该题简而言之:有一个由 n 个节点构成的二叉树,每个节点上有一个权值wi,求所有节点的一个计算值。对于某个节点来说,以它为根节点的子树的所有的节点的值构成的序列的前缀和的和,即为这个节点的计算值,对于每个节点求最小的计算值,那么很明显贪心,把这个序列从小到大排序,然后再就算前缀和的和。
思路:官方题解说是启发式合并,我不懂什么是启发式,我看了别人写的代码后,我的理解是对于左右子树合并时,把小的子树合并到大的子树中去,这样复杂度较小,我觉得这个过程就是应用了启发式思想。 本题思路没什么好说的,只想简单说一下合并,本题中的计算是由树状数组完成的,对于每次在已有节点上添加一个节点x时,这些数的前缀和的和变化为:tmp+=大于w[x]的数个数*w[x]+w[x]+小于w[x]的所有数的和。
代码如下:
[align=left]Problem Description[/align]
> Stormtroopers were the assault/policing troops of the Galactic Empire. Dissenting citizens referred to them as bucketheads, a derogatory nickname inspired by the bucket-shaped helmets of stormtroopers. They wore white armor made from plastoid over a black body glove which, in addition to creating an imposing image, was outfitted with a wide array of survival equipment and temperature controls that allowed its wearer to survive in most environments, and were designed to disperse blaster bolt energy. As members of the Stormtrooper Corps, an independent branch that operated under the Imperial Army, stormtroopers represented the backbone of the Imperial Military—trained for total obedience to the command hierarchy, as well as absolute loyalty to Emperor Sheev Palpatine and the Imperial regime. Stormtroopers were trained at Imperial Academies, and used a variety of weapons.
>
> --- Wookieepedia
Though being cruel and merciless in the battlefields, the total obedience to the command hierarchy makes message delivering between Stormtroopers quite inefficient, which finally caused the escape of Luke Skywalker, the destroy of the Death Star, and the collapse of the Galactic Empire.
In particular, the hierarchy of Stormtroopers is defined by a *binary tree*. Everyone in the tree has at most two direct subordinates and exactly one direct leader, except the first (numbered 1) Stormtrooper, who only obey the order of the Emperor.
It has been a time-consuming task for the Stormtroopers to input messages into his own log system. Suppose that the i-th Stormtrooper has a message of length ai, which means that it costs ai time to input this message into a log system. Everyone in the hierarchy has the mission of collecting all messages from his subordinates (a direct or indirect children in the tree) and input thses messages and his own message into his own log system.
Everyone in the hierarchy wants to optimize the process of inputting. First of all, everyone collects the messages from all his subordinates. For a Stormtrooper, starting from time 0, choose a message and input it into the log system. This process proceeds until all messages from his subordinates and his own message have been input into the log system. If a message is input at time t, it will induce t units of penalty.
For every Stormtrooper in the tree, you should find the minimum penalty.
[align=left]Input[/align]
The first line of the input contains an integer T, denoting the number of test cases.
In each case, there are a number n (1≤n≤105) in the first line, denoting the size of the tree.
The next line contains n integers, the i-th integer denotes ai (0≤ai≤108), the i-th Stormtrooper’s message length.
The following n−1 lines describe the edges of the tree. Each line contains two integers u,v (1≤u,v≤n), denoting there is an edge connecting u and v.
[align=left] [/align]
[align=left]Output[/align]
For each test case, output n space-separated integers in a line representing the answer. i-th number is the minimum penalty of gathering messages for i-th Stormtrooper.
[align=left] [/align]
[align=left]Sample Input[/align]
1
3
1 2 3
1 2
2 3
[align=left] [/align]
[align=left]Sample Output[/align]
10 7 3
题意:该校出题题目太长了,而且表达的不是很清楚,太难理解了。 该题简而言之:有一个由 n 个节点构成的二叉树,每个节点上有一个权值wi,求所有节点的一个计算值。对于某个节点来说,以它为根节点的子树的所有的节点的值构成的序列的前缀和的和,即为这个节点的计算值,对于每个节点求最小的计算值,那么很明显贪心,把这个序列从小到大排序,然后再就算前缀和的和。
思路:官方题解说是启发式合并,我不懂什么是启发式,我看了别人写的代码后,我的理解是对于左右子树合并时,把小的子树合并到大的子树中去,这样复杂度较小,我觉得这个过程就是应用了启发式思想。 本题思路没什么好说的,只想简单说一下合并,本题中的计算是由树状数组完成的,对于每次在已有节点上添加一个节点x时,这些数的前缀和的和变化为:tmp+=大于w[x]的数个数*w[x]+w[x]+小于w[x]的所有数的和。
代码如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <vector> using namespace std; typedef long long LL; const int N=1e5+5; int w ; int val ,id ; int sz ,lson ,rson ,tot; vector<int>e ; LL num ,sum ; LL ans ,tmp; int calSize(int now,int pre) { sz[now]=1; for(int j=0;j<e[now].size();j++) { int to=e[now][j]; if(to==pre) continue; if(!lson[now]) lson[now]=to; else rson[now]=to; sz[now]+=calSize(to,now); } if(lson[now]&&rson[now]&&sz[lson[now]]>sz[rson[now]]) swap(lson[now],rson[now]); return sz[now]; } int lowbit(int x) { return x&(-x); } void update(LL *a,int x,int y) { while(x<=tot) { a[x]+=(LL)y; x+=lowbit(x); } } LL query(LL *a,int x) { LL r=0; while(x) { r+=a[x]; x-=lowbit(x); } return r; } void add(int x) { tmp+=(query(num,tot)-query(num,id[x]))*(LL)w[x]+(LL)w[x]; tmp+=query(sum,id[x]); update(num,id[x],1); update(sum,id[x],w[x]); } void del(int x) { update(num,id[x],-1); update(sum,id[x],-w[x]); tmp-=(query(num,tot)-query(num,id[x]))*(LL)w[x]+(LL)w[x]; tmp-=query(sum,id[x]); } void cle(int x) { del(x); if(lson[x]) cle(lson[x]); if(rson[x]) cle(rson[x]); } void validate(int x) { add(x); if(lson[x]) validate(lson[x]); if(rson[x]) validate(rson[x]); } void dfs(int now) { if(!lson[now]) { ans[now]=(LL)w[now]; add(now); return ; } dfs(lson[now]); if(rson[now]) { cle(lson[now]); dfs(rson[now]); validate(lson[now]); } add(now); ans[now]=tmp; } int main() { int T; cin>>T; while(T--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&w[i]); val[i]=w[i]; e[i].clear(); num[i]=sum[i]=0; lson[i]=rson[i]=0; } sort(val+1,val+n+1); tot=unique(val+1,val+n+1)-val-1; for(int i=1;i<=n;i++) { id[i]=lower_bound(val+1,val+tot+1,w[i])-val; } for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); e[u].push_back(v); e[v].push_back(u); } calSize(1,-1); tmp=0; dfs(1); for(int i=1;i<=n;i++) printf("%lld ",ans[i]); puts(""); } return 0; }
相关文章推荐
- HDU - 6133 Army Formations(启发式合并+树状数组)
- hdu 5997 rausen loves cakes [启发式合并+树状数组/线段数]【杂类+数据结构】
- HDU 6133 Army Formations 树状数组 + 启发式合并
- 【Splay】【启发式合并】hdu6133 Army Formations
- HDU 5997 rausen loves cakes (启发式合并+树状数组)
- 1483: [HNOI2009]梦幻布丁 链表+启发式合并
- BZOJ 3123 森林 主席树启发式合并
- 并查集的优化---路径压缩与启发式合并
- 【Educational Codeforces Round 2E】【STL-map 启发式合并 or 线段树动态开节点 】Lomsat gelral 一棵树每点一个颜色问每个节点子树的颜色众数之和
- bzoj 2733: [HNOI2012]永无乡(线段树启发式合并)
- bzoj 2733: [HNOI2012]永无乡(线段树启发式合并)
- bzoj1483[HNOI2009]梦幻布丁 链表+启发式合并
- Educational Codeforces Round 2 E. Lomsat gelral(启发式合并)
- [线段树 启发式合并] BZOJ 2733 永无乡
- [BZOJ2733][HNOI2012]永无乡(并查集+splay启发式合并)
- treap启发式合并(BZOJ2809)||splay||左偏树
- 【HDU】5197 DZY Loves Orzing 【FFT启发式合并】
- 数据结构(启发式合并):HNOI 2009 梦幻布丁
- csu oj 1811: Tree Intersection (启发式合并)
- CSU 1811 Tree Intersection(启发式合并)