HDU 4288-coder (simulation)

Source: Internet
Author: User
Coder Time Limit: 20000/10000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3327 accepted submission (s): 1316


Problem description in mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. the term has become increasing popular since the advent of cheap and reliable computers. compile companies now employ a single coder to write an algorithm that will replace into other employees. an added benefit to the employer is that the coder will also become redundant once their work is done. 1
You are now the signle coder, and have been assigned a new task writing code, since your boss wocould like to replace into other employees (and you when you become redundant once your task is complete ).
Your code shocould be able to complete a task to replace these employees who do nothing all day but eating: Make the digest sum.
By saying "digest sum" we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code shoshould give response to following operations:
1. Add X-Add the element X to the Set;
2. del X-remove the element x from the set;
3. Sum-find the digest sum of the set. The digest sum shocould be understood

Where the set S is written as {A1, A2,..., AK} satisfying A1 <A2 <A3 <... <AK
Can you complete this task (and be then fired )?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
Input there're several test cases.
In each test case, the first line contains one integer N (1 <=n <= 105), the number of operations to process.
Then following is n lines, each one containing one of three operations: "add X" or "del X" or "sum ".
You may assume that 1 <= x <= 109.
Please see the sample for detailed format.
For any "Add X" it is guaranteed that X is not currently in the set just before this operation.
For any "del X" it is guaranteed that X must currently be in the set just before this operation.
Please process until EOF (end of file ).
 
Output for each operation "sum" Please Print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.
 
Sample Input
9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum
 
Sample output
34. Add X to a set (strictly ascending with no duplicates): Add X to the set, and delete X from the set by del X, sum is the sum of the elements whose subscript % 5 = 3. Use arrays to simulate collections. Ensure the array order during insertion and deletion, and then use sum to solve the problem of 6000 + MS ..
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>#define ll long longusing namespace std;const int INF=1<<27;const int maxn=100010;int a[maxn];int Binary_search(int low,int high,int x){int mid;while(low<=high){mid=(low+high)/2;if(a[mid]==x)return mid;else if(a[mid]>x)high=mid-1;else if(a[mid]<x)low=mid+1;}return -1;}int main(){int n,x,i;char op[5];while(scanf("%d",&n)!=EOF){int len=0;while(n--){scanf("%s",op);if(op[0]=='a'){scanf("%d",&x);for(i=len++;i>0;i--)if(a[i-1]>x)a[i]=a[i-1];elsebreak;a[i]=x;}else if(op[0]=='d'){scanf("%d",&x);int indx=Binary_search(0,len-1,x);while(indx<len-1){a[indx]=a[indx+1];indx++;}len--;}else{__int64 sum=0;for(i=2;i<len;i+=5)sum+=a[i];printf("%I64d\n",sum);}}}    return 0;}

HDU 4288-coder (simulation)

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.