Problem:
Merge Sort
Write a program of a Merge Sort algorithm implemented by the following pseudocode. You should also report the number of comparisons in the Merge function.
Merge(A, left, mid, right)
  n1 = mid - left;
  n2 = right - mid;
  create array L[0...n1], R[0...n2]
  for i = 0 to n1-1
    do L[i] = A[left + i]
  for i = 0 to n2-1
    do R[i] = A[mid + i]
  L[n1] = SENTINEL
  R[n2] = SENTINEL
  i = 0;
  j = 0;
  for k = left to right-1
    if L[i] <= R[j]
      then A[k] = L[i]
           i = i + 1
      else A[k] = R[j]
           j = j + 1

Merge-Sort(A, left, right){
  if left+1 < right
    then mid = (left + right)/2;
         call Merge-Sort(A, left, mid)
         call Merge-Sort(A, mid, right)
         call Merge(A, left, mid, right)
Input
In the first line
n
is given. In the second line,
n
integers are given.
Output
In the first line, print the sequence S. Two consequtive elements should be separated by a space character.
In the second line, print the number of comparisons.
Constraints
n ≤ 500000
0 ≤ an element in S ≤ 10
9
Sample Input 1
10
8 5 9 2 6 3 7 1 10 4
Sample Output 1
1 2 3 4 5 6 7 8 9 10
34
Notes

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

long long cnt = 0;

void merge(vector<int> &A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;

    vector<int> L(n1 + 1), R(n2 + 1);

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];

    L[n1] = 999999999;
    R[n2] = 999999999;

    int i = 0, j = 0;

    for (int k = left; k < right; k++) {
        if (L[i] < R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }
        cnt++;
    }
}

void mergeSort(vector<int> &A, int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

int main() {
    int n;
    cin >> n;

    vector<int> A(n);
    for (int i = 0; i < n; i++) cin >> A[i];

    mergeSort(A, 0, n);

    for (int i = 0; i < n; i++) {
        cout << A[i];
        if (i != n - 1) cout << " ";
    }
    cout << endl;

    cout << cnt << endl;

    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 7012
final_rank: 8

code_snippet: # include <stdio.h>
# define MAX 500000
int count;

void print_array(int array[], int array_len)
{
    for (int i = 0; i < array_len; i++)
    {
        printf("%d", array[i]);
        if (i != array_len - 1)
            printf(" ");
    }
    printf("\n");
}

void merge_sort(int A[], int left, int right)
{
    int i, j, k, mid;
    int work[MAX];

    if (left < right)
    {
        mid = (left + right) / 2;
        merge_sort(A, left, mid);
        merge_sort(A, mid + 1, right);

        for (i = mid; i >= left; i--)
            work[i] = A[i];

        for (j = mid + 1; j <= right; j++)
            work[right - (j - (mid + 1))] = A[j];

        i = left;
        j = right;
        for (k = left; k <= right; k++)
        {
            count++;
            if (work[i] < work[j])
                A[k] = work[i++];
            else
                A[k] = work[j--];
        }
    }
}

int main(void)
{
    int N;
    int A[MAX];

    scanf("%d", &N);
    for (int i = 0; i < N; i++)
        scanf("%d", &A[i]);

    merge_sort(A, 0, N - 1);
    print_array(A, N);
    printf("%d\n", count);

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 41788
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

static const long long INFTY = LLONG_MAX;
long long cnt = 0;

void merge_vec(vector<long long> &A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;

    vector<long long> L(n1 + 1), R(n2 + 1);

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];

    L[n1] = INFTY;
    R[n2] = INFTY;

    int i = 0, j = 0;

    for (int k = left; k < right; k++) {
        cnt++;
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(vector<long long> &A, int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge_vec(A, left, mid, right);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<long long> A(n);
    for (int i = 0; i < n; i++) cin >> A[i];

    mergeSort(A, 0, n);

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

    cout << cnt << "\n";

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 10688
final_rank: 3

code_snippet: #include <bits/stdc++.h>

using namespace std;

int mergeSort(int left, int right)
{
  if(left + 1 < right) {
    int mid = (left + right) >> 1;
    return (mergeSort(left, mid) + mergeSort(mid, right) + right - left);
  } else {
    return (0);
  }
}

int main()
{
  int N, A[500000];
  scanf("%d", &N);
  for(int i = 0; i < N; i++) scanf("%d", &A[i]);
  sort(A, A + N);
  for(int i = 0; i < N; i++) {
    if(i > 0) putchar(' ');
    printf("%d", A[i]);
  }
  putchar('\n');
  printf("%d\n", mergeSort(0, N));
}
accuracy: 1
time_ms: 80
memory_kb: 5436
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

const long long INF = (1LL << 60); 
long long cnt = 0;                 

void merge(vector<long long> &A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;

    vector<long long> L(n1 + 1), R(n2 + 1);

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int j = 0; j < n2; j++) R[j] = A[mid + j];

    L[n1] = INF;
    R[n2] = INF;

    int i = 0, j = 0;
    for (int k = left; k < right; k++) {
        cnt++;  
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(vector<long long> &A, int left, int right) {
    if (left + 1 < right) {          
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);     
        mergeSort(A, mid, right);    
        merge(A, left, mid, right);  
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<long long> S(n);
    for (int i = 0; i < n; i++) cin >> S[i];

    mergeSort(S, 0, n);

    for (int i = 0; i < n; i++) {
        if (i) cout << ' ';
        cout << S[i];
    }
    cout << '\n';

    cout << cnt << '\n';

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 10824
final_rank: 6

code_snippet: #include <vector>
#include <iostream>
using namespace std;

void Merge(vector<unsigned int> &Sequence, int LeftIndex, int MidIndex, int RightIndex, int &CompareCount)
{
    int LeftSequenceCount = MidIndex - LeftIndex;
    int RightSequenceCount = RightIndex - MidIndex;
    unsigned int Infty = 1000000001; //十分大きな整数値
    vector<unsigned int> LeftSequence(LeftSequenceCount + 1);
    vector<unsigned int> RightSequence(RightSequenceCount + 1);
    
    for (int i = 0; i < LeftSequenceCount; i++)
    {
        LeftSequence.at(i) =  Sequence.at(LeftIndex + i);
    }
    LeftSequence.back() = Infty;
    
    for (int i = 0; i < RightSequenceCount; i++)
    {
        RightSequence.at(i) =  Sequence.at(MidIndex + i);
    }
    RightSequence.back() = Infty;
    
    int NowLeftSequenceIndex = 0;
    int NowRightSequenceIndex = 0;
    for (int i = LeftIndex; i < RightIndex; i++)
    {
        if (LeftSequence.at(NowLeftSequenceIndex) <= RightSequence.at(NowRightSequenceIndex))
        {
            Sequence.at(i) = LeftSequence.at(NowLeftSequenceIndex);
            NowLeftSequenceIndex ++;
        }
        else
        {
            Sequence.at(i) = RightSequence.at(NowRightSequenceIndex);
            NowRightSequenceIndex ++;    
        }
        CompareCount ++;
    }
}

void MergeSort(vector<unsigned int> &Sequence, int LeftIndex, int RightIndex, int &CompareCount)
{
    if (RightIndex - LeftIndex > 1)
    {
        int MidIndex = (LeftIndex + RightIndex) / 2;
        MergeSort(Sequence, LeftIndex, MidIndex, CompareCount);
        MergeSort(Sequence, MidIndex, RightIndex, CompareCount);
        Merge(Sequence, LeftIndex, MidIndex, RightIndex, CompareCount);
    }
    else
    {
    }
}
int main(){
    int ElementCount;
    cin >> ElementCount;
    vector<unsigned int> Sequence(ElementCount);
    for (int i = 0; i < ElementCount; i++){
        cin >> Sequence.at(i);
    }
    int CompareCount = 0;
    MergeSort(Sequence, 0, ElementCount, CompareCount);
    
    for (int i = 0; i < ElementCount - 1; i++){
        cout << Sequence.at(i) << " ";
    }
    cout << Sequence.back() << "\n";
    cout << CompareCount << "\n";
    
}
accuracy: 1
time_ms: 190
memory_kb: 6984
final_rank: 15

code_snippet: #include<iostream>
using namespace std;

int count = 0;

void merge(int A[], int left, int mid, int right){
    int n1 = mid - left;
    int n2 = right - mid;
    int L[n1+1];
    int R[n2+1];
    for(int i=0;i<n1;i++){
        L[i] = A[left+i];
    }
    for(int j=0;j<n2;j++){
        R[j] = A[mid+j];
    }
    L[n1] = 1000000000;
    R[n2] = 1000000000;
    int i = 0;
    int j = 0;
    for(int k=left;k<right;k++){
        count++;
        if(L[i] <= R[j]){
            A[k] = L[i];
            i++;
        }
        else{
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(int A[], int left, int right){
    if(left+1 < right){
        int mid = (left + right)/2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}
int main(){
    int n;
    cin >> n;
    
    int S[n];
    for(int x=0;x<n;x++){
        cin >> S[x];
    }
    
    mergeSort(S, 0, n);
    
    for(int y=0;y<n;y++){
        if(y!=n-1){
            cout << S[y] << " ";
        }
        else{
            cout << S[y] << endl;
        }
    }
    
    cout << count << endl;
    
}
accuracy: 1
time_ms: 170
memory_kb: 7268
final_rank: 9

code_snippet: #include<iostream>
using namespace std;

int count = 0;

void merge(int A[], int left, int mid, int right){
    int n1 = mid - left;
    int n2 = right - mid;
    int L[n1+1];
    int R[n2+1];
    for(int i=0;i<n1;i++){
        L[i] = A[left+i];
    }
    for(int j=0;j<n2;j++){
        R[j] = A[mid+j];
    }
    L[n1] = 100000000;
    R[n2] = 100000000;
    int i = 0;
    int j = 0;
    for(int k=left;k<right;k++){
        count++;
        if(L[i] <= R[j]){
            A[k] = L[i];
            i++;
        }
        else{
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(int A[], int left, int right){
    if(left+1 < right){
        int mid = (left + right)/2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}
int main(){
    int n;
    cin >> n;
    
    int S[n];
    for(int x=0;x<n;x++){
        cin >> S[x];
    }
    
    mergeSort(S, 0, n);
    
    for(int y=0;y<n;y++){
        if(y!=n-1){
            cout << S[y] << " ";
        }
        else{
            cout << S[y] << endl;
        }
    }
    
    cout << count << endl;
    
}
accuracy: 0.9
time_ms: 150
memory_kb: 7272
final_rank: 19

code_snippet: #include<iostream>
using namespace std;

int count = 0;

void merge(int A[], int left, int mid, int right){
    int n1 = mid - left;
    int n2 = right - mid;
    int L[n1+1];
    int R[n2+1];
    for(int i=0;i<n1;i++){
        L[i] = A[left+i];
    }
    for(int j=0;j<n2;j++){
        R[j] = A[mid+j];
    }
    L[n1] = 10000000;
    R[n2] = 10000000;
    int i = 0;
    int j = 0;
    for(int k=left;k<right;k++){
        count++;
        if(L[i] <= R[j]){
            A[k] = L[i];
            i++;
        }
        else{
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(int A[], int left, int right){
    if(left+1 < right){
        int mid = (left + right)/2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}
int main(){
    int n;
    cin >> n;
    
    int S[n];
    for(int x=0;x<n;x++){
        cin >> S[x];
    }
    
    mergeSort(S, 0, n);
    
    for(int y=0;y<n;y++){
        if(y!=n-1){
            cout << S[y] << " ";
        }
        else{
            cout << S[y] << endl;
        }
    }
    
    cout << count << endl;
    
}
accuracy: 0.9
time_ms: 150
memory_kb: 7272
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

static const ll INFTY = LLONG_MAX;

long long cnt = 0; // 比較回数

void merge_vec(vector<ll> &A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;
    vector<ll> L(n1 + 1), R(n2 + 1);

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];

    L[n1] = INFTY;
    R[n2] = INFTY;

    int i = 0, j = 0;

    for (int k = left; k < right; k++) {
        cnt++;                   // 比較回数をカウント
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(vector<ll> &A, int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge_vec(A, left, mid, right);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<ll> A(n);
    for (int i = 0; i < n; i++) cin >> A[i];

    mergeSort(A, 0, n);

    // 出力
    for (int i = 0; i < n; i++) {
        if (i) cout << " ";
        cout << A[i];
    }
    cout << "\n";

    cout << cnt << "\n";

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 10804
final_rank: 5

code_snippet: #include <iostream>
#include <climits>
using namespace std;

long long cnt = 0;

void merge(int A[], int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;

    int* L = new int[n1 + 1];
    int* R = new int[n2 + 1];

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];

    L[n1] = INT_MAX;  
    R[n2] = INT_MAX;

    int i = 0, j = 0;

    for (int k = left; k < right; k++) {
        cnt++;  
        if (L[i] <= R[j]) A[k] = L[i++];
        else A[k] = R[j++];
    }

    delete[] L;
    delete[] R;
}

void mergeSort(int A[], int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

int main() {
    int n;
    cin >> n;
    int S[n];
    for (int i = 0; i < n; i++) cin >> S[i];

    mergeSort(S, 0, n);

    // ★末尾に空白を出力しない★
    for (int i = 0; i < n; i++) {
        if (i) cout << " ";
        cout << S[i];
    }
    cout << endl;

    cout << cnt << endl;
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 7116
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <limits> // for numeric_limits

long long comparisonCount = 0; // 比較回数をグローバル変数で管理

void merge(std::vector<int>& A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;

    std::vector<int> L(n1 + 1);
    std::vector<int> R(n2 + 1);

    for (int i = 0; i < n1; ++i) {
        L[i] = A[left + i];
    }
    for (int i = 0; i < n2; ++i) {
        R[i] = A[mid + i];
    }

    L[n1] = std::numeric_limits<int>::max(); // 番兵
    R[n2] = std::numeric_limits<int>::max(); // 番兵

    int i = 0;
    int j = 0;

    for (int k = left; k < right; ++k) {
        comparisonCount++; // ここで比較が行われる
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(std::vector<int>& A, int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}

int main() {
    std::ios_base::sync_with_stdio(false); // 高速化
    std::cin.tie(NULL); // 高速化

    int n;
    std::cin >> n;

    std::vector<int> S(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> S[i];
    }

    mergeSort(S, 0, n);

    for (int i = 0; i < n; ++i) {
        std::cout << S[i] << (i == n - 1 ? "" : " ");
    }
    std::cout << std::endl;

    std::cout << comparisonCount << std::endl;

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 6992
final_rank: 2

code_snippet: #include<iostream>
#include<vector>

using namespace std;

void merge(vector<int>&A,int left, int mid, int right, int&Hikaku_count){
    int n1;
    int n2;
    n1 = mid - left;
    n2 = right - mid;
    vector<int> L(n1+1);
    vector<int> R(n2+1);
    for(int i = 0; i < n1; i++){
        L[i] = A[left + i];
    }
    for(int j = 0; j < n2; j++){
        R[j] = A[mid + j];
    }
    int INFTY = 1000000000;
    L[n1] = INFTY;
    R[n2] = INFTY ; 
    int i = 0;
    int j = 0;
    for(int k = left; k < right; k++ ){
        Hikaku_count ++;
        if(L[i] <= R[j]){
            A[k] = L[i];
            i = i + 1;
        }else{
            A[k] = R[j];
            j = j+1;
        }
    }
}

void mergeSort(vector<int>&A, int left, int right, int&Hikaku_count){
    int mid;
    if(left + 1 < right){
        mid = (left + right) / 2;
        mergeSort(A, left, mid,Hikaku_count);
        mergeSort(A, mid, right,Hikaku_count);
        merge(A, left, mid, right,Hikaku_count);
    }
}

int main(){
    int x;
    cin >> x;
    vector<int>S(x);
    for(int i = 0; i < x; i++){
        int y;
        cin >> y;
        S[i] = y;
    }
    int Hikaku_count = 0;
    mergeSort(S,0,x,Hikaku_count);
    for(int i = 0; i < x; i++){
        cout << S[i];
        if(i != x-1){
            cout << " ";
        }
    }
    cout << endl;
    cout << Hikaku_count << endl;
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 6968
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<cmath>

using namespace std;

void merge(vector<int>&A,int left, int mid, int right, int&Hikaku_count){
    int n1;
    int n2;
    n1 = mid - left;
    n2 = right - mid;
    vector<int> L(n1+1);
    vector<int> R(n2+1);
    for(int i = 0; i < n1; i++){
        L[i] = A[left + i];
    }
    for(int j = 0; j < n2; j++){
        R[j] = A[mid + j];
    }
    int INFTY = 1000000000;
    L[n1] = INFTY;
    R[n2] = INFTY ; 
    int i = 0;
    int j = 0;
    for(int k = left; k < right; k++ ){
        Hikaku_count ++;
        if(L[i] <= R[j]){
            A[k] = L[i];
            i = i + 1;
        }else{
            A[k] = R[j];
            j = j+1;
        }
    }
}

void mergeSort(vector<int>&A, int left, int right, int&Hikaku_count){
    int mid;
    if(left + 1 < right){
        mid = (left + right) / 2;
        mergeSort(A, left, mid,Hikaku_count);
        mergeSort(A, mid, right,Hikaku_count);
        merge(A, left, mid, right,Hikaku_count);
    }
}

int main(){
    int x;
    cin >> x;
    vector<int>S(x);
    for(int i = 0; i < x; i++){
        int y;
        cin >> y;
        S[i] = y;
    }
    int Hikaku_count = 0;
    mergeSort(S,0,x,Hikaku_count);
    for(int i = 0; i < x; i++){
        cout << S[i];
        if(i != x-1){
            cout << " ";
        }
    }
    cout << endl;
    cout << Hikaku_count << endl;
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 6940
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int count =0;
void merge(vector<int>& A,int left,int mid,int right){
    int n1=mid-left;
    int n2=right-mid;
    vector<int> L(n1+1),R(n2+1);
    for (int i=0;i<n1;i++){
        L[i]=A[left+i];
    }
    for (int j=0;j<n2;j++){
        R[j]=A[mid+j];
    }

    L[n1]=1000000000;
    R[n2]=1000000000;
    int i=0;
    int j=0;
    for (int k=left;k<right;k++){
        if (L[i]<=R[j]){
            A[k]=L[i];
            i+=1;
        }
        else{
            A[k]=R[j];
            j+=1;
        }
        count+=1;
    }
}

void mergeSort(vector<int>& A,int left,int right){
    if (left+1<right){
        int mid = (left+right)/2;
        mergeSort(A,left,mid);
        mergeSort(A,mid,right);
        merge(A,left,mid,right);
    }
}

int main(){
    int n;
    cin >> n;
    vector<int> S(n);
    for (int i=0;i<n;i++){
        cin >> S[i];
    }

    mergeSort(S,0,n);
    for (int i=0;i<n;i++){
        if(i>0) cout <<" ";
        cout << S[i];
    }
    cout << endl << count << endl;
}
accuracy: 1
time_ms: 190
memory_kb: 7184
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

const long long INFTY = (1LL << 60);   // 番兵として十分大きい値
long long cnt = 0;                     // 比較回数

void mergeVec(vector<long long> &A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;

    vector<long long> L(n1 + 1), R(n2 + 1);

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];

    L[n1] = INFTY;   // 番兵
    R[n2] = INFTY;   // 番兵

    int i = 0, j = 0;

    for (int k = left; k < right; k++) {
        cnt++;  // ★ 比較回数 L[i] <= R[j] を数える
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }
    }
}

void mergeSort(vector<long long> &A, int left, int right) {
    if (left + 1 < right) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        mergeVec(A, left, mid, right);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<long long> A(n);
    for (int i = 0; i < n; i++) cin >> A[i];

    mergeSort(A, 0, n);

    // 整列済みの数列を出力
    for (int i = 0; i < n; i++) {
        if (i) cout << " ";
        cout << A[i];
    }
    cout << "\n";

    // 比較回数
    cout << cnt << "\n";

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 10792
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <limits>

using namespace std;

vector<int> L, R;

int merge(vector<int>& A,int left,int mid,int right, int& cnt){
    const int INF = numeric_limits<int>::max();
    int n1 = mid - left;
    int n2 = right - mid;

    for (int i=0;i<n1;i++){
        L[i] = A[left+i];
    }
    for (int j=0;j<n2;j++){
        R[j] = A[mid+j];
    }
    L[n1] = INF;
    R[n2] = INF;

    int i = 0;
    int j = 0;
    for (int k=left;k<right;k++){
        cnt ++;
        if (L[i]<=R[j]){
            A[k] = L[i];
            i ++;
        } else {
            A[k] = R[j];
            j ++;
        }
    }
    return 0;
}

int mergeSort(vector<int>& A,int left,int right, int& cnt){
    if (left+1 < right){
        int mid = (left + right)/2;
        mergeSort(A, left, mid, cnt);
        mergeSort(A, mid, right, cnt);
        merge(A, left, mid, right, cnt);
    }
    return 0;
}

int main(){
    int n;
    cin >> n;
    int a;
    vector<int> A(n);
    L.resize(n);
    R.resize(n);
    for (int i=0;i<n;i++){
        cin >> a;
        A[i] = a;
    }
    int cnt = 0;
    mergeSort(A,0,n,cnt);
    for (int i=0;i<n;i++){
        if (i > 0){
        cout << " " << A[i];
        } else {
            cout << A[i];
        }
    }
    cout << endl;
    cout << cnt << endl;
}
accuracy: 1
time_ms: 170
memory_kb: 8920
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
using namespace std;

vector<int> L, R;

int merge(vector<int>& A,int left,int mid,int right, int& cnt){
    int n1 = mid - left;
    int n2 = right - mid;

    for (int i=0;i<n1;i++){
        L[i] = A[left+i];
    }
    for (int j=0;j<n2;j++){
        R[j] = A[mid+j];
    }
    L[n1] = 1000000;
    R[n2] = 1000000;

    int i = 0;
    int j = 0;
    for (int k=left;k<right;k++){
        cnt ++;
        if (L[i]<=R[j]){
            A[k] = L[i];
            i ++;
        } else {
            A[k] = R[j];
            j ++;
        }
    }
    return 0;
}

int mergeSort(vector<int>& A,int left,int right, int& cnt){
    if (left+1 < right){
        int mid = (left + right)/2;
        mergeSort(A, left, mid, cnt);
        mergeSort(A, mid, right, cnt);
        merge(A, left, mid, right, cnt);
    }
    return 0;
}

int main(){
    int n;
    cin >> n;
    int a;
    vector<int> A(n);
    L.resize(n);
    R.resize(n);
    for (int i=0;i<n;i++){
        cin >> a;
        A[i] = a;
    }
    int cnt = 0;
    mergeSort(A,0,n,cnt);
    for (int i=0;i<n;i++){
        if (i > 0){
        cout << " " << A[i];
        } else {
            cout << A[i];
        }
    }
    cout << endl;
    cout << cnt << endl;
}
accuracy: 0.9
time_ms: 140
memory_kb: 8720
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
using namespace std;
int count = 0;
void merge(vector<int> &A, int left, int mid, int right)
{
    int n1 = mid - left;
    int n2 = right - mid;
    vector<int> L(n1 + 1), R(n2 + 1);
    for (int i = 0; i < n1; i++)
    {
        L[i] = A[left + i];
    }
    for (int i = 0; i < n2; i++)
    {
        R[i] = A[mid + i];
    }
    L[n1] = 1000000000;
    R[n2] = 1000000000;
    int i = 0;
    int j = 0;
    for (int k = left; k < right; k++)
    {
        count++;
        if (L[i] <= R[j])
        {
            A[k] = L[i];
            i++;
        }
        else
        {
            A[k] = R[j];
            j++;
        }
    }
}
void mergeSort(vector<int> &A, int left, int right)
{
    if (left + 1 < right)
    {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
}
int main()
{
    int n;
    cin >> n;
    vector<int> S(n);
    for (int i = 0; i < n; i++)
    {
        cin >> S[i];
    }
    mergeSort(S, 0, n);
    for (int i = 0; i < n - 1; i++)
    {
        cout << S[i] << " ";
    }
    cout << S[n - 1] << endl;
    cout << count << endl;
}
accuracy: 1
time_ms: 180
memory_kb: 7156
final_rank: 14

code_snippet: #include <iostream>
using namespace std;

long long _count = 0;

void merge_sort(long long S[], long long n, long long left, long long right){
    // printf("left: %lld, right: %lld\n", left, right);
    if(left==right) return;
    long long mid = (left + right) /2;
    // cout << mid << '\n';
    merge_sort(S, n, left, mid);
    merge_sort(S, n, mid+1, right);

    // printf("Re left: %lld, right: %lld\n", left, right);
    
    long long n_1 = mid-left+1;
    long long n_2 = right-mid;
    // printf("n_1=%lld, n_2=%lld\n", n_1, n_2);
    long long L_1[n_1+1];
    long long L_2[n_2+1];
    for(long long i=0; i<n_1; i++){
        L_1[i] = S[i+left];
        // printf("L_1[%lld]: %lld", i, L_1[i]);printf("\n");
    }
    L_1[n_1] = 0x7fffffffffffffff;
    for(long long i=0; i<n_2; i++){
        L_2[i] = S[i+mid+1];
        // printf("L_2[%lld]: %lld", i, L_2[i]);printf("\n");
    }
    L_2[n_2] = 0x7fffffffffffffff;
    

    long long j=0, k=0;
    // cout << L_1[0] << "\t" << L_2[0] << "\n";
    for(long long i=0; i<right-left+1; ++i){
        if(L_1[j]<L_2[k]){
            S[i+left] = L_1[j];
            // cout << "S[" << i+left << "] = " << S[i+left] << "\n";
            ++j;
        }
        else{
            S[i+left] = L_2[k];
            // cout << "S[" << i+left << "] = " << S[i+left] << "\n";
            ++k;
        }
        ++_count;
    }
    return;
    
}



int main(){

    long long n;
    cin >> n;
    long long S[n];
    for(long long i=0; i<n; ++i) cin >> S[i];
    merge_sort(S, n, 0, n-1);
    for(long long i=0; i<n-1; ++i){
        cout << S[i] << ' ';
    }
    cout << S[n-1] << endl;
    cout << _count << endl;
    
    
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 11392
final_rank: 7