demo_neel's blog

By demo_neel, history, 23 months ago, In English

This was my code of problem A : Koxia and Whiteboards Can some one say what is wrong with my code?

include

include

include

include

include

using namespace std;

define ll long long int

const unsigned int mod = 1e9+7;

int main(){

ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//this is fast I/O (inputput output) use header file <cstdio>
ll t;cin>>t;
while(t--){
    ll n,m; cin>>n>>m;
    vector<ll>a(n),b(m);
    for(int i=0; i<n; i++) cin>>a[i];
    for(int i=0; i<m; i++) cin>>b[i];
    sort(a.begin(),a.end());
    sort(b.begin(),b.end(),greater<int>());
    if(m==1){
        ll sum = b[0];
        for(int i=1; i<n; i++) sum+=a[i];
        cout<<sum<<endl;
        continue;
    }
    if(n>m){                
        // no problem we 
        int flag = 0;
        for(int i=0; i<m; i++){
            if(a[i]<b[i]) {
                flag = 1;           // there is atleast 1 swap
                a[i]=b[i];
            }
        }
        if(flag == 0)       // if there is no swap (i.e all elements of b are smaller than a)
            a[0]=b[0];
    }
    else{
        // here m>n
        int flag = 0;
        for(int i=0; i<n; i++){
            if(a[i]<b[i]) {
                flag = 1;           //there is atleast 1 swap
                a[i]=b[i];
            }
        }
        if(flag == 0)             //if there is no swap(i.e all element of b are smaller than a)
            a[0]=b[0];
    }
    ll sum = 0;
    for(int i=0; i<n; i++) sum+=a[i];
    cout<<sum<<endl;
}
return 0;

}

  • Vote: I like it
  • -9
  • Vote: I do not like it

| Write comment?
»
23 months ago, # |
Rev. 4   Vote: I like it +8 Vote: I do not like it

please, do use a spoiler as well as the block feature.
this is how you do it:

put your code inside a spoiler

countless many have been downvoted for posting unreadable code and expecting others to give a answer to that. Also please look at the editorial before you ask.