CodeForces - 284C - Cows and Sequence
先上题目:
3 seconds
256 megabytes
standard input
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:
- Add the integer xi to the first ai elements of the sequence.
- Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1)
- 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!
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 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.
5
2 1
3
2 3
2 1
3
0.500000
0.000000
1.500000
1.333333
1.500000
6
2 1
1 2 20
2 2
1 2 -3
3
3
0.500000
20.500000
14.333333
12.333333
17.500000
17.000000
In the second sample, the sequence becomes 
题意:给你三种操作,给前ai个数都加上一个数;在序列末尾加一个数,去除序列末尾的数,每种操作以后输出当前序列的平均值,一开始序列有一个0,保证操作合法,序列里至少有一个数。
比赛的时候想的太复杂了。想了一种更广泛的操作,可以在任意区间加数的。然后就想到了盛爷讲的树状数组的区间修改,单点查询什么的,然后一直调代码,结果白白浪费了2个小时,结果还是错了。这里有的特出条件就是加数的区间的其中一端是固定在开头,所以其实只需要用一个变量保存总值,然后对于区间添加的时候就直接标记一下是到哪里的区间,在删除的时候就将标记前移就可以了。
又打了一场脑子进水的比赛······
上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#define MAX 300200
#define ll long long
#define lowbit(x) (x&(-x))
using namespace std; ll d[MAX];
ll ma[MAX]; int n; int main()
{
int ti,m,a,x;
ll p,ss;
double avg;
//freopen("data.txt","r",stdin);
while(scanf("%d",&m)!=EOF){
// memset(d,0,sizeof(ll)*(lim+1));
// memset(ma,0,sizeof(ll)*(lim+1));
n=;
ss=;
d[]=;
for(int i=;i<m;i++){
scanf("%d",&ti);
switch(ti){
case :
scanf("%d %d",&a,&x);
ma[a]+=x;
ss+=a*x;
break;
case :
scanf("%d",&x);
ss+=x;
n++;
d[n]=x; ma[n]=;
break;
case :
ss-=d[n]+ma[n];
ma[n-]+=ma[n];
n--;
break;
}
avg=ss*1.0/n;
printf("%.6lf\n",avg);
}
//putchar('\n');
}
return ;
}
/*Cows and Sequence*/
CodeForces - 284C - Cows and Sequence的更多相关文章
- codeforces C. Cows and Sequence 解题报告
题目链接:http://codeforces.com/problemset/problem/284/C 题目意思:给出3种操作:t = 1:在前 a 个数中每个数都加上x: t= 2:在数组末尾增加一 ...
- Codeforces 486E LIS of Sequence(线段树+LIS)
题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...
- codeforces 284 C. Cows and Sequence(线段树)
题目链接:http://codeforces.com/contest/284/problem/C 题意:就是给出3个操作 1)是将前i 个数加x 2)在数组最后添加一个数x 3)删除数组最后的那个数 ...
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces GYM 100114 C. Sequence 打表
C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...
- CodeForces 622 A.Infinite Sequence
A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces 675A A. Infinite Sequence(水题)
题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces 487C. Prefix Product Sequence 逆+结构体
意甲冠军: 对于数字n, 他询问是否有1~n置换 这种布置能够在产品上模每个前缀n 有可能0~n-1 解析: 通过观察1肯定要在首位,n一定要在最后 除4意外的合数都没有解 其它质数构造 a[i]=i ...
- Codeforces 670E - Correct Bracket Sequence Editor - [线段树]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
随机推荐
- JSP-Runoob:JSP 异常处理
ylbtech-JSP-Runoob:JSP 异常处理 1.返回顶部 1. JSP 异常处理 当编写JSP程序的时候,程序员可能会遗漏一些BUG,这些BUG可能会出现在程序的任何地方.JSP代码中通常 ...
- Spark 机器学习 ---Word2Vec
package Spark_MLlib import org.apache.spark.ml.feature.Word2Vec import org.apache.spark.sql.SparkSes ...
- eclipse----快速设置主题色
- 城市平乱 ---- Dijkstra
题解 : 以暴乱城市 为 源点 向所有点做最短路径 , 然后检查每个不对到暴乱城市的 最短距离 #include<stdio.h> #include<string.h> #in ...
- $tsinsenA1067$
\(problem\) 这种题目需要一个定理 \(a[1]+a[2]+a[3]+a[4]...=(a[1]%mod)+...\) 本人出奇的懒 然后 动态规划?(恰似枚举) #include < ...
- C#最实用的快捷键
Ctrl+J(Alt+→):智能提示. Ctrl+X:删除整行. Shift+Alt+Enter:全屏切换 F12:跳转到定义. Ctrl+-.Ctrl+Shift+-:上一步.下一步(仅限于使用过上 ...
- Web程序安全机制
ASP.NET提供了一个多层的安全模型,这个模型能够非常容易的保护Web应用程序. 安全策略没有必要非常复杂,但是需要应用安全策略的地方却是非常广泛的.程序员需要保证自己的应用程序不能被骗取,而把私有 ...
- 快速搭建Hadoop及HBase分布式环境
本文旨在快速搭建一套Hadoop及HBase的分布式环境,自己测试玩玩的话ok,如果真的要搭一套集群建议还是参考下ambari吧,目前正在摸索该项目中.下面先来看看怎么快速搭建一套分布式环境. 准备 ...
- [Android]异常1-duplicate files during packaging of
异常原因: 可能一>项目存在重复资源文件 可能二>Android Studio与源代码Android Studio不一致 解决方法有: 解决一>查找重复资源,删除或者修改文件 解决二 ...
- 微信自定义分享功能实现Tips
以MVC为例 前台js通过.post()方法传给后台特定Controller当前页面的url,后台获取后,进行处理: 1.获取access_token:https://mp.weixin.qq.com ...