I've been stuck on this problem (https://codeforces.com/contest/1492/problem/B) for a pretty Long while. I always get "Time limit exceeded" when I submit the code.
My solution is to input the items of the array then determine the largest number in the array and diplay it along with the elements following it and so on.
How could I make my algorithm more efficient?
#include <iostream>
#include <vector>
#include <algorithm>
usingnamespace std;
int main() {
int T, n;
cin >> T;
while (T--) {
//inputting p[n]
scanf_s("%d", &n);
int* p = newint[n];
for (int i = 0; i < n; i++) {
scanf_s("%d", &p[i]);
}
while (n != 0) {
//*itr = largest element in the array
auto itr = find(p, p + n, *max_element(p, p + n));
int index = distance(p, itr);
for (int i = index; i < n; i++) {
printf("%d\n", p[i]);
}
//deleting element by decreasing n:
n = index;
}
delete[] p;
}
return 0;
}
You have clearly not even bothered to read the comments in that thread.
And we still can't do anything with the link you have attempted to provide, so we can't see what on earth you are trying to do. We aren't mind readers.
#include <iostream>
#include <limits>
int main() {
size_t T {};
std::cin >> T;
for (size_t n {}; T--; ) {
std::cin >> n;
constauto p {newint[n] {}};
int max {std::numeric_limits<int>::min()};
for (size_t i = 0; i < n; ++i) {
std::cin >> p[i];
if (p[i] > max)
max = p[i];
}
std::cout << "The largest value is " << max << '\n';
delete[] p;
}
}