Imi poate spune si mie cineva ce e gresit in codul meu ?
Nu inteleg care ar putea fi problema (poate afisarea pe rand nou a solutiilor?...).
Multumesc anticipat
PS: Rezultatele par a fi ok din ceea ce am vazut eu ca si teste..
PS2: Ar putea cineva sa imi puna la dispozitie testul oficial (sau alte teste pe care sa le incerc) ?
#include <fstream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int n, T;
ifstream in("linterv.in");
ofstream out("linterv.out");
class Interval{
long long a, b;
public:
inline long long getA() const { return a; }
inline long long getB() const { return b; }
friend bool operator<(const Interval&, const Interval&);
friend istream& operator>>(istream&, Interval&);
};
bool operator<(const Interval& x, const Interval& y){
return x.getA() < y.getA();
}
istream& operator>>(istream& is, Interval& inter){
is >> inter.a >> inter.b;
return is;
}
void readData(vector<Interval>& Io, Interval& inter){
Io.clear();
if (in && !in.eof()){
in >> n;
}
for (int i = 0; i < n; i++){
in >> inter;
Io.push_back(inter);
}
}
void processAndWrite(vector<Interval>& Io){
int L = 0, X = INT_MIN;
sort(Io.begin(), Io.end());
size_t i = Io.size()-1;
for (int j = 0; j < i; j++){
if (Io[j].getA() >= X){
L += Io[j].getB() - Io[j].getA();
X = Io[j].getB();
}
else
if (Io[j].getA() < X && Io[j].getB() <= X){
continue;
}
else
if (Io[j].getA() < X && Io[j].getB() > X){
L += Io[j].getB() - X;
X = Io[j].getB();
}
}
out << L << "\n";
}
int main(int argc, char *argv[]){
vector<Interval> Int;
Interval inter;
in >> T;
do{
readData(Int,inter);
processAndWrite(Int);
T--;
}while (T > 0);
return 0;
}