noi2004--depressed Teller (SBT)

Source: Internet
Author: User
Tags min printf first row

the depressing teller (Cashier.pas)

"Problem description"

Oier Company is a large-scale specialized software company, has tens of thousands of employees. As a cashier, one of my tasks is to count the salaries of each employee. It would have been a good job, but it was frustrating that our bosses were fickle and often adjusted their salaries. If he is in a good mood, he may add the same amount to each employee's salary. Conversely, if the mood is not good, it may be their wages deducted a same amount. I really don't know what else he's doing except for the pay raise.

The frequent adjustment of wages is very annoying to employees, especially when the collective deduction of wages, once an employee found that his salary is lower than the contract under the lower wages, he will immediately angrily leave the company, and never come back. The lower salary of each employee is uniformly stipulated. Every time a person leaves the company, I will delete his payroll file from the computer, again, whenever the company hires a new employee, I have to create a new payroll file for him.

The boss often come to my side to ask about the salary, he does not ask a specific employee's salary, but asked how many employees now pay K how much wages. At this point, I had to do a lengthy sort of tens of thousands of employees and tell him the answer.

Well, now you've learned a lot about my job. As you guessed, I would like to ask you to compile a payroll statistics program. It's not very difficult, is it?

"Input File"

The first row has two nonnegative integers n and Min. n indicates how many commands are below, min indicates the lower salary.

The next n lines, each line represents a command. The command can be one of the following four types:

Name

Format

Role

I command

I_k

Create a new payroll file with the initial salary of K. If an employee's initial salary is below the lower wage, he will leave the company immediately.

a command

A_k

Add k to each employee's salary

s command

S_k

Deduct every employee's salary from K

F command

F_k

Inquire about the wages of K-much

_ (underline) denotes a space, the i command, a command, the K in the S command is a nonnegative integer, and K in the F command is a positive integer.

At the beginning, you can assume that there is not a single employee in the company.

"Output File"

The number of rows in the output file is the number of bars in the F command plus one.

For each f command, your program outputs a row, contains only one integer, the number of salaries for employees who are currently paid a K-number, and if K is greater than the current number of employees, output-1.

The last line of the output file contains an integer that is the total number of employees who have left the company.

"Sample Input" (cashier.in)

9 10

I 60

I 70

S 50

F 2

I 30

S 15

A 5

F 1

F 2

"Sample Output" (Cashier.out)

10

20

-1

2

Agreed

L command the number of bars not exceeding 100000

L a command and the total number of S commands not exceeding 100

L command the number of bars not exceeding 100000

L adjustment for each wage adjustment does not exceed 1000

L The salary of the new employee does not exceed 100000

"Scoring Method"

For each test point, if you output a file with an incorrect number of rows, or if the output file contains an illegal character, the score is 0.

Otherwise your score is calculated as follows: If for all f commands, you output the correct answer, and the final number of people leaving the company is correct, you will get 10 points; If you only have the correct answer for all F commands, get 6 points; If the number of people leaving the company is correct, 4 points , or 0 points.


Analytical:

This is a data structure problem, it is said that splay,avl,sbt,treap, line tree, tree-like array can be implemented

These two days just learned SBT, although some of the code understand is not very thorough, but the general idea or mastered ...

Two learning SBT addresses are recommended here:

Explanation: Http://www.nocow.cn/index.php/Size_Balanced_Tree

Code (including the nine operations of SBT): http://www.docin.com/p-481614045.html

Code:

#include <cstdio> #include <algorithm> using namespace std;
const int n=100010;
int Root,tot;
    struct SBT {int left,right,size,key;
        void Init () {left=right=key=0;
    size=1;

}}tree[n];
    void Go_left (int &p) {int k=tree[p].right;
    Tree[p].right=tree[k].left;
    tree[k].left=p;
    Tree[k].size=tree[p].size;
    tree[p].size=tree[tree[p].left].size+tree[tree[p].right].size+1;
P=k;
    } void Go_right (int &p) {int k=tree[p].left;
    Tree[p].left=tree[k].right;
    tree[k].right=p;
    Tree[k].size=tree[p].size;
    tree[p].size=tree[tree[p].left].size+tree[tree[p].right].size+1;
P=k; } void maintain (int &p,bool flag) {if (flag) {if (tree[tree[tree[p].right].right].size>tree[tree[p
        ].left].size) Go_left (p);
            else if (tree[tree[tree[p].right].left].size>tree[tree[p].left].size) {go_right (tree[p].right);
        Go_left (P);
    else return; } elsE//Otherwise update the left subtree {if (tree[tree[tree[p].left].left].size>tree[tree[p].right].size) go_right (p); else if (tree[tree[tree[p].left].right].size>tree[tree[p].right].size) {Go_left (Tree[p].lef
            T);
        Go_right (P);
    else return;
    } maintain (Tree[p].left,false);
    Maintain (tree[p].right,true);
    Maintain (P,FALSE);
Maintain (p,true);
        } void Insert (int &p,int k) {if (p==0) {p=++tot;
        Tree[p].init ();
    Tree[p].key=k;
        else {tree[p].size++;
        if (k<tree[p].key) insert (TREE[P].LEFT,K);
        else insert (TREE[P].RIGHT,K);
    Maintain (P,k>=tree[p].key);
    } void Delete (int &p,int delay,int min_val) {if (!p) return;
        if (tree[p].key+delay<min_val) {p=tree[p].right;
    Delete (P,delay,min_val);
        else {Delete (tree[p].left,delay,min_val); tree[p].size=tree[tree[p].left].size+tree[tree[p].right].size+1;
    an int get_max_kth (int &p,int k) {int t=tree[tree[p].right].size+1;
    if (t==k) return tree[p].key;
    if (t<k) return get_max_kth (TREE[P].LEFT,K-T);
else return get_max_kth (tree[p].right,k);
    } void ReadData () {freopen ("cashier.in", "R", stdin);
Freopen ("Cashier.out", "w", stdout);
    } void Work () {int n,min_val,delay;
        while (scanf ("%d%d", &n,&min_val)!=eof) {tot=delay=root=0;
            while (n--) {char op[10];
            int k;
            scanf ("%s%d", &op,&k);
                if (op[0]== ' I ') {if (k<min_val) continue;
            Insert (Root,k-delay);
            else if (op[0]== ' A ') delay+=k;
            else if (op[0]== ' F ') printf ("%d\n", Tree[root].size<k?-1: (get_max_kth (ROOT,K) +delay));
                else {delay-=k;
            Delete (Root,delay,min_val);
    }
        }    printf ("%d\n", tot-tree[root].size);
    int main () {readdata ()};
    Work ();
return 0;
 }


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.