OJ-0708
示例1
1
App1 1 09:00 10:00
09:30
App1
示例2
2
App1 1 09:00 11:00
App2 2 09:10 09:30
09:20
App2
示例3
2
App1 1 09:00 11:00
App2 2 09:10 09:30
09:50
NA
示例4
4
App1 1 09:00 10:00
App2 2 10:10 11:00
App3 4 11:10 12:30
App4 5 10:30 11:30
11:20
App4
示例5
4
App1 1 09:00 10:00
App2 2 10:10 11:00
App3 4 11:10 12:30
App4 3 10:30 11:30
11:20
App3
方式一:
public class APP1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
in.nextLine();
List<App> list = new ArrayList<>();
for (int i = 0; i < N; i++) {
App app = new App(in.next(), in.nextInt(), timeToMin(in.next()), timeToMin(in.next()), -1);
list.add(app);
}
List<App> register = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (i == 0) {
App app = list.get(i);
app.index = i;
register.add(app);
continue;
}
App current = list.get(i);
current.index = i;
// 将注册app和已注册app比较,时间冲突的将index放入set集合中
Set<Integer> set = new HashSet<>();
for (int j = current.start; j < current.end; j++) {
for (App a : register) {
if (j >= a.start && j < a.end) {
set.add(a.index);
}
}
}
boolean flag = false;
// 取出时间冲突的index 和 已注册的比较等级,只要有等级比将注册的高就注册失败
for (int index : set) {
if (register.get(index).level > current.level) {
// 注册失败
flag = true;
break;
}
}
// 注册成功
if (!flag) {
for (int index : set) {
App app = list.get(index);
// App app1 = new App(app.name, app.level, app.start, app.end, app.index);
register.remove(app);
}
register.add(current);
}
}
// 获取输入的时间
int time = timeToMin(in.next());
App result = null;
for (App a : register) {
if (a.start <= time && time < a.end) {
result = a;
}
}
if (result == null) {
System.out.println("NA");
} else {
System.out.println(result.name);
}
}
// 时间转换为分钟
public static int timeToMin(String str) {
String[] strArr = str.split(":");
int hour = Integer.parseInt(strArr[0]);
int min = Integer.parseInt(strArr[1]);
return hour * 60 + min;
}
static class App {
private String name;
private int level;
private int start;
private int end;
private int index;
public App(String name, int level, int start, int end, int index) {
this.name = name;
this.level = level;
this.start = start;
this.end = end;
this.index = index;
}
@Override
public boolean equals(Object obj) {
if (null == obj) return false;
App app = (App) obj;
return name.equals(app.name) && level == app.level &&
start == app.start && end == app.end && index == app.index;
}
@Override
public int hashCode() {
return Objects.hash(name, level, start, end, index);
}
}
}
方式二
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N;
N = in.nextInt();
in.nextLine();
List<App> list = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
App app = new App(in.next(), Integer.parseInt(in.next()), timeToMin(in.next()), timeToMin(in.next()));
if (app.start <= app.end) {
list.add(app);
}
}
// 已注册app列表
List<Duration> dlist = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (i == 0) {
App app = list.get(i);
Duration duration = new Duration(app.name, app.start, app.end, i);
dlist.add(duration);
continue;
}
App current = list.get(i);
Set<Integer> set = new HashSet<>();
// 遍历将要注册app的开始时间-结束时间,依次比较已注册app的开始时间,结束时间
// 如果将注册app的开始时间 >= 已注册app的开始时间 && 将注册app的开始时间 < 已注册app的结束时间 ,同时增大将注册app的开始时间到结束时间
// (只要符合上述条件,说明将注册app的时间与已注册app的时间冲突了,此时将此已注册app的id记录下来)
for (int j = current.start; j < current.end; j++) {
for (Duration d : dlist) {
if (j >= d.start && j < d.end) {
set.add(d.index);
}
}
}
boolean isDown = false;
// 遍历记录下来的id,和已注册app的等级比较,只要存在将注册app的id大于已注册app的id,则set中的id是都要从已注册中删除的
for (int index : set) {
if (list.get(index).level > current.level) {
isDown = true;
}
}
if (isDown) {
} else {
for (int index : set) {
App app = list.get(index);
Duration duration = new Duration(app.name, app.start, app.end, index);
dlist.remove(duration);
}
Duration duration = new Duration(current.name, current.start, current.end, i);
dlist.add(duration);
}
}
String time = in.next();
int min = timeToMin(time);
Duration result = null;
for (Duration d : dlist) {
if (min >= d.start && min < d.end) {
result = d;
}
}
if (result == null) {
System.out.print("NA");
} else {
System.out.print(result.name);
}
}
static public int timeToMin(String t) {
String[] s = t.split(":");
int hour = Integer.parseInt(s[0]);
int min = Integer.parseInt(s[1]);
return hour * 60 + min;
}
static class App {
String name;
int level;
int start;
int end;
public App(String name, int level, int start, int end) {
this.name = name;
this.level = level;
this.start = start;
this.end = end;
}
}
static class Duration {
String name;
int start;
int end;
int index;
public Duration(String name, int start, int end, int index) {
this.name = name;
this.start = start;
this.end = end;
this.index = index;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
Duration duration = (Duration) o;
return this.name.equals(duration.name) && this.start == duration.start
&& this.end == duration.end && this.index == duration.index;
}
@Override
public int hashCode() {
return Objects.hash(this.name, this.start, this.end, this.index);
}
}
}
https://blog.csdn.net/weixin_52908342/article/details/135338594
原文地址:https://blog.csdn.net/wsd_ontheroad/article/details/140279903
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!