Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
帮着看看,http://ace.delos.com/TESTDATA/NOV06_4.htm的测试数据都过了,但还是WA,无语#include<iostream> #include<stdlib.h> using namespace std; struct Node{ unsigned long long weight; unsigned int lchild, rchild; }; Node nodes[40010]; unsigned long long ret_val = 0; void tr(int root, int depth) { if (nodes[root].lchild == 0 && nodes[root].rchild == 0) { ret_val += depth*nodes[root].weight; return; } tr(nodes[root].lchild, depth+1); tr(nodes[root].rchild, depth+1); } int cmp(const void *a, const void *b) { struct Node *c = (Node*)a; struct Node *d = (Node*)b; return c->weight> d->weight; } int main(void) { int n; cin>>n; for(int i=1; i<=n; i++) { int weight; cin>>weight; nodes[i].weight = weight; nodes[i].lchild = 0; nodes[i].rchild = 0; } qsort(nodes+1,n,sizeof(Node), cmp); for (int i=n+1; i<=2*n-1; i++) { nodes[i].weight = 1000000000; nodes[i].lchild = 0; nodes[i].rchild = 0; } int m = n+1; int k = 1; for (int i=1; i<=n-1;i++){ Node n1; Node n2; int left; int right; if (i == 1) { n1 = nodes[k]; left = k; k++; n2 = nodes[k]; right = k; k++; } else { if (k <=n && nodes[k].weight < nodes[m].weight) { n1 = nodes[k]; left = k; k++; } else { n1 = nodes[m]; left = m; m++; } if (k<=n && nodes[k].weight < nodes[m].weight) { n2 = nodes[k]; right = k; k++; } else { n2 = nodes[m]; right = m; m++; } } //cout<<"left = " << left << " right = " << right <<endl; Node n3; n3.weight = n1.weight+n2.weight; n3.lchild = left; n3.rchild = right; nodes[n+i] = n3; } tr(2*n-1, 0); cout<< ret_val<<endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator