Pagini recente » Cod sursa (job #1164239) | Arhiva de probleme | Cod sursa (job #99196) | Cod sursa (job #1759852) | Cod sursa (job #3135566)
#include <bits/stdc++.h>
using namespace std;
ifstream in("zeap.in");
ofstream out("zeap.out");
void sterge(vector<long int> &s ,long int x ){
auto it = std::find(s.begin(), s.end(), x);
if (it != s.end())
s.erase(it);
else
out << -1 << endl;
}
bool cauta(vector<long int> &s ,long int x){
for(auto aux : s)
if(aux == x)
return 1;
return 0;
}
void insereaza(vector<long int> &s ,long int x ){
if(cauta(s,x) == 0)
s.push_back(x);
}
long int max_diff(vector<long int >&s) {
if (s.size() < 2)
return -1;
int maxDiff= 0;
for (size_t i = 0; i < s.size(); i++)
for (size_t j = i + 1; j < s.size(); j++) {
long int diff = std::abs(s[i] - s[j]);
if (diff > maxDiff)
maxDiff = diff;
}
return maxDiff ;
}
int MIN_DIFF(const std::vector<long int>& s) {
if (s.size() < 2) {
return -1;
}
int minDiff = INT_MAX;
std::vector<long int> sortedS = s;
std::sort(sortedS.begin(), sortedS.end());
for (size_t i = 1; i < sortedS.size(); i++) {
int diff = std::abs(sortedS[i] - sortedS[i - 1]);
if (diff < minDiff) {
minDiff = diff;
}
}
return minDiff;
}
void afisare(vector<int> &s)
{
for(auto aux : s)
cout << aux << ' ';
cout << endl;
}
int main(){
vector<long int> s;
string t;
while(!in.eof())
{
in >> t;
if(t == "I")
{
char aux; in >> aux;
insereaza(s, int(aux - '0'));
}else if ( t == "S"){
char aux; in >> aux;
sterge(s,int(aux - '0'));
}else if (t == "C"){
char aux; in >> aux;
out << cauta(s,int(aux - '0')) << endl;
}else if (t == "MAX")
out << max_diff(s)<< endl;
else if ( t == "MIN")
out << MIN_DIFF(s) << endl;
}
return 0;
}