https://codeforces.me/edu/course/2/lesson/9/3/practice/contest/307094/problem/D↵
Can someone pls have a look on my approach and tell me whats wrong exactly its giving WA on test 3 and the test case if not visible also .↵
This is my code for it:↵
#include <bits/stdc++.h>↵
using namespace std;↵
typedef long long ll;↵
int main() {↵
ll l1;↵
cin >> l1;↵
vector<ll> v1(l1);↵
for (ll i = 0; i < l1; i++) cin >> v1[i];↵
sort(v1.begin(),v1.end());↵
ll l2;↵
cin >> l2;↵
vector<ll> v2(l2);↵
for (ll i = 0; i < l2; i++) cin >> v2[i];↵
sort(v2.begin(),v2.end());↵
ll l3;↵
cin >> l3;↵
vector<ll> v3(l3);↵
for (ll i = 0; i < l3; i++) cin >> v3[i];↵
sort(v3.begin(),v3.end());↵
ll l4;↵
cin >> l4;↵
vector<ll> v4(l4);↵
for (ll i = 0; i < l4; i++) cin >> v4[i];↵
sort(v4.begin(),v4.end());↵
ll diff = LLONG_MAX;↵
ll ans1 = -1, ans2 = -1, ans3 = -1, ans4 = -1;↵
ll p1 = 0, p2 = 0, p3 = 0, p4 = 0;↵
while (p1 < l1 && p2 < l2 && p3 < l3 && p4 < l4) {↵
vector<pair<ll, ll>> temp;↵
temp.push_back({v1[p1], 1});↵
temp.push_back({v2[p2], 2});↵
temp.push_back({v3[p3], 3});↵
temp.push_back({v4[p4], 4});↵
sort(temp.begin(), temp.end()); // Sort based on values and pair up correspoding pointers with the values to access the pointer of smallest element ↵
ll currdiff = temp[3].first — temp[0].first;↵
if (currdiff < diff) {↵
diff = currdiff;↵
ans1 = temp[0].first, ans2 = temp[1].first, ans3 = temp[2].first, ans4 = temp[3].first;↵
}↵
//any time at any point just increase the pointer corresponding to least element ↵
if (temp[0].second == 1) p1++;↵
else if (temp[0].second == 2) p2++;↵
else if (temp[0].second == 3) p3++;↵
else if (temp[0].second == 4) p4++;↵
}↵
cout << ans1 << " " << ans2 << " " << ans3 << " " << ans4 << endl;↵
return 0;↵
} https://codeforces.me/edu/course/2/lesson/9/3/practice/contest/307094/submission/304275765↵
Can someone pls have a look on my approach and tell me whats wrong exactly its giving WA on test 3 and the test case if not visible also .↵
This is my code for it:
#include <bits/stdc++.h>↵
using namespace std;↵
typedef long long ll;↵
int main() {↵
ll l1;↵
cin >> l1;↵
vector<ll> v1(l1);↵
for (ll i = 0; i < l1; i++) cin >> v1[i];↵
sort(v1.begin(),v1.end());↵
ll l2;↵
cin >> l2;↵
vector<ll> v2(l2);↵
for (ll i = 0; i < l2; i++) cin >> v2[i];↵
sort(v2.begin(),v2.end());↵
ll l3;↵
cin >> l3;↵
vector<ll> v3(l3);↵
for (ll i = 0; i < l3; i++) cin >> v3[i];↵
sort(v3.begin(),v3.end());↵
ll l4;↵
cin >> l4;↵
vector<ll> v4(l4);↵
for (ll i = 0; i < l4; i++) cin >> v4[i];↵
sort(v4.begin(),v4.end());↵
ll diff = LLONG_MAX;↵
ll ans1 = -1, ans2 = -1, ans3 = -1, ans4 = -1;↵
ll p1 = 0, p2 = 0, p3 = 0, p4 = 0;↵
while (p1 < l1 && p2 < l2 && p3 < l3 && p4 < l4) {↵
vector<pair<ll, ll>> temp;↵
temp.push_back({v1[p1], 1});↵
temp.push_back({v2[p2], 2});↵
temp.push_back({v3[p3], 3});↵
temp.push_back({v4[p4], 4});↵
sort(temp.begin(), temp.end()); // Sort based on values and pair up correspoding pointers with the values to access the pointer of smallest element ↵
ll currdiff = temp[3].first — temp[0].first;↵
if (currdiff < diff) {↵
diff = currdiff;↵
ans1 = temp[0].first, ans2 = temp[1].first, ans3 = temp[2].first, ans4 = temp[3].first;↵
}↵
//any time at any point just increase the pointer corresponding to least element ↵
if (temp[0].second == 1) p1++;↵
else if (temp[0].second == 2) p2++;↵
else if (temp[0].second == 3) p3++;↵
else if (temp[0].second == 4) p4++;↵
}↵
cout << ans1 << " " << ans2 << " " << ans3 << " " << ans4 << endl;↵
return 0;↵
}