2797 2023. 01. 25 12:23:30 Error42 Videójáték cpp11 Accepted 100/100 188ms 8660 KiB
// Code of Martin Nagy (CWM), only debugged by me

#include <iostream>
#include <vector>
using namespace std;
//#define int long long
int T;

// last minute in which the character wasn't bored
//
// special value: if equal to Me.end, means that we got bored and stopped playing
int lastPlayedGlobal;
struct interval {
    int beginning;
    int end;
};
char intersectionType(interval Me, interval Hano) {
    // intersecting and Me.beginning <= Hano.beginning
    if (Me.beginning <= Hano.beginning && Me.end >= Hano.beginning) return 'M';
    // intersecting and Me.beginning > Hano.begin
    if (Me.beginning >= Hano.beginning && Hano.end >= Me.beginning) return 'H';
    // no intersection
    return 'N'; 
}
int calcPlayTime(interval Me, interval Hano) {
    char iType = intersectionType(Me, Hano);
    if (iType == 'N') return 0;
    if (iType == 'H') return min(Hano.end, Me.end) - Me.beginning + 1;
    if (iType == 'M') {
        if (
            lastPlayedGlobal + T + 1 >= Hano.beginning 
            && /* handle special value: */ lastPlayedGlobal != Me.end
        ) {
            return min(Hano.end, Me.end) - Hano.beginning + 1;
        }
        else {
            lastPlayedGlobal = Me.end;
            return 0;
        }
    }
}
signed main()
{
    cin >> T;
    int meIntervalCount, hanoIntervalCount;
    cin >> meIntervalCount;
    vector<interval> meIntervals;
    vector<interval> hanoIntervals;
    for (size_t i = 0; i < meIntervalCount; i++)
    {
        int a, b;
        cin >> a >> b;
        meIntervals.push_back({ a,b });
    }
    cin >> hanoIntervalCount;
    for (size_t i = 0; i < hanoIntervalCount; i++)
    {
        int a, b;
        cin >> a >> b;
        hanoIntervals.push_back({ a,b });
    }
    int hanoIntervalIndex = 0;
    int meIntervalIndex = 0;
    lastPlayedGlobal = meIntervals[0].beginning - 1;
    int MaxPlayTime = 0;
    while (true) {
        MaxPlayTime += calcPlayTime(meIntervals[meIntervalIndex], hanoIntervals[hanoIntervalIndex]);
        if (meIntervals[meIntervalIndex].end >= hanoIntervals[hanoIntervalIndex].end) {
            // Hano's planned break ended strictly earlier

            // special value handled: always Hano.end < Me.end
            if (lastPlayedGlobal < hanoIntervals[hanoIntervalIndex].end)lastPlayedGlobal = hanoIntervals[hanoIntervalIndex].end;
            hanoIntervalIndex++;
            if (hanoIntervalIndex == hanoIntervalCount) {
                cout << MaxPlayTime << "\n";
                return 0;
            }
        }
        else {
            meIntervalIndex++;
            if (meIntervalIndex == meIntervalCount) {
                cout << MaxPlayTime << "\n";
                return 0;
            }
            lastPlayedGlobal = meIntervals[meIntervalIndex].beginning - 1;
        }
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 3ms 1812 KiB
2 Accepted 2ms 2060 KiB
subtask2 10/10
3 Accepted 2ms 2208 KiB
4 Accepted 2ms 2604 KiB
5 Accepted 2ms 2736 KiB
6 Accepted 2ms 2796 KiB
7 Accepted 2ms 2920 KiB
8 Accepted 2ms 2964 KiB
9 Accepted 2ms 2844 KiB
10 Accepted 2ms 2996 KiB
11 Accepted 2ms 3188 KiB
subtask3 20/20
12 Accepted 2ms 3160 KiB
13 Accepted 2ms 3156 KiB
14 Accepted 2ms 3284 KiB
15 Accepted 2ms 3164 KiB
16 Accepted 2ms 3164 KiB
17 Accepted 3ms 3436 KiB
18 Accepted 3ms 3300 KiB
19 Accepted 4ms 3404 KiB
20 Accepted 3ms 3300 KiB
21 Accepted 3ms 3532 KiB
22 Accepted 2ms 3600 KiB
subtask4 30/30
23 Accepted 14ms 4028 KiB
24 Accepted 50ms 5008 KiB
25 Accepted 123ms 8100 KiB
26 Accepted 136ms 8200 KiB
27 Accepted 137ms 8152 KiB
28 Accepted 54ms 5624 KiB
29 Accepted 10ms 3972 KiB
30 Accepted 10ms 4340 KiB
31 Accepted 6ms 4192 KiB
subtask5 40/40
32 Accepted 16ms 4484 KiB
33 Accepted 61ms 5580 KiB
34 Accepted 115ms 6988 KiB
35 Accepted 164ms 8552 KiB
36 Accepted 188ms 8660 KiB
37 Accepted 185ms 8476 KiB
38 Accepted 94ms 6532 KiB
39 Accepted 114ms 8156 KiB
40 Accepted 28ms 5320 KiB
41 Accepted 43ms 5520 KiB