#Can u help me with it, idk which problem that make my code wrong, thank u in advance

1 messages · Page 1 of 1 (latest)

edgy pewter
#
#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    ll n, q, b;
    cin >> n >> q >> b;

    ll tree[n];

    for (ll &x : tree) x = b;

    while (q--) {
        ll queryRight, value, minPos = -1;
        cin >> queryRight >> value;
        queryRight--;

        while (value--) {
            ll left = 0, right = queryRight;
            ll key = tree[queryRight];
            while (left <= right) {
                ll mid = (left + right) / 2;
                if (tree[mid] == key) {
                    minPos = mid;
                    right = mid - 1;
                }
                else if (tree[mid] > key) left = mid + 1;
                else right = mid - 1;
            }
            tree[minPos]++;
        }
    }

    for (ll x : tree) cout << x << " ";
    return 0;
}

vivid charmBOT
#

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.

edgy pewter
#

😭

spiral heath
edgy pewter
#

our checker tell that the code wrong

spiral heath
#

does it compile?

edgy pewter
#

wrong awnser

#

ye

#

hmm

spiral heath
#

what does it need to do

edgy pewter
#

ok i will send u the problem

#

hmm

ancient oyster
#

Send it here

edgy pewter
#

ok

#

i wil ping all u

ancient oyster
#

cin >> queryRight >> value; this could fail if you enter something out of bound of tree

#

when you access tree[queryRight]

edgy pewter
#

Problem 4: Hoa Ban

This time, when visiting Dien Bien, you won't have the opportunity to admire the beautiful Ban flowers, as they only bloom in spring. However, you can still enjoy the row of n Ban flower trees planted along the road, numbered from tree 1 to tree n, from the foot to the top of Pha Din mountain.

When the trees were planted, each tree initially had a height represented by an integer B. To take care of these Ban flower trees, Q bags of fertilizer have been used. The bags of fertilizer were used sequentially, starting from bag 1 and finishing all fertilizer in one bag before moving to the next.

It is known that the i-th bag (1 ≤ i ≤ Q) contains p₁ granules of fertilizer. When using it, p₁ times of fertilization must be performed, each time doing the following:

Choose the tree with the smallest height among the trees from tree 1 to tree rᵢ. If multiple trees have the same minimum height, choose the tree with the smallest index among them.
Fertilize that tree by adding 1 unit to its height.
Task: Given the input, find the final height of each tree after all fertilization steps.

Input:
The first line contains three integers: n, Q, B (n, Q ≤ 10⁵, B ≤ 10⁹).
Each of the following Q lines contains two integers: rᵢ, pᵢ.
Output:
Output the final heights of all n trees, from tree 1 to tree n, separated by spaces.

#

here the problem

#

and

#

INPUT:
8 3 2
3 11
8 7
6 3

OUTPUT:
6 6 5 5 5 4 3 3

ancient oyster
#

Did you try debugging?

edgy pewter
#

still wrong

#

i receive the result after do the test

#

@ancient oyster

ancient oyster
edgy pewter
#

haizzz

#

idk what the problem make the code return wrong awnser

spiral heath
#

now i dont know how to implement it and im not sure if i did it correctly in my head, but the output should be 6 6 6 4 4 4 4 3 i think?

spiral heath
#

okay so i atleast understand the objective

edgy pewter
#

ye

#

im looking forward for ur new

ancient oyster
#

What are those values? cin >> queryRight >> value;

edgy pewter
#

oh i will explain to u:

edgy pewter
#

hmm

ancient oyster
#

Did you try to implement a searching algorithm?

edgy pewter
#

3 is the last place, that i can only Fertilize from first tree to that free

edgy pewter
#

but idk what to do

edgy pewter
#

and

#

i have 11 bag of fertilize

ancient oyster
#

You could use stuff from the std or you want to do it yourself for learning purposes?

edgy pewter
#

remember

ancient oyster
#

huh

edgy pewter
#

it's a test not a homework

ancient oyster
#

Ah okay. A test for what?

edgy pewter
#

Competency assessment

#

dem

#

all i did vẻy good

#

but it i have bad result

#

:0

#

hmm

#

did u see any problem from that code

ancient oyster
#

Yeah those can be really hard

#

Its difficult to implement by yourself. I will need more time or try it by myself firs

#

I will maybe try it later

edgy pewter
#

oh ok

#

thx alot

#

for u if u disunderstand my code
n: The number of trees.
q: The number of fertilizer bags.
b: The initial height of each tree.
tree[]: An array that stores the height of each tree.
queryRight: The index of the last tree to consider when applying fertilizer.
value: The number of times fertilizer will be applied from the current bag.
minPos: The position of the tree with the smallest height within the range.
left, right, key, mid: Variables used for binary search to find the tree with the smallest height within the given range.

ancient oyster
#

But isnt QueryRight at the first cycle =0? Because the assignment says to choose the tree with the first index if all trees the same hight

edgy pewter
#

if all at the same hight, it will fertilizer at place :0

ancient oyster
#

I dont understand why u have to input the value yourself

#

If you know the smallest tree

ancient oyster
#

Because how you check which is the smallest? Debugger?

edgy pewter
#

hmm

ancient oyster
#

But you input the value yourself

#

Maybe i just dont get it lol

edgy pewter
#

after all

#

only computer know the smallest tree

#

but the problem that

#

why it still wrong 😭

#

@ancient oyster

#

hmm

#

and all test are input all auto

#

:(((

#

did u understand the test and input yet? @ancient oyster

ancient oyster
#

yes

edgy pewter
#

hope u reply soon

ancient oyster
#

I tried doing it with c++ but there still the values wrong. Im out haha

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    int q;
    int b;
    cin >> n >> q >> b;

    std::vector<int> tree;
    tree.reserve(n);

    for (int i = 0; i < n; i++)
    {
        tree.push_back(b);
    }

    while (q--)
    {
        int queryRight;
        int value;

        cin >> queryRight >> value;
        queryRight--;

        auto it = tree.begin();
        auto last = it + queryRight;

        while (value--)
        {
            auto& min = *std::min_element(it, last);
            min++;
        }
    }

    for (auto x : tree)
    {
        cout << x << " ";
    }

    return 0;

}
#

That should be right from the logic?

#

Now it works @edgy pewter

#
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    int q;
    int b;
    cin >> n >> q >> b;

    std::vector<int> tree;
    tree.reserve(n);

    for (int i = 0; i < n; i++)
    {
        tree.push_back(b);
    }

    while (q--)
    {
        int queryRight;
        int value;

        cin >> queryRight >> value;
        //queryRight--;

        auto it = tree.begin();
        auto last = it + queryRight;

        while (value--)
        {
            auto& min = *std::min_element(it, last);
            min++;
        }
    }

    for (auto x : tree)
    {
        cout << x << " ";
    }

    return 0;

}
#

the queryRight-- was wrong

#

Now try to implement this with your binary search if thats necessary xD

edgy pewter
#

oh lemme try it :))

#

seem good

#

thx u so much

#

:)))

ancient oyster
#

np i suck at algorithms

edgy pewter
#

lêmme see how it work

edgy pewter
#

but i think it have less eror than mine

ancient oyster
#

But i passed the module haha

#

Its easier for sure

edgy pewter
#

oh yeah

ancient oyster
#

And even possible to achive in a test.

#

The solution

#

I just havent used iterators lately

#

So i had to look it up

edgy pewter
#

oh

#

:)))

#

but

ancient oyster
#

You know iterators?

edgy pewter
#

i think i don't know about it so much

#

if u ask which i will learn next

#

i will search about it :)))

ancient oyster
#

Thats also why i use auto it = ...; otherwise you had to type vector<int>::iterator and it can get alot longer

#

auto is nice

edgy pewter
#

oh yeah

#

i see it

#

lol

#

i was did knew that auto& min = *std::min_element(it, last); min++; can increse the smallest :)))

#

thx for it XD

ancient oyster
#

the ambersent is important. Do you know why?

edgy pewter
#

hmm

#

can u tell my why :)

ancient oyster
#

So you work on the same object. Not a copy

#

otherwise the value is copied to min and than gone

edgy pewter
#

oh

#

got it

ancient oyster
#

Also important in for each loops to consider

#

either const, reference or const reference

edgy pewter
#

oh

#

thx

#

||u don't know that i have 4 not 1 XD||

#

ok

#

cya

ancient oyster
#

cya

edgy pewter
#

hmm

#

i see that my code was so complicated

#

dem XD

ancient oyster
#

I think better variable names would also make it easier

#

I had to look the assignment each time

edgy pewter
#

oh srry for that 💀

ancient oyster
#

But yeah needs time 😃

edgy pewter
#

hell yeah

ancient oyster
#

But i was to lazy too haha

edgy pewter
#

:))

ancient oyster
#

I even dont know if a binary search tree is the right approach. Because you dont search a specific element.

edgy pewter
#

idk

#

i was thing the smallest is the last element

#

and i was try to search the elements before it have value same it

#

dem

#

i think i make problem from it

ancient oyster
#

Depends where you stopped. And how far you get with the fertilizer

#
#include <iostream>
int main()
{
    int v[100] = {5,14,2,4,6};
    int n = 5;
    int mic = v[0];
    for(int i = 0; i != n; ++i)
    {
        if(v[i] < mic)
        mic = v[i];
    }
    std:cout << mic << std::endl;;
}

You could also just loop through it(I just copied the code).
Because it has the same runtime as std::min_element(it, last);

#

But the best way is to use the function

edgy pewter
#

ye

#

but will all of them are sorted?

#

and we can do anything by it

ancient oyster
#

why it has to be sorted?

edgy pewter
#

to readuce the complexity

ancient oyster
#

It dont need to be sorted

edgy pewter
#

ye ik

ancient oyster
#

I dont think that reduces it. Because you dont know what you search. But im not sure.

#

So its O(N)

edgy pewter
#

idk will it true but it always stand in in order from largest to smallest

edgy pewter
#

i think it still good ( ur code )

ancient oyster
#

But how you learn C++?

edgy pewter
#

learn online

#

newbie

#

learn from august

#

this year

ancient oyster
#

Because why shouldnt the implement binary search tree in std::min_element if its faster. But maybe ask that question in #1013107104678162544

edgy pewter
#

ok

#

i will ask every one

#

dem so quiet XD

#

oh @ancient oyster now i think i need to look at correct awnser

#

i asked my teacher that why i have bad

#

Wrong Anwer(from teacher)

#

dem

#

XD

ancient oyster
#

what teacher?

edgy pewter
#

he got the result XD

ancient oyster
#

Okay

#

And what is wrong?

edgy pewter
#

sed that i can't find the website that check my code

ancient oyster
#

ask him?

edgy pewter
#

hmm

ancient oyster
#

But is this for a test you know doing? I thought it is just for training? Otherwise i cant help you

edgy pewter
#

i can't use internet when have a test

#

but is was a test lmao

#

i wanna to learn from my wrong

queen gyro
#

you can't allocate a non-dynamic array with run-time size

#
  ll tree[n];
#

you should use

auto tree = new long long[n];
queen gyro
#

nvm then

ancient oyster
#

Okay, but i recommend to learn more about the basics before doing such questions