1353B - Two Arrays And Swaps - CodeForces Solution


greedy sortings *800

Please click on ads to support us..

Python Code:

for t in range(int(input())):
    n,k=map(int,input().split())
    l=[int(x) for x in input().split()]
    m=[int(x) for x in input().split()]
    for x in range(k):
        a,b=min(l),max(m)
        if a==b:
            break
        elif a<b:
            i,j=l.index(a),m.index(b)
            l[i],m[j]=m[j],l[i]
    print(sum(l))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define fofejo ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
 
int main()
{
 
    fofejo;
    
    int t;

    cin >> t;

    while(t--) {
        int n,k;
        cin >> n >> k;
        int a[n],b[n];
        
        int a_index,b_index;
        for(int i = 0; i<n; i++) {
            cin >> a[i];
            }
        
        for(int j = 0; j<n; j++) {
            cin >> b[j];
        }

        while(k--){
            int smallest = 31;
            int largest = 0;
            for(int i = 0; i<n; i++) {
                if(a[i]<smallest) {
                    smallest = a[i];
                    a_index = i;
                    
                }
                if(b[i] > largest) {
                    largest = b[i];
                    b_index = i;
                }
            }

            if(smallest < largest) {
                int temp;
                temp = a[a_index];
                a[a_index] = b[b_index];
                b[b_index] = temp;

            }


        }

        int sum=0;

        for(int i=0; i<n; i++) {
            sum += a[i];
        }
        
        cout << sum << "\n";

        



    }

        
    }

    

    


Comments

Submit
0 Comments
More Questions

1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion