My code below works for the 6 cases but times out for the next 9 cases. What can I do to make my code more efficient? Please ask if you need clarifications regarding my code.
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen("cardgame.in","r",stdin);
freopen("cardgame.out","w",stdout);
cin >> n;
vector<int> elsH, elsL;
vector<int> bes1, bes2;
for (int i=0; i<n/2; i++) {
int curr;
cin >> curr;
elsH.push_back(curr);
}
for (int i=0; i<n/2; i++) {
int curr;
cin >> curr;
elsL.push_back(curr);
}
int add = 0;
for (int i=1; i<=2*n; i++) {
if (!count(elsH.begin(),elsH.end(),i)&&!count(elsL.begin(),elsL.end(),i)) {
if(add >= n/2) {
bes1.push_back(i);
add++;
} else {
bes2.push_back(i);
add++;
}
}
}
int points = 0;
for (auto x : elsH) {
auto it = upper_bound(bes1.begin(),bes1.end(),x);
if(*it && it !=bes1.end()) {
points++;
bes1.erase(it);
} else {
bes1.erase(bes1.begin());
continue;
}
}
for (auto x : elsL) {
auto it = upper_bound(bes2.begin(),bes2.end(),x);
if (it == bes2.begin()) {
bes2.pop_back();
continue;
} else {
points++;
it--;
bes2.erase(it);
}
}
cout << points;
}