-2

I have been trying to solve this problem for an online judge system, which requires a time limit of 1000ms. I have tried a few variations of solutions, but I will post my best draft here. I was able to get my answers right, however, I am exceeding the time limit. The best solution I can come up with is O(n^2).

The Task problem
Once the programmer X was in China and noticed that Russian clocks “Zarya” are 10 times cheaper there than in Russia. X chose to do some shenanigans and bought a huge amount of clocks to bring it to his own country and sell them at half price (which actually means 5x times more expensive than he bought). But as soon as he came back home, he realized that many clocks go discordantly, moreover, they stop from a simple push (or start going if they were stopped before).

Obviously, the clocks were fake, just very accurate copies. To sell them really quickly, X wants to set them all to the same time (so it won’t matter if the time’s correct or not – he can say this is “the time of the manufacturer”) and just shake his bag to make them tick.

To set the time, he has to spin a winding crown that will make clock’s hands move: the hour hand moves 60 times slower than the minute hand and the minute hand is 60 times slower than the second hand. One full spin of a crown makes a full spin of the second hand; and although the spin takes just a second, it will take 6 minutes to change the time to 6 hours. It is allowed to spin a crown only clockwise to save fragile mechanism of a clock.

Help the programmer X minimize the effort put in preparing the clocks to be sold, choosing the optimal time to set all clocks to.

Input:

The first line contains a natural n (1 ≤ n ≤ 50000) – the quantity of clocks.

The next n lines contain the time of each clock in a format “h:mm:ss”, where h (1 ≤ h  ≤ 12) means hours, mm and ss (00 ≤ mm,ss ≤ 59) – minutes and seconds.

Output:

The time all clocks need to be set to in the format presented above.

Example Input
3
11:30:00
12:10:01
6:10:18
Output
12:10:01

#include<iostream>
using namespace std;

struct Clock {
    int hours;
    int mins;
    int secs;
    Clock() {
        hours = mins = secs = 0;
    }
};

void strtotime(string str, Clock& clock) { //converts string input to time
    if (str[1] == ':') {
        clock.hours = (str[0] - 48);
        clock.mins = (str[2] - 48) * 10 + (str[3] - 48);
        clock.secs = (str[5] - 48) * 10 + (str[6] - 48);
    }
    else {
        clock.hours = (str[0] - 48) * 10 + (str[1] - 48);
        clock.mins = (str[3] - 48) * 10 + (str[4] - 48);
        clock.secs = (str[6] - 48) * 10 + (str[7] - 48);
    }
    
}
double calctime(Clock from, Clock to) {//calculates time taken to change one clock time to other's
    //calculate time for hours
    double minutes;
    if (from.hours > to.hours) {
        minutes = 12 - (from.hours - to.hours);
    }
    else {
        minutes = to.hours - from.hours;
    }
    //calculate time for mins
    double seconds;
    if (from.mins > to.mins) {
        seconds = 60 - (from.mins - to.mins);
    }
    else {
        seconds = to.mins - from.mins;
    }
    //calculate time for secs
    double seconds2;
    if (from.secs > to.secs) {
        seconds2 = 60 - (from.secs - to.secs);
    }
    else {
        seconds2 = to.secs - from.secs;
    }

    double totalTime = minutes * 60 + seconds + (seconds2 / 60);

    return totalTime;
}
int main() {
    int n;
    string str;
    cin >> n;
    Clock* clock = new Clock[n];
    for (int x = 0; x < n; x++) {
        cin >> str;
        strtotime(str, clock[x]);
    }
    double totaltime;
    double mintime;
    int loc = 0;
    bool first = true;
    double* timearr = new double[n];
    for (int x = 0; x < n; x++) {
        totaltime = 0.0;
        for (int y = 0; y < n; y++) {
            if (x != y) {
                totaltime += calctime(clock[y], clock[x]);
            }
        }
        if (first) {
            mintime = totaltime;
            first = false;
        }
        else {
            if (totaltime < mintime) {
                mintime = totaltime;
                loc = x;
            }
        }
    }

    cout << clock[loc].hours;
    cout << ':';
    if (clock[loc].mins < 10) {
        cout << 0 << clock[loc].mins;
    }
    else {
        cout << clock[loc].mins;
    }
    cout << ':';
    if (clock[loc].secs < 10) {
        cout << 0 << clock[loc].secs;
    }
    else {
        cout << clock[loc].secs;
    }
    
}
  • 2
    I think it needs an example. The text is long and confusing. – Carlos Jan 13 '21 at 10:28
  • @Carlos Added an example. Sorry but forgot it the first time. – Yahya Abdul Majeed Jan 13 '21 at 10:33
  • 3
    What is the point of the exercise if someone else solves it for you. – Quimby Jan 13 '21 at 10:40
  • Write `'0'`, which is guaranteed to work, instead of `48`, which isn't. (Even better would be to write a conversion function.) – molbdnilo Jan 13 '21 at 13:23
  • You can simplify your life by representing time only as seconds, converting to "display time" when you're done. (I wouldn't be surprised if this would let you apply some maths to make a simpler solution fall out.) – molbdnilo Jan 13 '21 at 13:31
  • Suppose the clock was a 6 second clock. And there are 2 clocks. They read 1 and 5. How far off is 0, 1, 2, 3, 4, 5 innthat situation? Do you see any mathematical pattern? – Yakk - Adam Nevraumont Jan 13 '21 at 13:48

1 Answers1

-1
  1. Sort the times
  2. Calculate the difference between each two neighbors and the first and last element
  3. Find the maximum difference and remove it (the solution is the left neighbor of this difference)
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • Can you explain why it is so? I don't seem to get the logic behind you solution. @Thomas Sablik – Yahya Abdul Majeed Jan 14 '21 at 09:50
  • @YahyaAbdulMajeed That's because you won't learn anything from coding competitions. You need mathematical background. The brute force approach is usually to slow. Try to formulate a mathematical formula to calculate the sum of differences depending on the clock and try to minimize it. E.g. You have 3 clocks with 1, 2, 10. For 10 it's (8 + 1) + 8 + 0. For 1 it's 0 + (8 + 3) + 3. For 2 it's 1 + 0 + (3 + 1). You have to remove the 8. Therefore the solution is 2. – Thomas Sablik Jan 14 '21 at 09:54