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. Many companies now employ a single coder to write an algorithm that will replace many 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 would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should 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 should 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 should be understood by
where the set S is written as {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3 < ... < a k
Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should 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 should 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 should be understood by
where the set S is written as {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3 < ... < a k
Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm
In each test case, the first line contains one integer N ( 1 <= N <= 10 5 ), 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 <= 10 9.
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).
9 add 1 add 2 add 3 add 4 add 5 sum add 6 del 3 sum 6 add 1 add 3 add 5 add 7 add 9 sum
3
4
5
C++ maybe run faster than G++ in this problem.
题解:
题意:
给n种操作,add往集合里面加数,del往集合里面删数,sum求出排好序后的下标%5=3的数值和
刚刚看了这题的线段树的题解。。。好复杂啊有点难懂,留着以后写就更新博客吧,看ACM荣耀之路博主的博客有一种暴力写法也可以过,当然纯暴力肯定过不了,要一些技巧,他的代码是8s8过的,题目要求10s以内,我将他的代码在查找删除位置的时候改进成二分了,7s5过的,详解看代码
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
int a[100005];
int find(int l,int r,int x)//二分查找x在数组中的位置
{
int mid;
while(l<r)
{
mid=(l+r)/2;
if(a[mid]>x)
r=mid;
else if(a[mid]<x)
l=mid;
else
return mid;
}
return mid;
}
int main()
{
char s[10];
int i,j,k,n,x,ans,tot,t,pos;
long long p;
while(scanf("%d",&n)!=EOF)
{
ans=0;
for(i=0;i<n;i++)
{
scanf("%s",s);
if(strcmp(s,"add")==0)
{
scanf("%d",&x);
for(j=ans++;j>0;j--)//精华,有点类似插入排序,插入的时候保持数组的有序性
{
if(a[j-1]>x)
a[j]=a[j-1];
else
break;
}
a[j]=x;
}
else if(strcmp(s,"del")==0)
{
scanf("%d",&x);
pos=find(0,ans,x);//二分查出删除的位置
for(j=pos;j<ans-1;j++)//覆盖
{
a[j]=a[j+1];
}
ans--;
}
else
{
p=0;
for(j=2;j<ans;j+=5)//因为下标是从0开始的,所以起点为2,每次加5就相当于%5=3了
p+=a[j];
printf("%I64d\n",p);
}
}
}
return 0;
}