Bzoj 2809 Apio-dispatching balanced tree heuristic merging

Source: Internet
Author: User

The main idea: give a tree, each node has two values, respectively, the Ninja's salary and ninja leadership. Customer satisfaction is the point of leadership can be achieved by the number of people, provided that the sum of the salary of the person does not exceed the total amount of money.


Thought: Only in the sub-tree operation, greedy think, we as long as the sub-tree cost the smallest of those points can be. So on a deep search, each to a node, put itself and all the child nodes of the balance tree heuristic and and then retain no more than the total amount of money in the number of statistics. Data range is large, can open long long where not stingy.

PS: Spit Trough, the beginning of the problem has been TTT, I thought it was my constant write too big, others are left to the heap write, is not the balance of the tree has become the tears of the Times ... Then I got the test point, ran the first set of data and so on for more than 1 more than a solution. I feel like I have to rewrite again and go out for a spin. 10 minutes later, I came back and found out! And incredibly still!! Then I looked carefully over the program ... Found to be heuristic merge write inverse .... Write the reverse ... The reverse ... The ...


CODE:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 100010using namespace Std;struct complex{long long Cost,leader;} Point[max];struct treap{int Random,size,cnt;long long val,sum; Treap *son[2]; Treap (Long Long _) {val = sum = _;size = CNT = 1;random = rand (); son[0] = son[1] = NULL;} int Compare (long long x) {if (x = = val) Return-1;return x > val;} void maintain () {size = Cnt;sum = val * cnt;if (son[0]! = NULL) Size + = Son[0]->size,sum + son[0]->sum;if (son[1]! = N ULL) Size + = Son[1]->size,sum + = Son[1]->sum;} *tree[max];long long points,money;int head[max],total;int next[max << 1],aim[max << 1];long Long Ans;inline V OID Add (int x,int y) {next[++total] = head[x];aim[total] = y;head[x] = total;} inline void Rotate (treap *&a,bool dir) {treap *k = A->son[!dir];a->son[!dir] = K->son[dir];k->son[dir] = a ; A->maintain (), K->maintain (); a = k;} inline void Insert (treap *&a,long long x) {if (a = = NULL) {a = new treap (x); return;} int dir = a->compare (x); if (dir = =-1) ++a->cnt;else {Insert (a->son[dir],x); if (A->son[dir]->random > A->random) Rotate (A,!dir);} A->maintain ();} inline int Findmax (treap *a) {return a->son[1] = = NULL? A->val:findmax (A->son[1]);} inline void Delete (treap *&a,long long x) {int dir = a->compare (x); if (dir! =-1) Delete (a->son[dir],x); else {if (a ->cnt > 1)--a->cnt;else {if (a->son[0] = = null) A = A->son[1];else if (a->son[1] = = null) A = A->son[0];e LSE {BOOL _ = (A->son[0]->random > A->son[1]->random); Rotate (a,_);D elete (a->son[_],x);}} if (A! = NULL) A->maintain ();} void Transfrom (Treap *&from,treap *&aim) {if (from = = NULL) return; Transfrom (From->son[0],aim); Transfrom (From->son[1],aim); for (int i = 1; I <= from->cnt; ++i) Insert (aim,from->val);d elete From;from = NULL ;} void DFS (int x) {Tree[x] = new Treap (point[x].cost); if (point[x].cost <= money) ans = max (ans, (long long) poinT[x].leader), if (!head[x]) return, for (int i = head[x]; i; i = Next[i]) {DFS (aim[i]); if (Tree[x]->size < tree[aim[i]]- >size) Swap (tree[x],tree[aim[i]); Transfrom (Tree[aim[i]],tree[x]);} while (Tree[x]->sum > Money) Delete (Tree[x],findmax (tree[x)); ans = max (ans, (long Long) tree[x]->size * point[x ].leader);} int main () {cin >> points >> money;for (int x,i = 1; I <= points; ++i) {scanf ("%d%lld%lld", &x,&point[ I].cost,&point[i].leader); ADD (x,i);} DFS (0); cout << ans << endl;return 0;}


Bzoj 2809 Apio-dispatching balanced tree heuristic merging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.