51nod1785-好题&思维&模拟窗口-数据流算法
来源:互联网 发布:抽奖系统数据库设计 编辑:程序博客网 时间:2024/06/17 04:58
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1785
按照题意模拟即可。
开始用vector模拟,用O(n)的erase函数来模拟删除操作。妥妥tle。
用各种求 中位数的方法也不行。动态维护和也不行。
后来用deque模拟,发现deque比vector好用多了。但是还是t最后一组数据
用输入挂也不行。。。
用数组模拟数据流。其实窗口的前进,我们只需要让新数和最前面的数替换就行。。 用输入挂,即使排序查找中位数也能卡过。
收获:求中位数的方法:① 排序
② 用桶排的方法。还是值得学习一下的。
stdio.h 要比cstdio快,100ms。。。。
#include <stdio.h>#include <algorithm>#include <bits/stdc++.h>using namespace std;/*这个用deque模拟也是超时的。用vector带erase,那更是超时的没边了中位数用sort的来写就行。*/template <class T>bool scan_d(T &ret){ char c; int sgn; T bit = 0.1; if (c=getchar(), c==EOF) { return 0; } while ((c!= '-') &&(c!= '.') && (c < '0' || c > '9')) { c = getchar(); } sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'); } if (c == ' ' || c == '\n') { ret *= sgn; return 1; } while (c = getchar(), c >= '0' && c <= '9') { ret += (c - '0') * bit, bit /= 10; } ret *= sgn; return 1;}const int maxn=200;int a[maxn];int b[maxn];int mp[maxn];int m,n,ch,num;int main(){ while(~scanf("%d%d",&m,&n)){ int loc=0; int sum=0; for(int i=0;i<maxn;i++) mp[i]=0,a[i]=-1; for(int i=0;i<m;i++){ scan_d(ch); if(ch==1){ scan_d(num); mp[num]++; if(mp[a[loc%n]]>0) mp[a[loc%n]]--; if(a[loc%n]==-1) a[loc%n]=0; sum+=(num-a[loc%n]); a[loc%n]=num; loc++; } else if(ch==2){ printf("%d.00\n",sum/min(loc,n)); } else if(ch==3){ double aaa=(1.0*sum)/min(loc,n); double anss=0; for(int i=0;i<min(loc,n);i++){ anss+=1.0*(a[i]-aaa)*(a[i]-aaa); } printf("%.2f\n",anss/(1.0*min(loc,n))); } else if(ch==4){ int now=min(loc,n); if(now%2==1) { //printf("99"); int pos=now/2+1,ans=0; for(int i=0;i<=100;i++) { ans+=mp[i]; //if(mp[i]<0) //printf("!!"); if(ans>=pos) { printf("%.2f\n",(double)i); break; } } } else { int pos1=now/2,pos2=now/2+1,st=-1,ed=-1,ans=0; for(int i=0;i<=100;i++) { ans+=mp[i]; if(ans>=pos1&&st==-1)st=i; if(ans>=pos2&&ed==-1)ed=i; if(st!=-1&&ed!=-1) { printf("%.2lf\n",(double)(st+ed)/2); break; } } } } } } return 0;}
#include <stdio.h>#include <algorithm>using namespace std;/*这个用deque模拟也是超时的。用vector带erase,那更是超时的没边了中位数用sort的来写就行。*/template <class T>bool scan_d(T &ret){ char c; int sgn; T bit = 0.1; if (c=getchar(), c==EOF) { return 0; } while ((c!= '-') &&(c!= '.') && (c < '0' || c > '9')) { c = getchar(); } sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'); } if (c == ' ' || c == '\n') { ret *= sgn; return 1; } while (c = getchar(), c >= '0' && c <= '9') { ret += (c - '0') * bit, bit /= 10; } ret *= sgn; return 1;}const int maxn=200;int a[maxn];int b[maxn];int m,n,ch,num;int main(){ while(~scanf("%d%d",&m,&n)){ int loc=0; int sum=0; for(int i=0;i<m;i++){ scan_d(ch); if(ch==1){ scan_d(num); sum+=(num-a[loc%n]); a[loc%n]=num; loc++; } else if(ch==2){ printf("%d.00\n",sum/min(loc,n)); } else if(ch==3){ double aaa=(1.0*sum)/min(loc,n); double anss=0; for(int i=0;i<min(loc,n);i++){ anss+=1.0*(a[i]-aaa)*(a[i]-aaa); } //cout<<anss<<"**"<<min(loc,n)<<endl; printf("%.2f\n",anss/(1.0*min(loc,n))); } else if(ch==4){ int len=min(loc,n); for(int i=0;i<len;i++){ b[i]=a[i]; //cout<<a[i]<<" "; } sort(b,b+len); if(len%2==0) printf("%.2f\n",1.0*(1.0*b[len/2]+1.0*b[len/2-1])/2); else printf("%.2f\n",1.0*b[len/2]); } } } return 0;}
阅读全文
0 0
- 51nod1785-好题&思维&模拟窗口-数据流算法
- 51NOD1785 数据流中的算法 【水】
- HDU5961-搜索|模拟&思维&好题-A
- 1785 数据流中的算法(模拟,乱搞)
- 1785 数据流的算法(模拟)
- Hrbust 1390 Leyni, LOLI and Numbers【思维+树状数组+二分+双向链表模拟】好题!好题!好题!
- Codeforces 492D Vanya and Computer Game【模拟+循环节+思维】好题~
- 【NOIP模拟赛】 思维+离线+递推 数列(好题)
- 【NOIP2017模拟赛】思维+转化+图论 徒然Children(好题)
- 51Nod 数据流中的算法
- [51nod1786]数据流中的算法
- 51Nod-数据流算法-1785
- 51nod 1020 逆序排列【Dp+思维递推优化】好题!好题!好题!
- 思维题(好题)
- poj1017 贪心 思维 好题
- 数据流算法
- 51nod1770-模拟&好题-数数字
- 51nod 1391 01串(锻炼思维的好题)
- Spring+ Spring cloud + SSO单点登录应用认证
- Python学习---9
- BackBox错误,无法获得锁...资源暂时不可用...无法锁定管理目录
- centos上编译caffe(二)
- 文件的上传和下载
- 51nod1785-好题&思维&模拟窗口-数据流算法
- 关于ajax从spring mvc后台取值 返回值乱码问题
- Effective C++ 第三章 资源管理 理解&总结
- easyui 说明(一)
- 进程控制模拟程序
- Activity跳转的过渡动画全解析
- EventBus的两个Activity的传值
- 初识压缩感知Compressive Sensing
- 洛谷 P3379 【模板】最近公共祖先(LCA)