Answer the question
In order to leave comments, you need to log in
How to solve such a problem? USE 2014 informatics
I don't think my question is out of scope for this site, but I apologize in advance.
I came across such a task, but such that there were no similar ones in any collection or in the demo version. The task was in real kims of 2014.
I write from memory, so I omit some details.
C4.
Scientists take readings from the Tonegawa apparatus once a minute.
The input is N - the number of readings taken during this period and the readings themselves.
Your task is to write a program that will find the minimum product of two elements obtained with an interval of at least 6 minutes.
Input example:
11
20
12
11
4
5
9
10
24
43
20
7
Sample output:
28
Memory limit: 1 kilobyte.
Time limit: if the number of elements increases by K times, the execution time should not increase by more than K times.
Give me at least an idea for a solution. If you need any clarification - ask.
Answer the question
In order to leave comments, you need to log in
I also wrote this issue. Maintained a minimum on the prefix. The program passed the stress test at home, though in the form because I wrote in Pascal, and not in C ++ I wrote for i := 1 to n out of habit, but I had to have the condition i < n, that is, up to n-1;
So, if we are in the element with number i, then minpref is the minimum on the interval from 1 to i-6 that we support (in each iteration, we look at the element that was 6 iterations ago and if a[i-6] < minpref then minpref = a[i-6]), and if a[i] * minpref < minout, then minout = a[i] * minpref. How to do this with O (n) memory is obvious, I have written a crutch that works with constant memory (array a), it was possible to use a standard queue, but they would not have given the maximum points for it according to the criteria.
Running time - O(n).
A piece with a reading from 0 to 5 can also be pushed into the main loop, if everything is pre-filled with +inf.
If it is not clear, then I can explain some points.
Actually code:
#include <stdio.h>
#include <iostream>
using namespace std;
double a[6];
int main(void){
int i,n;
double minpref = 1000000, minout = 1000000, xcur, a[5];
cin >> n;
for (int i = 0; i <= 5; i++)
cin >> a[i];
for (int i = 6; i < n; i++){
cin >> xcur;
minpref = min(minpref, a[i % 6]);
a[i % 6] = xcur;
minout = min(minout, xcur * minpref);
}
cout << minout << "\n";
return 0;
}
Cyclically move from the first element to the last, count the products along the way, and if the last counted is greater than the previous minimum, update the min.
Maybe I misunderstood something, but the task is childish? Is it possible that you, having studied all the collections, could not write it?
In addition to @jezzit 's answer
#include <iostream>
#define TIME 6
using namespace std;
int main() {
int n,min=0xffffff;
cin >> n;
int list[n];
for(int i=0;i<n;i++) {
cin >> list[i];
if(i >= TIME) {
for(int j=i-TIME;j>=0;j--) {
if(list[i]*list[j] < min) {
min = list[i]*list[j];
}
}
}
}
cout << min << endl;
return 0;
}
If you understand the task correctly, then look for the 2 smallest numbers as you receive data, at the end you multiply these two numbers
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question