• CodeForces


    先上题目:

    C. Cows and Sequence
    time limit per test
    3 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 integersai, 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.

    Sample test(s)
    input
    5
    2 1
    3
    2 3
    2 1
    3
    output
    0.500000
    0.000000
    1.500000
    1.333333
    1.500000
    input
    6
    2 1
    1 2 20
    2 2
    1 2 -3
    3
    3
    output
    0.500000
    20.500000
    14.333333
    12.333333
    17.500000
    17.000000
    Note

    In the second sample, the sequence becomes 

      题意:给你三种操作,给前ai个数都加上一个数;在序列末尾加一个数,去除序列末尾的数,每种操作以后输出当前序列的平均值,一开始序列有一个0,保证操作合法,序列里至少有一个数。

      比赛的时候想的太复杂了。想了一种更广泛的操作,可以在任意区间加数的。然后就想到了盛爷讲的树状数组的区间修改,单点查询什么的,然后一直调代码,结果白白浪费了2个小时,结果还是错了。这里有的特出条件就是加数的区间的其中一端是固定在开头,所以其实只需要用一个变量保存总值,然后对于区间添加的时候就直接标记一下是到哪里的区间,在删除的时候就将标记前移就可以了。

      又打了一场脑子进水的比赛······

    上代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <utility>
     5 #define MAX 300200
     6 #define ll long long
     7 #define lowbit(x) (x&(-x))
     8 using namespace std;
     9 
    10 ll d[MAX];
    11 ll ma[MAX];
    12 
    13 int n;
    14 
    15 int main()
    16 {
    17     int ti,m,a,x;
    18     ll p,ss;
    19     double avg;
    20     //freopen("data.txt","r",stdin);
    21     while(scanf("%d",&m)!=EOF){
    22 //        memset(d,0,sizeof(ll)*(lim+1));
    23 //        memset(ma,0,sizeof(ll)*(lim+1));
    24         n=1;
    25         ss=0;
    26         d[1]=0;
    27         for(int i=0;i<m;i++){
    28             scanf("%d",&ti);
    29             switch(ti){
    30                 case 1:
    31                     scanf("%d %d",&a,&x);
    32                     ma[a]+=x;
    33                     ss+=a*x;
    34                     break;
    35                 case 2:
    36                     scanf("%d",&x);
    37                     ss+=x;
    38                     n++;
    39                     d[n]=x; ma[n]=0;
    40                     break;
    41                 case 3:
    42                     ss-=d[n]+ma[n];
    43                     ma[n-1]+=ma[n];
    44                     n--;
    45                     break;
    46             }
    47             avg=ss*1.0/n;
    48             printf("%.6lf
    ",avg);
    49         }
    50         //putchar('
    ');
    51     }
    52     return 0;
    53 }
    /*Cows and Sequence*/
  • 相关阅读:
    LeetCode-Read N Characters Given Read4 II
    LeetCode-One Edit Distance
    LeetCode-Palindrome Permutation II
    LeetCode- Longest Absolute File Path
    LeetCode-Strobogrammatic Number II
    LeetCode-Strobogrammatic Number
    LeetCode-Flatten 2D Vector
    LeetCode-Shortest Word Distance III
    LeetCode-Shortest Word Distance II
    Cookie/Session
  • 原文地址:https://www.cnblogs.com/sineatos/p/3905548.html
Copyright © 2020-2023  润新知