Pagini recente » Cod sursa (job #1008876) | Cod sursa (job #463456) | Cod sursa (job #1551272) | Cod sursa (job #2782046) | Cod sursa (job #786052)
Cod sursa(job #786052)
//============================================================================
// Name : COding.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <climits>
using namespace std;
struct elementInfo{
int position;
int value;
};
void put_into_vector( ifstream& ifs, vector<int>& v )
{
string s;
getline( ifs, s );
istringstream iss( s );
// not the fastest method ...
copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter( v ) );
}
void process(char* filePath){
ifstream indata;
stack<struct elementInfo> orderedElements;
indata.open(filePath);
if(!indata){
cerr << "Error: file could not be opened" <<endl;
exit(1);
}
vector<int> numbers;
put_into_vector(indata , numbers);
int N = numbers[0];
int K = numbers[1];
int length = N-K+1;
vector<int> secv;
put_into_vector(indata , secv);
for (int index = 0; index < length; index ++){
// cout << "iteration " << index << endl;
if(index == 0)
for(int i = index; i< index + K; i ++ ){
// check whether the current element is smaller
// than the last element in the stack
while(!orderedElements.empty() &&
secv[i] < orderedElements.top().value
){
// cout << "removing " << orderedElements.top().value << endl;
orderedElements.pop(); // remove the last element
}
struct elementInfo newStruct;
newStruct.value = secv[i];
newStruct.position = i;
orderedElements.push(newStruct);
// cout <<" adding element " << secv[i] << endl;
}
else{
int location = K + index - 1;
// cout << "Index " << index << " Stack size " << orderedElements.size() << endl;
// check whether the current element is smaller
// than the last element in the stack
while(!orderedElements.empty() &&
secv[location] < orderedElements.top().value
){
if(orderedElements.top().position <= index)
break;
// cout << "removing " << orderedElements.top().value << endl;
orderedElements.pop(); // remove the last element
}
struct elementInfo newStruct;
newStruct.value = secv[location];
newStruct.position = location;
orderedElements.push(newStruct);
// cout <<" adding element " << secv[location] << endl;
}
// cout <<endl;
}
int minStartPos = 0;
struct elementInfo maxEl;
maxEl.value = INT_MIN;
maxEl.position = 0;
while(!orderedElements.empty()){
struct elementInfo element = orderedElements.top();
if(element.position > N -K +1){
orderedElements.pop();
continue;
}
if (element.value > maxEl.value){
maxEl.value = element.value;
maxEl.position = element.position;
}
orderedElements.pop();
}
// cout << endl;
int maxValue = maxEl.value;
int start = maxEl.position + 1;
int end = maxEl.position + K;
// store the results
freopen("secventa.out","w",stdout);
printf("%d %d %d",start,end,maxValue);
}
int main(int argc, char* argv[]) {
process("secventa.in");
return 0;
}