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;}