不知为何我的代码有一组数据过不了
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> using namespace std; const int N = 1000010; long long a[N],b[N]; int main() { #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif // LOCAL int i=1,j=1; int n,m; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%lld",&b[i]); } int pos=m+n; int cnt=1; if(pos%2==0)pos/=2; else pos=pos/2+1; while(i<=n&&j<=m){ if(a[i]<=b[j]){ if(cnt==pos){ cout<<a[i]; break; } i++;cnt++; }else{ if(cnt==pos){ cout<<b[j]; break; } j++;cnt++; } } if(cnt!=pos){ while(i<=n){ if(cnt==pos){ cout<<a[i]; break; } i++;cnt++; } while(j<=m){ if(cnt==pos){ cout<<b[j]; break; } j++;cnt++; } } return 0;
}
满分代码:
#define LOCAL #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> using namespace std; const int INF = 0x7fffffff; const int N = 1000010; int a[N],b[N]; int main() { #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif // LOCAL int n,m; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%d",&b[i]); } a[n]=b[m]=INF; int pos=(m+n-1)/2; int i=0,j=0,cnt=0; while(cnt<pos){ if(a[i]<b[j])i++; else j++; cnt++; } if(a[i]<b[j]){ printf("%d\n",a[i]); }else printf("%d\n",b[j]); return 0; }