Skip to content

Instantly share code, notes, and snippets.

@brun0xff
Created January 22, 2017 05:36
Show Gist options
  • Save brun0xff/2a6f95a7507a734a5c7a284f5b0cddeb to your computer and use it in GitHub Desktop.
Save brun0xff/2a6f95a7507a734a5c7a284f5b0cddeb to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
typedef struct elem{
int value;
int acc;
}tipoElem;
void printVet(int v[], int n){
for (int i = 0; i < n; i++) {
cout << v[i] << " ";
}
cout << endl;
}
void printVetElem(tipoElem v[], int n){
for (int i = 0; i < n; i++) {
cout << v[i].value << "," << v[i].acc << " ";
}
cout << endl;
}
int partition(int a[], int l, int r) {
int pivot, i, j, t;
pivot = a[l];
i = l; j = r+1;
while(1){
do ++i; while( a[i] >= pivot && i <= r );
do --j; while( a[j] < pivot );
if( i >= j ) break;
t = a[i]; a[i] = a[j]; a[j] = t;
}
t = a[l]; a[l] = a[j]; a[j] = t;
return j;
}
void quickSort(int a[], int l, int r) {
int j;
if(l < r ){
j = partition( a, l, r);
quickSort(a, l, j-1);
quickSort(a, j+1, r);
}
}
int partitionM(int a[], int l, int r) {
int pivot, i, j, t;
pivot = a[l];
i = l; j = r+1;
while(1){
do ++i; while( a[i] <= pivot && i <= r );
do --j; while( a[j] > pivot );
if( i >= j ) break;
t = a[i]; a[i] = a[j]; a[j] = t;
}
t = a[l]; a[l] = a[j]; a[j] = t;
return j;
}
void quickSortM(int a[], int l, int r) {
int j;
if(l < r ){
j = partitionM( a, l, r);
quickSortM(a, l, j-1);
quickSortM(a, j+1, r);
}
}
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partitionElem (tipoElem arr[], int low, int high)
{
int pivot = arr[high].acc; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j].acc >= pivot)
{
i++; // increment index of smaller element
swap(&arr[i].acc, &arr[j].acc);
swap(&arr[i].value, &arr[j].value);
}
}
swap(&arr[i + 1].acc, &arr[high].acc);
swap(&arr[i + 1].value, &arr[high].value);
return (i + 1);
}
void quickSortElem(tipoElem arr[], int low, int high)
{
if (low < high)
{
int pi = partitionElem(arr, low, high);
quickSortElem(arr, low, pi - 1);
quickSortElem(arr, pi + 1, high);
}
}
void solve(int v[], int ini, int end){
quickSort(v, ini, end);
tipoElem vp[end+1];
int value = v[0], acc = 1, aux = 0, i = 0;
// cout << "quickSort: ";
// printVet(v, end+1);
while (i < end) { /* i < n - 1 */
if(v[i] == v[i+1]){
acc++;
}
else{
vp[aux].value = value;
vp[aux].acc = acc;
value = v[i+1];
aux++;
acc = 1;
}
i++;
}
vp[aux].value = value;
vp[aux].acc = acc;
// cout << "VP: ";
// printVetElem(vp, aux+1);
quickSortElem(vp, 0, aux);
// cout << "Sort: ";
// printVetElem(vp, aux+1);
int second = vp[1].acc;
int x = 1, cont = 0;
while(second == vp[x].acc && cont < aux){
cont++; x++;
}
for (int i = 0; i < cont; i++) {
v[i] = vp[i+1].value;
}
quickSortM(v, 0, cont-1);
// cout << "Resp: ";
printVet(v, cont);
// cout << "\n";
}
int main(int argc, char const *argv[]) {
int n, m;
while(cin >> n >> m && n != 0 && m != 0){
int v[n*m];
for (int i = 0; i < n*m; i++) {
cin >> v[i];
}
solve(v, 0, (n*m)-1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment