NodeJS Google Code Jam:我的火车时刻表问题解决方案失败了

igetnqfo  于 2023-04-05  发布在  Node.js
关注(0)|答案(1)|浏览(113)

Google Code Jam 2008中的一个例子:
该问题名为列车时刻表,您可以在此处找到完整的解释:代码堵塞-列车时刻表

注意:我决定用Node.js解决这个问题。

我的代码是下一个:

function timeToMinutes(time) {
  const timeArray = time.split(":");
  const hours = parseInt(timeArray[0]);
  const minutes = parseInt(timeArray[1]);
  const hoursInMinutes = hours * 60;
  const total = hoursInMinutes + minutes;
  return total;
}

function timetableFiller(NAB, NBA, array) {
  let timetable = {
    departuresFromA: [],
    arrivalsToB: [],
    departuresFromB: [],
    arrivalsToA: [],
  };
  for (let i = 0; i < NAB + NBA; i++) {
    let tempArr = [];
    tempArr = array[i].split(" ");

    if (i < NAB) {
      timetable.departuresFromA.push(tempArr[0]);
      timetable.arrivalsToB.push(tempArr[1]);
    } else {
      timetable.departuresFromB.push(tempArr[0]);
      timetable.arrivalsToA.push(tempArr[1]);
    }
  }
  return timetable;
}

function timetableToMinutes(timetable) {
  let timetableMinutes = {
    departuresFromA: [],
    arrivalsToB: [],
    departuresFromB: [],
    arrivalsToA: [],
  };

  for (const property in timetable) {
    timetable[property].map((element) =>
      timetableMinutes[property].push(timeToMinutes(element))
    );
  }

  return timetableMinutes;
}

function trainsNeededCounter(arrivalsFromDestiny, departuresFromOrigin, tat) {
  let trainsNeeded = departuresFromOrigin.length;
  for (let i = 0; i < arrivalsFromDestiny.length; i++) {
    for (let j = 0; j < departuresFromOrigin.length; j++) {
      if (arrivalsFromDestiny[i] + tat <= departuresFromOrigin[j]) {
        trainsNeeded = trainsNeeded - 1;
        departuresFromOrigin.splice(j, 1);
      }
    }
  }
  return trainsNeeded;
}

function responseGenerator(inputA, inputB, caseNumber) {
  return `Case #${caseNumber}: ${inputA} ${inputB}`;
}

function problemSolution(input) {
  const numberOfCases = parseInt(input[0]);
  input.shift();
  let response = [];
  let caseNumber = 0;
  let NAB;
  let NBA;
  for (let i = 0; i < input.length; i = i + NAB + NBA + 2) {
    caseNumber = caseNumber + 1;
    const tat = parseInt(input[i]);
    const arrayNTrips = input[i + 1].split(" ");
    NAB = parseInt(arrayNTrips[0]);
    NBA = parseInt(arrayNTrips[1]);
    const arraySchedule = input.slice(i + 2, i + 2 + NAB + NBA);
    const timetable = timetableFiller(NAB, NBA, arraySchedule);
    const timetableMinutes = timetableToMinutes(timetable);

    const trainsNeededAB = trainsNeededCounter(
      timetableMinutes.arrivalsToA,
      timetableMinutes.departuresFromA,
      tat
    );
    const trainsNeededBA = trainsNeededCounter(
      timetableMinutes.arrivalsToB,
      timetableMinutes.departuresFromB,
      tat
    );
    response.push(
      responseGenerator(trainsNeededAB, trainsNeededBA, caseNumber)
    );
  }

  return response;
}

function readInput() {
  const readline = require("readline");
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false,
  });
  let problem = [];
  rl.on("line", (line) => {
    problem.push(line);
  }).on("close", () => {
    const solution = problemSolution(problem);
    solution.map((response) => console.log(response));
  });
}

readInput();

如何复制问题

1.您应该使用Google帐户登录Code Jam。
1.粘贴到右侧代码区,激活测试运行模式
1.作为输入,您可以复制粘贴问题中提供的示例输入,您可以看到输出与示例输出完全相同。
我已经尝试了我自己的输入变化和响应似乎是正确的,但当我运行真实的的尝试平台说WA或错误的答案。

非常感谢您的帮助!

mrzz3bfm

mrzz3bfm1#

我最近做了一个关于这个的视频。你应该看看。我想你可以从中理解逻辑流程。我们基本上都在做同样的事情。https://youtu.be/_Cp51vMDZAs-看看这个

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve(int t)
{
int NA, NB;
float T;
cin >> T >> NA >> NB;
cin.ignore();
vector<string> ASchedule, BSchedule;
if (NA > 0)
    for (int i = 0; i < NA; i++)
    {
        string s;
        getline(cin, s);
        ASchedule.push_back(s);
    }
if (NB > 0)
    for (int i = 0; i < NB; i++)
    {
        string s;
        getline(cin, s);
        BSchedule.push_back(s);
    }
int alength, blength;
alength = (int)ASchedule.size();
blength = (int)BSchedule.size();
if (alength == 0 || blength == 0)
{
    cout << "Case #" << t << ": " << alength << " " << blength << endl;
    return;
}
float TT = T / 10;
string val, value;
int d;
float ADH, ADM, AAH, AAM, BDH, BDM, BAH, BAM;
vector<float> AD, AA, BD, BA;
for (int i = 0; i < alength; i++)
{
    val = ASchedule[i];
    ADH = stof(val.substr(0, 2));
    AAH = stof(val.substr(6, 2));
    ADM = stof(val.substr(3, 2));
    AAM = stof(val.substr(9, 2));
    if (val.at(9) == '0')
    {
        AAM /= 10;
        AAM += TT;
        AAM *= 10;
    }
    else
        AAM += T;
    if (AAM > 59)
    {
        d = -1;
        while (AAM != 59)
        {
            AAM -= 1;
            d++;
        }
        AAH++;
        AAM = 0;
        AAM += d;
    }
    // if (ADH > 23)
    //     ADH = 0;
    // if (AAH > 23)
    //     AAH = 0;
    ADM /= 100;
    ADH += ADM;
    AAM /= 100;
    AAH += AAM;
    AD.push_back(ADH);
    AA.push_back(AAH);
}
for (int j = 0; j < blength; j++)
{
    value = BSchedule[j];
    BDH = stof(value.substr(0, 2));
    BDM = stof(value.substr(3, 2));
    BAH = stof(value.substr(6, 2));
    BAM = stof(value.substr(9, 2));
    if (value.at(9) == '0')
    {
        BAM /= 10;
        BAM += TT;
        BAM *= 10;
    }
    else
        BAM += T;

    if (BAM > 59)
    {
        d = -1;
        while (BAM != 59)
        {
            BAM -= 1;
            d++;
        }
        BAH++;
        BAM = 0;
        BAM += d;
    }
    // if (BDH > 23)
    //     BDH = 0;
    // if (BAH > 23)
    //     BAH = 0;
    BDM /= 100;
    BDH += BDM;
    BAM /= 100;
    BAH += BAM;
    BA.push_back(BAH);
    BD.push_back(BDH);
}
int no1 = alength, no2 = blength;
sort(BD.begin(), BD.end());
sort(BA.begin(), BA.end());
sort(AA.begin(), AA.end());
sort(AD.begin(), AD.end());
for (int i = 0; i < alength; i++)
    for (int j = 0; j < blength; j++)
        if (AD[i] >= BA[j])
        {
            no1--;
            BA[j] = 50;
            break;
        }
for (int i = 0; i < blength; i++)
    for (int j = 0; j < alength; j++)
        if (AA[j] <= BD[i])
        {
            no2--;
            AA[j] = 50;
            break;
        }
cout << "Case #" << t << ": " << no1 << " " << no2 << endl;
}
int main()
{
int N;
cin >> N;
cin.ignore();
for (int t = 1; t <= N; t++)
    solve(t);
}

相关问题