#C++ Logic Implementation Help - program timing out

157 messages · Page 1 of 1 (latest)

gritty knotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

real acorn
#

Cant help without seeing code

wise flax
real acorn
#

TLE is almost always because your algorithm is suboptimal

wise flax
#

these are the sample cases btw

#

Scoring:
Inputs 1-3: N <= 2
Inputs 4-5: N <= 50 and a_i, h_i <= 10^3
Inputs 6-8: N <= 10^3
Inputs 9-13: No additional constraints
and this is the scoring (for time complexity)

gritty knotBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
wise flax
#

my program is getting the first 3 cases, but 4-8 wrong, and timing out for 9-13

#

@real acorn can i dm you my code?

real acorn
#

no

wise flax
real acorn
#

just at a glance, TLEs are gonna be caused because you have an O(n^2 log n) algorithm

wise flax
#

how do you think i should fix it

#

so like O(n^2) isn't' going to be sufficient right

#

so it would be like O(N) or O(Nlogn)

real acorn
#

likely not

wise flax
#

right

real acorn
#

you want something sub-quadratic

wise flax
#

okay

#

what data structure do u think

#

i'm confused

real acorn
#

But first, my focus would be on getting it correct

#

The data structure probably isn't the issue, but the algorithm itself

wise flax
#

oh ok

#

can u take a look at it tell me what i should fix

#

ive been staring at this for the past 2 hours lol

real acorn
#

ngl, your code is extremely hard to comprehend

wise flax
real acorn
#

don't write code you can't explain 😉

wise flax
#

so its basically iterating through a map

real acorn
#

but the reason your code is hard to leave it because of the lack of spacing, single letter variable names, etc

wise flax
#

should i send it with it being reformatted

real acorn
#

yeah, it's hard to read as it is

wise flax
#

got it

#

too long so ill split it up into 2 messages

#

@real acorn

real acorn
#

it's a lot of code, gotta chill and give me time to read and digest

#

also, doing a quick google search of your problem, looks like your classmates are trying in various places as well lol

wise flax
#

lol

real acorn
#

can you come up with a minimal case that does not work?

wise flax
#

my code works for 1-3

#

gets it wrong for 4-8, and times out for 9-13

real acorn
#

No no no. A minimal test case.

wise flax
#

so it only works when N<=2

real acorn
#

Not the conditions it doesn't work under. A reproducing example where it doesn't work that is as small as possible

wise flax
real acorn
#

make one

wise flax
# real acorn make one

i tried, but i can't find one
the sample test cases provided work
i checked some edge cases and they seem to work fine

#

i think it might be a problem with the time complexity

real acorn
#

only the last 4 are tested for time complexity

real acorn
#

that's what your description says, at least

wise flax
#

but there doesn't seem to be any inherent issues with the code though

real acorn
#

sure there is. it's getting a wrong answer

wise flax
#

hmm

#

is it like an edge case

#

i cant think of anything

real acorn
#

Maybe, hard to tell without an example that reproduces it

wise flax
#

yeah like

#

i can't actually see which test cases fail

real acorn
#

means it's time to make one that fails

wise flax
#

ok ill try

marble gulch
#

<@&847915341954154536> this is part of an ongoing copetition close asap

#

USACO Bronze

#

@wise flax I WILL report you

shadow aurora
#

It looks like @real acorn has done a good job of guiding them without giving them the answer

wise flax
#

yeah im just trying to work through the solution

marble gulch
#

ah

#

ok but you csnt be asking about the question while the competition is going

#

ppl might see

#

its not rly a big deal cuz bronze but still

desert egret
#

I'm not really sure it's reasonable for you to ask us to enforce that

marble gulch
#

ik

#

sry i kinda over reacted

desert egret
#

All good

wise flax
#

yeah alr i just wanna know what's wrong with my code

marble gulch
#

but if it were plat i would def do smthn

wise flax
#

dw its just bronze lmao

marble gulch
#

ikik

real acorn
#

If the competition is ongoing, I will not be helping further, sorry

wise flax
#

i finished the contest

#

i will send screenshot of proof

real acorn
#

it's not just you, it's anyone else in that competition

#

no unfair advantages

wise flax
#

im j rly upset cuz i was just a few test cases off

real acorn
#
  1. No, I can't make private channels
  2. I don't help in DMs, sorry
wise flax
#

alright, sorry

#

i will delete everything to ensure competition security

gritty knotBOT
#

@wise flax

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

stoic ore
#

i don't think that's necessary

wise flax
#

oh

#

can people still help?

stoic ore
#

i mean that i don't think it's necessary for you to delete everything

wise flax
#

yea but

#

i just dont want to risk competition security yk

stoic ore
#

i don't think there are any relevant spoilers here

wise flax
#

ok

real acorn
#

Not here, but a working solution would be

wise flax
#

yeah

stoic ore
#

ofc

wise flax
#

can i still get advice for implementaiton here

stoic ore
#

ofc you can ask questions

#

we simply won't just write you a solution

#

that's all

real acorn
#

and if you find a solution, we strongly advice you to not post the solution 😉

stoic ore
#

yeah

wise flax
#

okay

#

got it

#

kk i deleted some stuff so ill repost

stoic ore
#

though the probability that anyone in that competition will find this thread in time to copy the code is pretty much 0 anyways

wise flax
#

true

stoic ore
#

but yeah, better not post a working solution

wise flax
#

ok

#

my solution below is not working btw

#

only for first 3/13 tc

#

FJ is growing N (1 <= N <= 2*10^5) plants of asparagus on his farm! However some of his plants have genetic differences, so some plants will grow faster than others. The initial height of the i’th plant is h_i inches, and after each day, the ith plant grows by a_i inches.

FJ likes some of his plants more than others, and he wants some specific plants to be taller than others. He gives you an array of distinct values t_1,....,t_N containing all integers from 0 to N-1 and he wants the i’th plant to have exactly t_i other plants that are taller than it. Find the minimum number of days so that FJ’s request is satisfied, or determine that it is impossible.

#

Input format:
The first will contain of an integer T, denoting the number of independent test cases (1 <= T <= 10).
The first line of each test case contains of an integer N.
The second line consists of N integers h_i (1 <= h_i <= 10^9) denoting the initial height of the i’th plant in inches.
The third line consists of N integers a_i (1 <= a_i <= 10^9) denoting the number of inches the i’th plant grows each day.
The fourth line consists of N distinct integers t_i denoting the array that FJ gives you.
It is guaranteed that the sum of N over all test cases does not exceed 2*10^5.

Output format:
Output T lines, the answer to each test case on a different line. If it's not possible, output -1.
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (eg. long long).

Sample input:
6
1
10
1
0
2
7 3
8 10
1 0
2
3 6
10 8
0 1
2
7 3
8 9
1 0
2
7 7
8 8
0 1
2
7 3
8 8
1 0

Sample output:
0
3
2
5
-1
-1

In the first sample input, there are 6 test cases.
In the first test case, there is only one plant, so the condition is satisfied on day 0.
In the second test case, we need the first plant to be shorter than the second plant. After day 1, the heights are 15 and 13. After day 2, the heights are both 23. After day 3, the heights are 31 and 33, and that’s the first day in which the condition is satisfied.
The third and fourth test cases are similar to the second.
In the fifth test case, both plants start with an initial height of 7 and a growth rate of 8. So they will always have identical heights, and therefore the condition is never satisfied.
In the sixth test case, the condition is not satisfied initially and the growth rates are the same. So the condition can never be satisfied.

Sample input:
2
5
7 4 1 10 12
3 4 5 2 1
2 1 0 3 4
5
4 10 12 7 1
3 1 1 4 5
2 4 3 1 0

Sample output:
4
7

#

In the second sample input, there are 2 test cases.
In the first test case, the final heights after day 4 are 19, 20, 21, 18, 16.
In the second test case, the final heights after day 7 are 25, 17, 19, 35, 36.

#

**
Scoring:
Inputs 1-3: N <= 2
Inputs 4-5: N <= 50 and a_i, h_i <= 10^3
Inputs 6-8: N <= 10^3
Inputs 9-13: No additional constraints**

#
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <map>
using namespace std;

int main() {
    long long T;
    cin >> T;
    for (long long mc = 0; mc < T; mc++) {
        long long N;
        cin >> N;
        vector<long long> heights(N), growthRates(N), targets(N), currentCounts(N);
        for (long long i = 0; i < N; i++) {
            cin >> heights[i];
        }
        for (long long i = 0; i < N; i++) {
            cin >> growthRates[i];
        }
        for (long long i = 0; i < N; i++) {
            cin >> targets[i];
        }
        for (long long i = 0; i < N; i++) {
            currentCounts[i] = 0;
        }

        set<long long> timeStamps;
        map<long long, vector<long long>> increaseCounts, decreaseCounts;

        // Calculate the initial counts and potential changes in the future
        for (long long i = 0; i < N; i++) {
            for (long long j = 0; j < N; j++) {
                if (heights[i] > heights[j]) continue;
                if (heights[i] < heights[j]) {
                    currentCounts[i]++;
                    if (growthRates[i] > growthRates[j]) {
                        long long timeToOvertake = (long long)ceil((heights[j] - heights[i]) / (double)(growthRates[i] - growthRates[j]));
                        long long nextTime = (long long)((heights[j] - heights[i]) / (growthRates[i] - growthRates[j])) + 1;
                        decreaseCounts[timeToOvertake].push_back(i);
                        timeStamps.insert(timeToOvertake);
                        increaseCounts[nextTime].push_back(j);
                        timeStamps.insert(nextTime);
                    }
                } else if (growthRates[i] < growthRates[j]) {
                    increaseCounts[1].push_back(i);
                }
            }
        }

#
       // Check if initial condition is satisfied
        bool isSatisfied = true;
        for (long long i = 0; i < N; i++) {
            if (currentCounts[i] != targets[i]) {
                isSatisfied = false;
                break;
            }
        }
        if (isSatisfied) {
            cout << 0 << "\n";
            continue;
        }

        // Iterate over each timestamp to check if the condition is satisfied
        for (long long time : timeStamps) {
            isSatisfied = true;
            for (long long i : increaseCounts[time]) {
                currentCounts[i]++;
            }
            for (long long i : decreaseCounts[time]) {
                currentCounts[i]--;
            }
            for (long long i = 0; i < N; i++) {
                if (currentCounts[i] != targets[i]) {
                    isSatisfied = false;
                    break;
                }
            }
            if (isSatisfied) {
                cout << time << "\n";
                break;
            }
        }

        if (!isSatisfied) {
            cout << "-1\n";
        }
    }
    return 0;
}```
#

this is my code, it only works for the first 3 test cases and gets 4-8 incorrect, and times out for 9-13

#

any advice?

stoic ore
#

i doubt anyone's gonna be bored enough to debug your code for you ^^

#

but we'll see

wise flax
#

for every single case and edge case it works

#

and time complexity shouldnt really be an issue till test case 8

#

so i'm not sure what im particularly missing

stoic ore
#

sry, i don't really have time to look into this, but you can ofc wait if someone else has

wise flax
#

alright

#

if someon can help it would be greatly appreciated

#

like just something to start with looking at

marble gulch
wise flax
#

i can just use <bits/stdc++.h>

#

instead right?

marble gulch
#

yes...
but I hope you know never to do that outside competitions

wise flax
#

i was just referring to the part about making the code cleaner

marble gulch
#

yes

wise flax
#

do you have any advice i should use

marble gulch
#

didnt solve the problem in comp and haven't looked at it since

wise flax
#

oh alr

#

if anyone can help advice would be greatly appreciated

marble gulch
#

please dont ask for hlpe while the competition is ongoing

wise flax
#

i also didn't make silver

wise flax
#

!solved