Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream>
- #include<vector>
- #include<algorithm>
- #include<stack>
- using namespace std;
- ifstream in("solar.in");
- ofstream out("solar.out");
- //declare stacks and N and some other variables to check wheather the first
- // of the last value fits out criteria
- int N;
- stack<int>max_from_startpoint;
- stack<int> values;
- int max_val,min_val;
- int first_val=0,last_val=-1;
- void Read_and_find_mins_to_here(){
- in>>N;
- //read the values and find the minimun one up to a certain point
- for(int i=0; i<N; i++){
- int val;
- in>>val;
- if(i==0){
- max_from_startpoint.push(val);
- values.push(val);
- min_val=max_val=val;
- }
- else{
- int curmax=max(max_from_startpoint.top(),values.top());
- max_from_startpoint.push(curmax);
- values.push(val);
- //den xreiazetai na alla3w tin timi toy afoy den 8a to 3anaxrisimopoiisw
- if(val<min_val){
- first_val=-1;
- }
- if(val>max_val){
- max_val=val;
- if(i==N-1){
- last_val=0;
- }
- }
- }
- }
- }
- //run through the two stacks to find max values up
- //to a certain point and then check if the values.top()
- //satisfies the given condition
- void Find_solution(){
- //prwta elegxw to last_val
- if(last_val==0){
- out<<max_val<<'\n';
- return;
- }
- //meta ta eswterika
- int minc=values.top();
- int minprev=minc;
- values.pop();
- max_from_startpoint.pop();
- while(!values.empty()){
- minc=min(values.top(),minc);
- //out<<values.top()<<' '<<minprev<<' '<<max_from_startpoint.top()<<'\n';
- if(minprev>values.top() && max_from_startpoint.top()<values.top()){
- out<<values.top();
- break;
- }
- minprev=minc;
- values.pop();
- max_from_startpoint.pop();
- }
- if(values.empty()){
- //kai an den exw brei kati mexri edw elegxw kai to first_val
- if(first_val==0){
- out<<min_val<<'\n';
- return;
- }
- out<<"NOT FOUND";
- }
- }
- int main(){
- Read_and_find_mins_to_here();
- Find_solution();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement