zoukankan      html  css  js  c++  java
  • Median(vector+二分)

    Median


    Time Limit: 5 Seconds Memory Limit: 65536 KB

    The median of m numbers is after sorting them in order, the middle one number of them if m is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at first. Then you can add or remove some number from the list.

    For each add or remove operation, output the median of the number in the list please.

    Input

    This problem has several test cases. The first line of the input is an integer T (0<T<=100) indicates the number of test cases. The first line of each test case is an integer n (0<n<=10000) indicates the number of operations. Each of the next n lines is either "add x" or "remove x"(-231<=x<231) indicates the operation.

    Output

    For each operation of one test case: If the operation is add output the median after adding x in a single line. If the operation is remove and the number x is not in the list, output "Wrong!" in a single line. If the operation is remove and the number x is in the list, output the median after deleting x in a single line, however the list is empty output "Empty!".

    Sample Input

    2
    7
    remove 1
    add 1
    add 2
    add 1
    remove 1
    remove 2
    remove 1
    3
    add -2
    remove -2
    add -1

    Sample Output

    Wrong!
    1
    1.5
    1
    1.5
    1
    Empty!
    -2
    Empty!
    -1

    Hint

    if the result is an integer DO NOT output decimal point. And if the result is a double number , DO NOT output trailing 0s. 

    题意:找中位数,由于数据比较大,用到了vector容器,

    我也不会用,比赛前看过一点,可是不知道怎么用,so。。。比赛完,借大神的代码来观摩下,哈哈。我发现这个真的很好用。我一定要把他学会!!!

    ps:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4736

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    using namespace std;
    vector<int>vet;
    int deal(int x)
    {
        int left=0,right=vet.size()-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(vet[mid]==x)
            {
                return mid;
            }
            else if(vet[mid]>x)
            {
                right=mid-1;
            }
            else
            {
                left=mid+1;
            }
        }
        return -1;
    }
    int erfen(int x)
    {
        if(vet.size()==0) return 0;
        if(x<vet[0]) return 0;
        if(x>vet[vet.size()-1]) return vet.size();
        int left=0,right=vet.size()-1,k=-1;
        while(left<=right)
        {
            int mid=(left+right)/2;
            if(vet[mid]>=x&&vet[mid-1]<=x)
            {
                return mid;
            }
            else if(vet[mid]<=x&&vet[mid+1]>=x)
            {
                return mid+1;
            }
            else if(vet[mid]>x)
            {
                right=mid-1;
            }
            else left=mid+1;
        }
    }
    int main()
    {
        int text;
        scanf("%d",&text);
        while(text--)
        {
            int n;
            scanf("%d",&n);
            vet.clear();
            vector<int>::iterator it=vet.begin();
            while(n--)
            {
    
                char ch[30];
                int x;
                scanf("%s%d",ch,&x);
                if(ch[0]=='r')
                {
                    if(vet.size()==0)
                    {
                        printf("Wrong!
    ");
                        continue;
                    }
                    else
                    {
                        int tmp=deal(x);
                        if(tmp==-1)
                        {
                            printf("Wrong!
    ");
                            continue;
                        }
                        else
                        {
                            it=vet.begin();
                            vet.erase((it+tmp));
                            int len=vet.size();
                            if(len==0)
                            {
                                printf("Empty!
    ");
                                continue;
                            }
                            else if(len%2==1)
                                cout<<vet[len/2]<<endl;
                            else
                            {
                                long long ans=(long long)vet[len/2]+(long long)vet[len/2-1];
                                if(ans%2==0)
                                    printf("%lld
    ",ans/2);
                                else
                                {
                                    double sum=ans/2.0;
                                    printf("%.1lf
    ",sum);
                                }
                            }
    
                        }
                    }
                }
                else
                {
                    int tmp=erfen(x);
                    it=vet.begin();
                    vet.insert((it+tmp),x);
                    int len=vet.size();
                    if(len%2==1)
                        printf("%d
    ",vet[len/2]);
                    else
                    {
                        long long ans=(long long)vet[len/2]+(long long)vet[len/2-1];
                        if(ans%2==0)
                            printf("%lld
    ",ans/2);
                        else
                        {
                            double sum=ans/2.0;
                            printf("%.1lf
    ",sum);
                        }
                    }
                }
            }
        }
        return 0;
    }

    加油!!!加油!!!

  • 相关阅读:
    Javascript学习总结
    MVC和MVVM
    各大搜索引擎网址收录入口地址
    微软称IE9将更加出色 对手谷歌也能从中受益(图文)
    公安部:身份证丢失无需挂失 被冒用不担责 冒用身份证犯罪
    微软发布Mac 8版Messenger 支持视频会议(图)
    如何删除XP系统的NETWARE,改变登陆界面 火急!!XP系统登录界面由于netware造成的不能更改
    使用WebBrowser自动登录阿里妈妈网站
    微软发布IE9开发者预览版 不支持XP系统(图)
    ASP 0113 的错误的终极解决办法(三种)
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/3956292.html
Copyright © 2011-2022 走看看