問題 F: The Intervals
時間限制: 2 Sec 內存限制: 65 MB[提交][狀態][討論版]
題目描述
Cantor, the famous mathematician, was working on a problem about intervals. Let's start from a line segment of unit length. Remove its middle 1/3. Now remove the middle 1/3's from the remaining two segments. Now remove the middle 1/3's from the remaining four segments. Now remove the middle 1/3's from the remaining eight segments. Now remove ... well, you get the idea. If you could continue this procedure through infinitely many steps, what would you have left? Now he assigns the following task to you. (He asked me to pass his assignment to you last night.) Given two arrays of numbers {A(n)} and {B(m)}. For each B(i) in {B(m)}, find 2 numbers a and b from {A(n)}, such that B(i) is in [a,b) and b-a<=|b'-a'| for all a' and b' from {A(n)} such that [a',b') contains B(i).
輸入
輸出
樣例輸入
3 3
10 20 30
15 25 35
樣例輸出
[10,20)
[20,30)
no such interval
#include<stdio.h>
#include<algorithm>
using namespace std;
int m,n;
int a[1000];
int erfen(int k)
{
int low=0,high=m;
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(k>=a[mid]&&k<a[mid+1])
return mid;
else if(k<a[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
int main()
{
int q;
int c[1000];
int b[1000];
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
scanf("%d",&b[i]);
sort(a,a+m);
for(int i=0;i<n;i++)
{
q=0;
q=erfen(b[i]);
if(q==-1)
printf("no such interval\n");
else
printf("[%d,%d)\n",a[q],a[q+1]);
}
return 0;
}