Thank you for finding my mistake
I should have been careful in using pointers
Thank you for finding my mistake
I should have been careful in using pointers
#include <bits/stdc++.h>#define MAX 100#define pb(x) push_back(x)#define read() freopen("listin.txt","r",stdin)#define write() freopen("listout.txt","w",stdout)#define valid(x,y) (x >= 0 && x < n) && (y >= 0 && y < m)using namespace std;typedef long long int lli;lli skew(lli *a,lli *b,lli n,lli k){ lli t;for(lli i=1;i<=k;i++){if(a[n-1]<b[0]){ t=a[n-1];a[n-1]=b[0];b[0]=t;sort(a, a+n, greater<lli>());sort(b, b+n, greater<lli>());}}return a[0]+b[0];}int main(){lli n,k;cin>>n>>k;lli a[n],b[n];for(lli i=0;i<n;i++){cin>>a[i];}for(lli i=0;i<n;i++){cin>>b[i];}sort(a, a+n, greater<lli>());sort(b, b+n, greater<lli>());cout<<min(skew(a,b,n,k),skew(b,a,n,k))<<endl;return 0;}
In this question i was using an approach of sorting the arrays and then adding the first elements of both arrays , after checking if the last element of one is less than the first element of the other and then swapping them.
while in my first attempt i had not considered the minimum values obtained due to calling the function in 2 diffrent ways i got wa in cases
5,11,12,15.
and after considering minimum value i got wa 1,2,6,13,14,15.
Accepted
Turns out that my comparison condition needed to be changed...
But I would really appreciate your opinions regarding the code and how can I do it better
The IARCS site shows that one of the test cases failed . but I am unable to figure out the fault in my solution ... please help.
#include <iostream>#include <bits/stdc++.h>#define MAX 100#define pb(x) push_back(x)#define read() freopen("addin.txt","r",stdin)#define write() freopen("addout.txt","w",stdout)using namespace std;typedef long long int lli;int main(){lli p,q,r,i;cin>>p>>q>>r;lli a[p+q+r],b[p+q+r],c[p+q+r];for(i=0;i<p+q+r;i++)cin>>a[i];lli n = sizeof(a)/sizeof(a[0]);sort(a, a+n);lli k=0,j=0;for(i=0;i<p+q+r-2;i++){b[k]=0;if(a[i]==a[i+1]){c[j]=a[i];j++;b[k]++;if(i<(p+q+r-2)&&a[i+1]==a[i+2]){b[k]++;k++;i++;}elsek++;}}cout<<k<<endl;for(i=0;i<j;i++)cout<<c[i]<<endl;}