The problem statement site e-olymp.com
Elevator
In the center of the city of New-Donetsk, a skyscraper was built with a huge number of floors. It also has an underground part, the number of floors of which is also incredibly large. A person who enters this skyscraper from the street gets to the floor with the number 0. The floors above him have positive numbers, and the negative numbers below. There is an elevator in the skyscraper. However, for certain reasons, it cannot stop on some floors. The elevator has two buttons - "up" and "down." When you press the "up" button, the elevator rises to the nearest floor where it can stop. When you press the "down" - goes down to the nearest such floor. Repeatedly pressing the button causes the elevator to perform the required action the appropriate number of times, without stopping at intermediate floors. The boy Vasya decided to ride this elevator. He entered the elevator on the ground floor and began to press the buttons.
Write a program to find the path that Vasya does.
Input data
The first line contains two integers K and N (0 ≤ K, N ≤ 10 ^ 5) - the number of floors on which the elevator does not stop, and the number of movements of the elevator, respectively. The second line contains K integers l1, l2, ..., lK (-10 ^ 9 ≤ l1 <... <lK ≤ 10 ^ 9) defining the numbers of these floors. It is guaranteed that all these numbers are different from 0. The third line contains N integers that define the commands that Vasya gave when he was in the elevator. The sign of the number identifies the button (the positive button corresponds to the "up" button, the negative - "down"), and its absolute value - the number of clicks. All numbers do not exceed 10 ^ 6 in absolute value.
Output
Output N numbers - the numbers of the floors on which the elevator stopped after each of Vasya's teams.
The code passes 82% due to exceeding the time limit
#include <stdio.h> #include <cmath> #include<set> using namespace std; int main() { set<long long> noset; long long k , n , curr = 0 , current = 0 , count = 0 , cur ,ioo; scanf("%lld %lld" , &k , &n); for(long long i = 0;i < k;i++) { scanf("%lld" , &ioo); noset.insert(ioo); } //quickSort(0 , k-1 , no); for(long long i = 0;i < n;i++) { scanf("%lld" , &curr); if(curr == 1) { do { current++; }while(noset.count(current)); printf("%lld" , current); } else if(curr == -1) { do { current--; }while(noset.count(current)); printf("%lld" , current); } else if(curr > 0) { count = 0; cur = current; while(count < abs(curr)) { do { current++; }while(noset.count(current)); count++; } printf("%lld" , current); } else if(curr < 0) { count = 0; cur = current; while(count < abs(curr)) { do { current--; }while(noset.count(current)); count++; } printf("%lld" , current); } printf(" "); } }