【携程】真题-查询满足区间的记录
题目描述
有一批订单记录,数据有订单号,入店时间,离店时间;
输入一个时间值A
,需要在这批记录中找到符合入离店时间范围(A
大于等于入店时间,并且A
小于等于离店时间)内的所有记录。 单次查询时间复杂度控制在O(logN)
※注意:订单号升序输出
输入描述
记录数:10
时间值A:20180602
订单号 入店时间 离店时间
1001 20180103 20180105
1002 20180202 20180203
1003 20180304 20180306
1004 20180401 20180408
1005 20180501 20180504
1006 20180601 20180604
1007 20180705 20180706
1008 20180801 20180804
1009 20180903 20180903
1010 20181003 20181003
以上输入都为整型
输出描述
1006
示例一
输入
10
20180602
1001 20180103 20180105
1002 20180202 20180203
1003 20180304 20180306
1004 20180401 20180408
1005 20180501 20180504
1006 20180601 20180604
1007 20180705 20180706
1008 20180801 20180804
1009 20180903 20180903
1010 20181003 20181003
输出
1006
示例二
输入
5
20170103
1013 20180103 20180105
1022 20180102 20180103
1103 20180104 20180106
1034 20180101 20180102
1105 20180201 20180204
输出
null
说明
查不到时输出null字符串(小写)
示例三
输入
4
20180103
1013 20180103 20180105
1022 20180102 20180103
1026 20180103 20180103
1007 20180101 20180109
输出
1007
1013
1022
1026
说明
参考解题 C
#include<stdio.h>
int main()
{
int N;
scanf("%d",&N);
int T,j=0;
scanf("%d",&T);
int n[N],I[N],L[N],ans[N],s[N];
for(int i=0;i<N;i++)
{
scanf("%d %d %d",&n[i],&I[i],&L[i]);
}
for(int i=0;i<N;i++)
{
if(I[i]<=T&&L[i]>=T)
{
ans[j]=n[i];
j++;
}
}
if(j==0)
{
printf("null\n");
return 0;
}
for(int i=0;i<j;i++)
{
int a=0;
for(int k=0;k<j;k++)
{
if(ans[i]>ans[k])
{
a++;
}
}
s[a]=ans[i];
}
for(int i=0;i<j;i++)
{
printf("%d\n",s[i]);
}
}
参考解题 C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, t;
while (cin >> N >> t) {
map<int,int> m;
for (int i=0; i<N; i++) {
int serial, checkin, checkout;
cin >> serial>>checkin>> checkout;
if (checkin<=t && checkout>=t) m[serial] = 0;
}
if (m.empty()) cout << "null" << endl;
for (auto i : m) {
cout << i.first << endl;
}
}
return 0;
}
参考解题 Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.Queue;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String s1;
while((s1=reader.readLine())!=null){
int n=Integer.parseInt(s1);
int A=Integer.parseInt(reader.readLine());
Queue<Integer> queue=new PriorityQueue<>();
for (int i = 0; i <n ; i++) {
String[] strs=reader.readLine().split(" ");
int count=Integer.parseInt(strs[0]);
int time1=Integer.parseInt(strs[1]);
int time2=Integer.parseInt(strs[2]);
if(time1<=A&&A<=time2)
queue.offer(count);
}
if(queue.isEmpty())
System.out.println("null");
else {
while (!queue.isEmpty())
System.out.println(queue.poll());
}
}
}
}
参考解题 Python
import sys
n = int(input().strip())
a = int(input())
res = []
for i in range(n):
s = list(map(int, input().split()))
if s[1] <= a <= s[2]:
res.append(s[0])
if len(res) == 0:
print("null")
else:
res.sort()
for c in res:
print(c)