PAT Cars on Campus (30)
阿新 • • 發佈:2019-02-09
耗時好幾個小時寫完這道題,但是結果超時嚴重。記錄一下自己的程式碼與大神的程式碼。
我的程式碼:
package com.company; import java.text.SimpleDateFormat; import java.util.*; class Record implements Comparable<Record> { String carID; String time; String status; @Override public int compareTo(Record o) { return compareTime(this.time,o.time); } public static int compareTime(String s1,String s2) { SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss"); try{ Date d1=df.parse(s1); Date d2=df.parse(s2); long d=d1.getTime()-d2.getTime(); if(d>0) return 1; else if(d<0) return -1; else return 0; } catch (Exception e) { System.out.println(e.getStackTrace()); } return Integer.MIN_VALUE; } } class CarInfo { private String carID; private ArrayList<Date> inTime; private ArrayList<Date> outTime; private Date time; private int status; //1表示in,0表示out private SimpleDateFormat df; CarInfo() { inTime=new ArrayList<>(); outTime=new ArrayList<>(); status=0; df=new SimpleDateFormat("HH:mm:ss"); try { time = df.parse("0:0:0"); } catch (Exception e) { e.printStackTrace(); } } @Override public boolean equals(Object obj) { CarInfo car=(CarInfo)obj; return this.carID.equals(car.carID); } public void setCarID(String s) { this.carID=s; } public String getCarID() { return this.carID; } public void addInTime(String s) { try { Date d=df.parse(s); //判斷該輛車的狀態 if(status==0) { inTime.add(d); status=1; calcTime(); } else { inTime.remove(inTime.size()-1); inTime.add(d); calcTime(); } } catch (Exception e) { e.printStackTrace(); } } public ArrayList<Date> getInTime() { return inTime; } public void addOutTime(String s) { try { Date d=df.parse(s); if(status==1) { outTime.add(d); status=0; calcTime(); } } catch (Exception e) { e.printStackTrace(); } } public ArrayList<Date> getOutTime() { return outTime; } public int getStatus() { return status; } private void calcTime() { if(inTime.size()==outTime.size()) { long t=time.getTime(); t+=(outTime.get(outTime.size()-1).getTime()-inTime.get(inTime.size()-1).getTime()); time.setTime(t); } } public String getStrTime() { SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss"); return sdf.format(time); } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N=sc.nextInt(); int K=sc.nextInt(); Record[] records=new Record[N]; for(int i=0;i<N;i++) { records[i]=new Record(); records[i].carID=sc.next(); records[i].time=sc.next(); records[i].status=sc.next(); } Arrays.sort(records); ArrayList<CarInfo> carInfos=new ArrayList<>(); for(int i=0;i<N;i++) { CarInfo tmpCar=new CarInfo(); tmpCar.setCarID(records[i].carID); int index=carInfos.indexOf(tmpCar); if(index>=0) { if(records[i].status.equals("in")) { carInfos.get(index).addInTime(records[i].time); } else if(records[i].status.equals("out")) { carInfos.get(index).addOutTime(records[i].time); } } else { if(records[i].status.equals("in")) tmpCar.addInTime(records[i].time); else if(records[i].status.equals("out")) tmpCar.addOutTime(records[i].time); carInfos.add(tmpCar); } } SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss"); for(int i=0;i<K;i++) { try { Date queryTime=df.parse(sc.next()); int cnt=0; for(CarInfo car:carInfos) { for(int j=0;j<car.getInTime().size();j++) { if(queryTime.compareTo(car.getInTime().get(j))>=0) { if(j==car.getOutTime().size()||queryTime.compareTo(car.getOutTime().get(j))<0) cnt++; } } } System.out.println(cnt); } catch (Exception e) { e.printStackTrace(); } } TreeSet<String> longestCar=new TreeSet<>(); String longestTime="00:00:00"; int sum=0; for(int i=0;i<carInfos.size();i++) { //System.out.println(carInfos.get(i).getStrTime()); if(Record.compareTime(carInfos.get(i).getStrTime(),(longestTime))==1) { sum=1; longestTime=carInfos.get(i).getStrTime(); longestCar.clear(); longestCar.add(carInfos.get(i).getCarID()); } else if(Record.compareTime(carInfos.get(i).getStrTime(),(longestTime))==0) { sum++; longestCar.add(carInfos.get(i).getCarID()); } } for(String s:longestCar) { System.out.print(s+" "); } System.out.println(sum); } }
大神的程式碼:
// fun.cpp : Defines the entry point for the console application. // #include "stdafx.h" # include <cstdio> # include <algorithm> # include <string> # include <map> # include <iostream> # include <vector> using namespace std; const int debug = 0; const int size = 10050; const int range = 24 * 60 * 60; int sumcnt[range]; string ToWatch(int time_point) { char str[50]; sprintf(str, "%02d:%02d:%02d", time_point / 60 / 60, time_point % (60 * 60) / 60, time_point % 60); return string(str); } struct Log { string name; int type; int time; void Print() { cout << name << ' ' << ToWatch(time) << ' ' << type << endl; } bool match(const Log& cmper) const { return name == cmper.name&&time<cmper.time&&type == 0 && cmper.type == 1; } bool operator < (const Log& cmper) const { if (name != cmper.name) return name < cmper.name; else if (time != cmper.time) return time < cmper.time; else return type < cmper.type; } } car_log[size]; int ToDec(int hour, int min, int sec) { return (hour * 60 + min) * 60 + sec; } typedef pair<int, int> time_range; int main() { int i, j; int n, k; scanf("%d%d", &n, &k); string tmp; for (i = 0; i<n; i++) { cin >> car_log[i].name; int hour, min, sec; scanf("%d:%d:%d", &hour, &min, &sec); car_log[i].time = ToDec(hour, min, sec); cin >> tmp; car_log[i].type = tmp == "out"; } sort(car_log, car_log + n); if (debug)for (i = 0; i<n; i++) car_log[i].Print(); map<string, int > index; int maxrange = -1; for (i = 0; i<n; i++) { if (i + 1<n&&car_log[i].match(car_log[i + 1])) { index[car_log[i].name] += car_log[i + 1].time - car_log[i].time; if (index[car_log[i].name] > maxrange) maxrange = index[car_log[i].name]; sumcnt[car_log[i].time]++, sumcnt[car_log[i + 1].time]--; } } for (i = 1; i<range; i++) sumcnt[i] += sumcnt[i - 1]; while (k--) { int hour, min, sec; scanf("%d:%d:%d", &hour, &min, &sec); printf("%d\n", sumcnt[ToDec(hour, min, sec)]); } map<string, int>::iterator it; for (it = index.begin(); it != index.end(); it++) if (it->second == maxrange) cout << it->first << ' '; cout << ToWatch(maxrange) << endl; return 0; }