#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;
}
#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)
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.
😭
what is the error
does it compile?
what does it need to do
Send it here
cin >> queryRight >> value; this could fail if you enter something out of bound of tree
when you access tree[queryRight]
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
Did you try debugging?
ye
still wrong
i receive the result after do the test
@ancient oyster
yeah i need to understand the exercise first. Im really bad in that stuff unless I do it myself.
oh ok
haizzz
idk what the problem make the code return wrong awnser
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?
it the right output
hmm
okay so i atleast understand the objective
What are those values? cin >> queryRight >> value;
oh i will explain to u:
And also take a look here
hmm
Did you try to implement a searching algorithm?
3 is the last place, that i can only Fertilize from first tree to that free
i was try
but idk what to do
tree not free XD
and
i have 11 bag of fertilize
You could use stuff from the std or you want to do it yourself for learning purposes?
i did it by all i have
remember
huh
it's a test not a homework
Ah okay. A test for what?
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
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
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.
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
ye
if all at the same hight, it will fertilizer at place :0
I dont understand why u have to input the value yourself
If you know the smallest tree
Because how you check which is the smallest? Debugger?
binary search
hmm
only the ammount of tree, how tall all off them, how many fertilizer bag
then i will input that i need to fertilizer from 0 to which place, how many fertilizer slot i can do
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
yes
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
np i suck at algorithms
lêmme see how it work
oh yeah
And even possible to achive in a test.
The solution
I just havent used iterators lately
So i had to look it up
You know iterators?
hmm
i think i don't know about it so much
if u ask which i will learn next
i will search about it :)))
Thats also why i use auto it = ...; otherwise you had to type vector<int>::iterator and it can get alot longer
auto is nice
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
the ambersent is important. Do you know why?
So you work on the same object. Not a copy
otherwise the value is copied to min and than gone
Also important in for each loops to consider
either const, reference or const reference
now i wanna try to fix some exsercise by myseft
hmm
i see that my code was so complicated
dem XD
I think better variable names would also make it easier
I had to look the assignment each time
oh srry for that 💀
But yeah needs time 😃
hell yeah
But i was to lazy too haha
:))
I even dont know if a binary search tree is the right approach. Because you dont search a specific element.
hmm
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
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
why it has to be sorted?
to readuce the complexity
It dont need to be sorted
ye ik
I dont think that reduces it. Because you dont know what you search. But im not sure.
So its O(N)
idk will it true but it always stand in in order from largest to smallest
But how you learn C++?
Because why shouldnt the implement binary search tree in std::min_element if its faster. But maybe ask that question in #1013107104678162544
ye
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
what teacher?
wrong awnser
sed that i can't find the website that check my code
ask him?
hmm
But is this for a test you know doing? I thought it is just for training? Otherwise i cant help you
lol it is training
i can't use internet when have a test
but is was a test lmao
i wanna to learn from my wrong
you can't allocate a non-dynamic array with run-time size
ll tree[n];
you should use
auto tree = new long long[n];
ok, here u use vector, fine
nvm then
Okay, but i recommend to learn more about the basics before doing such questions