# Data structure (SET): COGS 62. [HNOI2004] Pet Adoption

Source: Internet
Author: User

★ ¡ï 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.

 50 20 41 31 21 5

"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"

 3

(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

Related Keywords:

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.