[HNOI2004] Pet Adoption
★ ¡ï Input File: pet.in
output file: pet.out
Simple comparison
Time limit: 1 s memory limit: MB
Recently, a Q opened a pet house. The adoption agency offers two services: Adopt pets abandoned by their owners and allow new owners to adopt them. Each adopter wants to adopt a pet that he or she is satisfied with, ah Q according to the adopter's request, through a special formula invented by himself, the adoptive pet's characteristic value A (A is a positive integer, a<2^31), and he gives each pet in the adoption of a characteristic value. So that he can easily deal with the entire adoption of pets, pet adoption will always have two things: abandoned pets too much or want to adopt a pet too many people, and too few pets.
1. When a pet is abandoned, if an adopter is to be adopted, the adoptive pet is expected to have a characteristic value of a, then it will adopt a pet that is closest to a in a pet that is currently not adopted. (None of the two pet's feature values can be the same, and any two adopters may not have the same characteristic value for the adopted pet) if there are two pets that meet the requirements, that is, there are two pets. Their characteristics are a-B and a+b, then the adopter will adopt the pet with a A-B characteristic value. 2. There are too many people to adopt a pet, and if a pet is adopted, which one will adopt it? The adopter who is able to adopt it is the one that wants to be adopted. The trait of the pet is closest to that of the pet, and if the pet has a characteristic value of a, there are two adopters who wish to adopt a pet with a characteristic value of a-B and a+b, then the adoptive person with a A-B trait will succeed in adopting the pet. An adopter has adopted a pet with a characteristic value of a, and it wants to adopt a pet with a characteristic value of B, so the adopter's dissatisfaction is abs (A-a).
"Task description"You've got a year in which the adoptive and adopted pets come to the adoption, and you want to count the total dissatisfaction of all the adoptive adopters who adopted the pet. At the beginning of the year, there were neither pets nor adopters in the adoption.
"input Format"You will read the data in the file. The first act of a file is a positive integer n,n<=80000 that represents the total number of pets and adopters who have come to the adoption agency during the year. The next n lines, in chronological order of arrival, describe the circumstances of the year when the pets and adopters came to the adoption. Each line has two positive integers a, b, of which a=0 represents a pet, a=1 represents an adopter, B denotes a pet's characteristic value, or a trait that the adopter wishes to adopt. (at the same time in the adoption, either all the pets, or all the adoptive, the number of pets and adopters will not exceed 10,000) "Input sample"
"Output format"There is only a positive integer in the output file that represents the total amount of dissatisfaction of all adopted pet adopters in the year after MoD 1000000 results. "Output Example"
(ABS (3-2) + ABS (2-4) = 3, the last adopter has no pets to adopt) Splay's topic is set.
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <Set>6 using namespacestd;7 Set<int>Q;8 intMain () {9Freopen ("pet.in","R", stdin);TenFreopen ("Pet.out","W", stdout); One intn,a,p,ans=0, flag=-1; Ascanf"%d",&n); - for(intI=1; i<=n;i++){ -scanf"%d%d",&p,&a); the if(flag==-1){ - Q.insert (a); -flag=p; - } + Else if(flag==p) - Q.insert (a); + A Else{ at Set<int>:: iterator l,r,tmp; -R=Q.lower_bound (a); - if(r==Q.end ()) { -r--; -ans= (ans+a-*r)%1000000; - Q.erase (r); in } - Else if(r==Q.begin ()) { toans= (ans+*r-a)%1000000; + Q.erase (r); - } the Else{ *tmp=r;l=--tmp; $ if(*r-a<a-*l) {Panax Notoginsengans= (ans+*r-a)%1000000; - Q.erase (r); the } + Else{ Aans= (ans+a-*l)%1000000; the q.erase (l); + } - } $ if(q.size () = =0) $flag=-1; - } - the } -printf"%d\n", ans);Wuyi}
Data structure (SET): COGS 62. [HNOI2004] Pet Adoption