"bzoj2947" [Poi2000] Promotions

Source: Internet
Author: User

2947: [Poi2000] Promotion time limit:10 Sec Memory limit:128 MB
submit:181 solved:120
[Submit] [Status] [Discuss] Description

Bytelandish Chain supermarket commissioned you to write a program to simulate a forthcoming promotion, the rules of the activity are as follows:
To participate in the customer, just write his personal information on the bill, and put the bill into the ballot box;
At the end of each day, the largest and smallest two bills are taken out, and the customer with the largest payment will receive a bonus, which is worth the difference in the amount of the two bills taken out;
In order not to repeat the calculation, the two bills taken out are no longer returned to the box, while the remaining bills remain in the box for the next day's activity.
The daily turnover of supermarkets is large, so it can be assumed that at the end of each day, there are at least two bills in the box for removal.
Your task is to calculate the total amount of bonuses paid by supermarkets during the campaign, based on the daily Bill in the box.
Task:
Write a program to complete the following tasks:
Read the information of the bill into the input box;
Calculate the total amount of bonuses during the promotion period;

The first line of input is an integer n (1 <= n <= 5000), indicating the number of days the campaign lasted. The following n rows, each containing a number of non-negative integers separated by spaces. The number of line i+1 indicates the amount of the bill to be put into the box on day I. The first line of each row is an integer k (0 <= K <= 105) that represents the number of daily bills. The following k positive integers represent the amount of the K-bill, which is less than 106. There will be no more than 106 bill pens involved in the entire event. The output unique line is an integer equal to the total amount of the bonus that should be paid for the entire campaign. Sample Input5
3 1 2 3
2 1 1
4 10 5) 5 1
0
1 2
Sample Output19
"Problem" balance tree naked, it is said that can be a variety of water over
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <ctime>6#include <cmath>7#include <algorithm>8 using namespacestd;9typedefLong Longll;Ten structData{ll L,r,v,size,fix,w;} tr[1000005]; One ll T,rt,ans,len; A Charbuf[1<< the],*fs,*ft; -InlineCharGetc () {return(fs==ft&& (ft= (fs=buf) +fread (buf,1,1<< the, stdin), fs==ft))?0:* fs++;} - inline ll read () the { -ll x=0, f=1;CharCh=getc (); -      while(!isdigit (CH)) {if(ch=='-') f=-1; Ch=getc ();} -      while(IsDigit (CH)) {x=x*Ten+ch-'0'; Ch=getc ();} +     returnx*F; - } + voidUpdate (ll k) {tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+TR[K].W;} A voidRturn (ll &k) {ll t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;tr[t].size=tr[k].size;update (k); k=t;} at voidLturn (ll &k) {ll t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;tr[t].size=tr[k].size;update (k); k=t;} - voidInsert (LL &k,ll x) - { -     if(k==0) {len++;k=len;tr[k].size=tr[k].w=1; Tr[k].v=x;tr[k].fix=rand ();return;} -tr[k].size++; -     if(tr[k].v==x) tr[k].w++; in     Else if(X&GT;TR[K].V) {Insert (TR[K].R,X);if(tr[tr[k].r].fix<Tr[k].fix) Lturn (k);} -     Else{Insert (TR[K].L,X);if(tr[tr[k].l].fix<Tr[k].fix) Rturn (k);}  to } + voidDel (LL &k,ll x) - { the     if(k==0)return;  *     if(tr[k].v==x) $     {Panax Notoginseng         if(tr[k].w>1) {tr[k].w--;tr[k].size--;return;} -         if(tr[k].l*tr[k].r==0) K=TR[K].L+TR[K].R;//There was a son who was empty the         Else if(tr[tr[k].l].fix<Tr[tr[k].r].fix) Rturn (k), Del (k,x); +         ElseLturn (k), Del (k,x); A     } the     Else if(X&GT;TR[K].V) tr[k].size--, Del (tr[k].r,x); +     Elsetr[k].size--, Del (tr[k].l,x); - } $LL Minn (ll P) { while(TR[P].L) P=tr[p].l;returntr[p].v;} $LL Maxx (ll p) { while(TR[P].R) P=TR[P].R;returntr[p].v;} - intMain () - { the     //freopen ("cin.in", "R", stdin); -     //freopen ("Cout.out", "w", stdout);Wuyit=read (); the      while(t--) -     { Wull n=read (); -          for(LL i=1; i<=n;i++) {ll x=read (); Insert (rt,x);} AboutLL Temp1=minn (RT), temp2=Maxx (RT); $ans+=temp2-Temp1; - del (RT,TEMP2); Del (RT,TEMP1); -     } -printf"%lld\n", ans); A     return 0; +}

"bzoj2947" [Poi2000] Promotions

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.