HDU 4288 Coder(STL水过)

D. Sum of Medians
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array.

sum of medians of a sorted k-element set S = {a1, a2, ..., ak}, where a1 < a2 < a3 < ... < ak, will be understood by as

The  operator stands for taking the remainder, that is  stands for the remainder of dividing x by y.

To organize exercise testing quickly calculating the sum of medians for a changing set was needed.

Input

The first line contains number n (1 ≤ n ≤ 105), the number of operations performed.

Then each of n lines contains the description of one of the three operations:

  • add x — add the element x to the set;
  • del x — delete the element x from the set;
  • sum — find the sum of medians of the set.

For any add x operation it is true that the element x is not included in the set directly before the operation.

For any del x operation it is true that the element x is included in the set directly before the operation.

All the numbers in the input are positive integers, not exceeding 109.

Output

For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).

Sample test(s)
input
6
add 4
add 5
add 1
add 2
add 3
sum
output
3
input
14
add 1
add 7
add 2
add 5
sum
add 6
add 8
add 9
add 3
add 4
add 10
sum
del 1
sum
output
5
11
13

 

 

题目大意:

  给你n个操作,操作的类型有 add, del,sum。 add x 就是说,给这个容器中加入一个x,del x 就是说把这个容器中的x删除掉, sum x 就是求所有下标能够%5==3的数字的和。

看了下n的范围,n<1e5,然后果断nlogn卡过去。

 

解题思路:

  这道题的解题思路很容易理解,其实就是把所有新加入的数字扔进vector里面,然后求的时候,只要每次lower_bound(v.begin(),v.end(),tmp)就行了,起初,要定义一个vector<int>::iterator it

的迭代器,每次找到大于等于这个值的位置,然后v.insert(it,tmp)进去,如果是删除操作的话,就直接v.erase(it)就行了。%5==3的数字的可以枚举出来,因为n<=1e5,在3s内是可以接受的。

 

代码:

# include<cstdio>
# include<iostream>
# include<string>
# include<algorithm>
# include<vector>

using namespace std;

vector<int>v;

int main(void)
{

    int n;
    while ( cin>>n )
    {
        string str;
        vector<int>::iterator it;
        while ( n-- )
        {
            int tmp;
            cin>>str;
            if ( str[0]=='a' )
            {
                cin>>tmp;
                it = lower_bound(v.begin(),v.end(),tmp);
                v.insert(it,tmp);
            }
            else if ( str[0]=='d' )
            {
                cin>>tmp;
                it = lower_bound(v.begin(),v.end(),tmp);
                v.erase(it);
            }
            else
            {
                long long ans = 0;
                for ( int i = 2;i < v.size();i+=5 )
                {
                    ans+=v[i];
                }
                cout<<ans<<endl;
            }
        }
        v.clear();

    }
    return 0;
}

  

转载于:https://www.cnblogs.com/wikioibai/p/4755912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值