Cod sursa(job #2968940)

Utilizator speedy_gonzalesDuca Ovidiu speedy_gonzales Data 22 ianuarie 2023 13:32:09
Problema Interclasari Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <stack>
#include <unordered_map>

using namespace std;

ifstream fin("interclasari.in");
ofstream fout("interclasari.out");

//6
//0 7 8 11 12 15
//2
//6 12
void interclasare(int a[], int b[], int n, int m) {
    if (m == 0) {
        return;
    }

    int i = 0, j = 0;
    int temp[n + m + 1], k = 0;
    while (i < n && j < m) {
        if (a[i] > b[j]) {
            temp[k] = b[j];
            ++k;
            ++j;
        } else {
            temp[k] = a[i];
            ++k;
            ++i;
        }
    }
    while (i < n) {
        temp[k] = a[i];
        ++k;
        ++i;
    }

    while (j < m) {
        temp[k] = b[j];
        ++k;
        ++j;
    }

    for (int z = 0; z < k; ++z) {
        a[z] = temp[z];
    }
}

int main() {
    int k;
    fin >> k;

    int ans[2000001];
    int photo = 0;

    for (int z = 0; z < k; ++z) {
        int n;
        fin >> n;
        photo += n;

        int temp[n + 1];
        for (int i = 0; i < n; ++i) {
            fin >> temp[i];
        }

        interclasare(ans, temp, photo - n, n);
    }
    fout << photo << "\n";

    for (int i = 0; i < photo; ++i) {
        fout << ans[i] << " ";
    }

    fin.close();
    fout.close();
    return 0;
}