Bzoj1208: [hnoi2004] pet adoption Institute

Source: Internet
Author: User
1208: [hnoi2004] pet adoption time limit: 10 sec memory limit: 162 MB
Submit: 4278 solved: 1624
[Submit] [Status] Description

Recently, Q opened a pet adoption Institute. Adoption provides two services: Adoption of pets abandoned by the owner and adoption of the pets by the new owner. Each adopter wants to adopt a pet that he or she is satisfied with. According to the requirements of the adopter, a q uses a special formula he has invented, the characteristic value a (A is a positive integer, A <2 ^ 31) of the pet to be adopted by the adopter is obtained, and he also gives each pet a characteristic value. In this way, he can easily handle the whole process of pet adoption. There are always two situations for pet adoption: too many abandoned pets or too many people who want to adopt pets, there are too few pets. 1. if there are too many abandoned pets, if a adopter arrives, the pet that the adopter wants to adopt features, then it will adopt a pet with the feature value closest to a that is not currently adopted. (The characteristics of any two pets cannot be the same, and the characteristics of any two pets are not the same.) If there are two pets that meet the requirements, that is to say, there are two pets whose feature values are A-B and A + B respectively, then the adopter will adopt the pet whose feature value is a-B. 2. There are too many people who adopt pets. If a adopted pet arrives, which of the following can be adopted? The adopter who can adopt the PET is the adopter who wants the feature value of the pet closest to the feature value of the pet. If the feature value of the pet is, there are two owners who want to adopt a pet with the characteristics of A-B and A + B respectively, then the recipient with the characteristics of a-B will successfully adopt the pet. A adopter has adopted a pet with a feature value of A, and the feature value of the pet that it wants to adopt is B, then the dissatisfaction of the adopter is ABS (a-B ). [Task description] after a year, you will be able to calculate the total dissatisfaction of all adopted pets. At the beginning of the year, there were no pets or breeders in the adoption site.

Input

The first behavior is a positive integer n, n <= 80000, indicating the total number of pets and owners who come to the adoption site during the year. The next n rows describe the pets and owners who came to the adoption site in the order of the arrival time. Each row has two positive integers, A and B. A = 0 indicates the pet, a = 1 indicates the pet, B indicates the characteristics of the pet, or the characteristics of the pet to be adopted. (If you stay in the adoption center at the same time, either it's all pets, or it's all owners. The number of pets and owners will not exceed 10000)

Output

There is only one positive integer, indicating the total dissatisfaction of all pets adopted in a year after mod 1000000.

Sample input5
0 2
0 4
1 3
1 2
1 5
Sample output3
(ABS (3-2) + ABS (2-4) = 3, the last adopter can adopt without pets)
Hint Source

Splay

Problem: I wrote a treap, and now I have switched to C ++. I will use the set incoming water code:
 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<set> 6 #define maxn 80000 7 #define inf 1000000000 8 #define mod 1000000 9 using namespace std;10 inline int read()11 {12     int x=0,f=1;char ch=getchar();13     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}14     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}15     return x*f;16 }17 set<int>a;18 int main()19 {20     freopen("input.txt","r",stdin);21     freopen("output.txt","w",stdout);22     int n,ans=0,flag;23     a.insert(inf);a.insert(-inf);24     n=read();25     for(int i=1;i<=n;i++)26     {27         int x,y;28         x=read();y=read();29         if(a.size()==2)30         {31             flag=x;a.insert(y);32         }33         else if(x!=flag)34         {35             int pre=*--a.lower_bound(y);36             int suc=*a.lower_bound(y);37             if(y-pre<=suc-y&&pre!=-inf)38             {39                 ans+=y-pre;a.erase(pre);40             }else41             {42                 ans+=suc-y;a.erase(suc);43             }44             ans%=mod;45         } 46         else a.insert(y);47     } 48     printf("%d\n",ans);49 }
View code

 

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.