POJ-1990 moofest---Two tree-like arrays

Source: Internet
Author: User

Topic Links:

https://vjudge.net/problem/POJ-1990

Main topic:

A group of cows to participate in the cattle after the festival has a different degree of deafness, the first cow heard someone else's speech, other people's volume must be greater than v[i], when the two cows i,j communication, the smallest voice of communication for max{v[i],v[j]}* between them distance. Now there are n cows, and ask them for a minimum of 22 communication between the volume and.

Problem Solving Ideas:

Using a tree-like array, the first two tuples according to the size of the V from small to large order, so that each cow than the front of the top of the V, calculate it and the cow in front of the volume and the time, you can directly use the cow's V, but also to calculate the |a[i].x-x| The sum of the absolute values.

Maintaining coordinate x with a tree array, you can directly find the sum of all x smaller than this cow, and also need to maintain the number of occurrences of each element in another tree array, and directly find the number of cows smaller than the x of the cow.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <map>6#include <Set>7#include <cmath>8#include <algorithm>9#include <vector>Ten#include <sstream> One #defineLowbot (i) (i& (-i)) A using namespacestd; -typedefLong Longll; - Const intMAXN = 1e5 +Ten; the structCow - { - ll v, x; -     BOOL operator< (Constcow& a)Const +     { -         returnV < A.V | | v = = A.V && x <a.x; +     } A }A[MAXN]; at intTREE[MAXN], ID_NUM[MAXN]; - voidAddintXintDinttree[]) - { -      while(x <= MAXN)//Upper limit is MAXN -     { -TREE[X] + =D; inX + =Lowbot (x); -     } to } +ll sum (intXinttree[]) - { thell ans =0; *      while(x) $     {Panax NotoginsengAns + =Tree[x]; -X-=Lowbot (x); the     } +     returnans; A } the intMain () + { -     intN; $scanf"%d", &n); $      for(inti =0; I < n; i++) scanf ("%lld%lld", &AMP;A[I].V, &a[i].x); -Sort (A, A +n); -ll num, tot, ans =0; the      for(LL i =0; I < n; i++) -     {Wuyinum =sum (a[i].x, id_num); thetot =sum (a[i].x, tree); -Ans + = (num * a[i].x-tot) *a[i].v; Wu         //cout<<num<< "-" <<tot<< "+" <<ans<<endl; -num = i-num; Abouttot = SUM (20000, tree)-tot; $Ans + = (tot-num * a[i].x) *a[i].v; -         //cout<<num<< "-" <<tot<< "-" <<ans<<endl; - Add (a[i].x, a[i].x, tree); -Add (a[i].x,1, id_num); A     } +cout<<ans<<Endl; the     return 0; -}

POJ-1990 moofest---Two tree-like arrays

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.