PAT (Advanced Level) 1029. Median (25)

scanf读入居然会超时...用了一下输入挂才AC...

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=1000000+10;
int a[maxn],b[maxn];
int n1,n2;

inline bool scan_d(int &num)  
{
        char in;bool IsN=false;
        in=getchar();
        if(in==EOF) return false;
        while(in!='-'&&(in<'0'||in>'9')) in=getchar();
        if(in=='-'){ IsN=true;num=0;}
        else num=in-'0';
        while(in=getchar(),in>='0'&&in<='9'){
                num*=10,num+=in-'0';
        }
        if(IsN) num=-num;
        return true;
}

int MIN(int a,int b)
{
    if(a<b) return a;
    return b;
}

int main()
{
    scanf("%d",&n1);
    for(int i=1;i<=n1;i++)  scan_d(a[i]);
    scanf("%d",&n2);
    for(int i=1;i<=n2;i++)  scan_d(b[i]);
    
    int sz=0; int p1=1,p2=1;
    a[n1+1]=999999999,b[n2+1]=999999999;

    while(sz<n1+n2)
    {
        sz++;
        int tmp1=a[p1],tmp2=b[p2];
        if(tmp1<tmp2) p1++; else p2++;
        if(sz==(n1+n2+1)/2)
        {
            printf("%d\n",MIN(tmp1,tmp2));
            break;
        }
    }
    return 0;
}

 

posted @ 2016-05-19 20:09  Fighting_Heart  阅读(108)  评论(0编辑  收藏  举报