Salve. Stavo provando a risolverre il problema Flappybird, ma ottengo un 25/100, poichè falliscono alcuni testcase delle ultime subtasks. Tuttavia, non riesco a capire quale sia l’errore nel mio codice. Qualcuno sa come aiutarmi?
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// input data
int N;
vector<int> A, B;
int main() {
// uncomment the following lines if you want to read/write from files
// ifstream cin("input.txt");
// ofstream cout("output.txt");
cin >> N;
A.resize(N);
B.resize(N);
for (int i=0; i<N; i++)
cin >> A[i] >> B[i];
int left = A[0];
int right = B[0];
bool isValid = true;
for( int i = 0; i < N && isValid; i++) {
//cout << left << " " << right << endl;
if (A[i] > left) {
left = A[i];
}
if (B[i] < right) {
right = B[i];
}
if(right - left < 0) {
isValid = false;
break;
}
}
if (isValid)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
Ok, ho risolto quel problema, principalmente perchè avevo letto male la traccia .
Tuttavia, le task sbagliate rimangono le stesse, il nuovo codice è questo:
// NOTE: it is recommended to use this even if you don't understand the following code.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// input data
int N;
vector<int> A, B;
int main() {
// uncomment the following lines if you want to read/write from files
// ifstream cin("input.txt");
// ofstream cout("output.txt");
cin >> N;
A.resize(N);
B.resize(N);
for (int i=0; i<N; i++)
cin >> A[i] >> B[i];
int left = A[0];
int right = B[0];
bool isValid = true;
for( int i = 1; i < N && isValid; i++) {
//cout << left << " " << right << endl;
int newLeft = A[i];
int newRight = B[i];
if ((left <= newLeft && newLeft <= right) || (left <= newRight && newRight <= right)) {
left = newLeft;
right = newRight;
} else {
isValid = false;
break;
}
}
if (isValid)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
Secondo me c’è un problema nella tua condizione.
Io invertirei la condizione e scriverei solo quando il passaggio del bird non è accettabile ovvero quando