noi2004--depressed Teller (SBT)

Source: Internet
Author: User
Tags min printf

depressed Teller (Cashier.pas)

"Problem description"

Oier Company is a large specialized software company, with tens of thousands of employees. As a teller, one of my tasks is to count the wages of each employee. It was supposed to be a good job, but the depressing part is that our bosses are fickle and often adjust their employees ' 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 is possible to deduct their wages by a similar amount. I really don't know what else he's going to do in addition to the salary adjustment.

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 stipulated wages, he would immediately angrily leave the company, and will never come back. The lower bound of wages for each employee is uniformly defined. Whenever a person leaves the company, I have to delete his payroll file from the computer, as well, whenever the company hires a new employee, I have to create a new payroll file for him.

The boss often came to my side to inquire about the salary, he did not ask the specific employee's salary, but asked how much the salary of the employees of the K-m pay. At this point, I had to make a lengthy sort of tens of thousands of employees and tell him the answer.

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

"Input File"

The first line has two non-negative integers n and min. n indicates how many commands are below and min represents the lower bound of wages.

Next n rows, each line represents a command. The command can be one of the following four:

Name

Format

Role

I command

I_k

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

a command

A_k

Add the wages of each employee to K

s command

S_k

Deduct the wages of each employee K

F command

F_k

Enquire about K-pay

_ (underscore) denotes a space, the I command, the A command, the K in the S command is a nonnegative integer, and the k in the F command is a positive integer.

At the beginning, it can be assumed 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 of the F command plus one.

For each f command, your program will output a line that contains only an integer, the number of wages for employees with a current salary of more than K, 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 leave 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

Convention

L I command no more than 100000 bars

L a command and S command have no more than 100 total number of bars

L F Order number not exceeding 100000

L The adjustment amount of each salary adjustment does not exceed 1000

• The salary of the new employee does not exceed 100000

"Scoring Method"

For each test point, if the number of rows in your output file is incorrect, or if the output file contains illegal characters, the score is 0.

Otherwise your score is calculated as follows: If you output the correct answer for all F commands, and the final output is correct for the number of people leaving the company, you will get 10 points; If you only output the correct answer for all f-commands, get 6 points; If the number of people leaving the company is correct, 4 points ; otherwise it will be 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 understanding is not very thorough, but the general idea still mastered ...

Two learning SBT addresses are recommended here:

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

Code (including nine of SBT operations): 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;
    }} 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.