Codeforces 284C Cows and Sequence【思维】

来源:互联网 发布:w10动态壁纸软件 编辑:程序博客网 时间:2024/05/21 22:56

C. Cows and Sequence
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following:

  1. Add the integer xi to the first ai elements of the sequence.
  2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1)
  3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence.

After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi(|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything.

It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.

Output

Output n lines each containing the average of the numbers in the sequence after the corresponding operation.

The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

Examples
input
52 132 32 13
output
0.5000000.0000001.5000001.3333331.500000
input
62 11 2 202 21 2 -333
output
0.50000020.50000014.33333312.33333317.50000017.000000
Note

In the second sample, the sequence becomes 


题目大意:

有三种操作:

①1 x val 表示将队列中前x个人的价值加上val;

②2 x表示在队列末尾加入一个人,其价值为x;

③3 x表示删除队列末尾的那个人;

问每次操作结束后,队列的平均值。


思路:


①对于整个问题来讲,其加入元素和删除元素都是在队伍末尾进行的,那么整体我维护一个栈就行。


②对于操作2,我们直接执行操作,在栈顶加入一个值为x的元素即可。


③对于操作1,我们维护一个数组sum【i】表示我们在位子i之前每个元素都要加上sum【i】的价值,那么对于一个操作1 x,val,我们直接sum【x】+=val即可。


④对于操作3,我们判断栈顶元素:u=s.top(),那么删除的那个数应该是u+sum【栈的大小】;然后我们将栈顶弹出,并且使得sum【栈的大小-1】+=sum【栈的大小】并且将sum【栈的大小】置为0.使得累加的部分前推。


过程维护一下元素的总和以及栈的size即可。不难,注意细节和数据范围。


Ac代码:

#include<stdio.h>#include<string.h>#include<stack>using namespace std;#define ll __int64ll sum[250000];int main(){    ll n;    while(~scanf("%I64d",&n))    {        memset(sum,0,sizeof(sum));        ll tot=0;        stack<ll>s;s.push(0);        for(ll i=0;i<n;i++)        {            ll op;scanf("%I64d",&op);            if(op==1)            {                ll x;scanf("%I64d",&x);                ll val;scanf("%I64d",&val);                sum[x]+=val;                tot+=x*val;            }            if(op==2)            {                ll x;scanf("%I64d",&x);                s.push(x);                tot+=x;            }            if(op==3)            {                ll u=s.top();                tot-=u+sum[s.size()];                sum[s.size()-1]+=sum[s.size()];                sum[s.size()]=0;                s.pop();            }            double fenzi=tot;            double fenmu=s.size();            printf("%.6f\n",fenzi/fenmu);        }    }}











原创粉丝点击