Algorithm


Problem Name: beecrowd | 2812

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2812

Laércio

 

By Felipe C. Ochial, URI BR Brazil

Timelimit: 1

Armandinho has a bit boring friend , named Laércio. When they play the game of master says, a game where someone gives an order and someone fulfills it, and instead of giving nice orders like climbing a tree, jumping the wall, doing a handstand or organizing manifestations to overthrow the government (regardless of who is in power) he always asks for something dull. In his last game, Laércio demanded that Armandinho order a list of numbers, so that only the odd numbers appear and the first item is the largest, the second is the smallest, the third is the second largest, the fourth is the second smaller, and so on. As doing this by hand is very annoying, Armandinho sought your help.

 

Input

 

The input consists of an integer N representing the number of test cases ( 1<N<1000 ). Each test case begins with an integer M, which represents the size of the list (0<M<100). followed by M integers Mi (0<Mi < 1000) that represent Laércio's list.

 

Output

 

Print the ordered list as Laércio requested, with a space between the values, breaking a line after each test case.

 

 

 

Input Sample Output Sample

3

10

1 2 3 4 5 6 7 8 9 10

7

2 4 6 8 10 12 14

4

9 7 77 63

9 1 7 3 5


77 7 63 9

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdlib.h>

#define true 1
#define false 0

int compara(unsigned *, unsigned *);
int compara2(unsigned *, unsigned *);

int main (int argc, char **argv)
{
	
	unsigned i, j, tmp;
	unsigned n, c, k, z;

	scanf("%u", &c);

	while(c--)
	{

		scanf("%u", &n);

		unsigned vet1[n];
		unsigned vet2[n];
		for (i = 0, j = 0; i  <  n; ++i)
		{

			scanf("%u", &tmp);

			if (tmp % 2 != 0)
				vet1[j] = vet2[j] = tmp, ++j;

		}

		qsort(vet1, j, sizeof(unsigned), compara);
		qsort(vet2, j, sizeof(unsigned), compara2);

		k = z = 0;
		_Bool flag = false;
		for (i = 0; i  <  j; ++i)
		{
			
			if (i % 2 == 0)
				if (flag)
					printf(" %u", vet2[k++]);
				else
					printf("%u", vet2[k++]), flag = true;
			else
				if (flag)
					printf(" %u", vet1[z++]);
				else
					printf("%u", vet1[z++]), flag = true;

		}

		printf("\n");

	}

	return 0;

}

int compara(unsigned *a, unsigned *b)
{

	return *a - *b;

}

int compara2(unsigned *a, unsigned *b)
{

	return *b - *a;

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 10 1 2 3 4 5 6 7 8 9 10 7 2 4 6 8 10 12 14 4 9 7 77 63

Output

x
+
cmd
9 1 7 3 5 77 7 63 9

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n, m, x, in, de;
    cin >> n;
    vector<int>a;
    for (int i = 0; i  <  n; ++i)
    {
        cin >> m;
        vector<int>v;
        for (int j = 0; j  <  m; ++j)
        {
            cin >> x;
            if (x%2!=0)
                v.push_back(x);
        }
        sort(v.begin(), v.end());
        in=0;
        de=v.size()-1;
        for (int i = 0; i  <  v.size(); ++i)
        {
            if (i%2==0)
                cout << v[de--];
            else
                cout << v[in++];
            if (i!=v.size()-1)
                cout << " ";
        }
        cout << endl;
    }

    return 0;
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
3 10 1 2 3 4 5 6 7 8 9 10 7 2 4 6 8 10 12 14 4 9 7 77 63

Output

x
+
cmd
9 1 7 3 5 77 7 63 9

#3 Code Example with Java Programming

Code - Java Programming


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
       Scanner leitor = new Scanner(System.in);
		int N = leitor.nextInt();
		for (int i = 0; i  <  N; i++) {
			int M = leitor.nextInt();
			ArrayList < Integer> Mi = new ArrayList<>();
			for (int j = 0; j  <  M; j++) {
				int temp = leitor.nextInt();
				if (temp % 2 == 1) {
					Mi.add(temp);
				}
			}
			Collections.sort(Mi);
			String str = "";
			if (Mi.size() % 2 != 0) {
				int cont = 0;
				for (int j = 0; cont != Mi.size(); j++) {
					if (j  <  Mi.size() / 2) {
						str += (Mi.get(Mi.size() - 1 - j) + " " + Mi.get(j) + " ");
						cont += 2;
					} else {
						str += (Mi.get(j));
						cont++;
					}
				}
			} else {
				for (int j = 0; j  <  Mi.size() / 2; j++) {
					if (j + 1 < Mi.size() / 2)
						str += (Mi.get(Mi.size() - 1 - j) + " " + Mi.get(j) + " ");
					else
						str += (Mi.get(Mi.size() - 1 - j) + " " + Mi.get(j));
				}
			}
			System.out.println(str.trim());
		}
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 10 1 2 3 4 5 6 7 8 9 10 7 2 4 6 8 10 12 14 4 9 7 77 63

Output

x
+
cmd
9 1 7 3 5 77 7 63 9

#4 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};

const cases = parseInt(prompt());

for (let i = 0; i  <  cases; i++) {
  prompt();
  var sequence = prompt().split(" ").map(Number);
  var odds = [];
  var oddsSorted = [];

  for (let item of sequence) {
    if (item % 2 == 1) {
      odds.push(item);
    }
  }

  for (let aux; odds.length > 0; ) {
    if (oddsSorted.length % 2 == 0) {
      aux = Math.max(...odds);
    } else {
      aux = Math.min(...odds);
    }

    oddsSorted.push(aux);
    odds.splice(odds.indexOf(aux), 1);
  }

  console.log(oddsSorted.join(" "));
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 10 1 2 3 4 5 6 7 8 9 10 7 2 4 6 8 10 12 14 4 9 7 77 63
Advertisements

Demonstration


Previous
#2808 Beecrowd Online Judge Solution 2808 Knights Again Solution in C, C++, Java, Js and Python
Next
#2813 Beecrowd Online Judge Solution 2813 Avoiding Rain Solution in C, C++, Java, Js and Python