Pagini recente » Cod sursa (job #204659) | Cod sursa (job #40443) | Cod sursa (job #206014) | Cod sursa (job #300503) | Cod sursa (job #1316104)
#include <stdio.h>
#define MAXN 20000000
int v[MAXN];
inline void cobor(int x, int n){
int best, aux;
char bun = 1;
while((x << 1) <= n && bun){
best = x;
if(v[x << 1] > v[best])
best = (x << 1);
if((x << 1) + 1 <= n)
if(v[(x << 1) + 1] > v[best])
best = (x << 1) + 1;
if(best == x)
bun = 0;
aux = v[best];
v[best] = v[x];
v[x] = aux;
x = best;
}
}
inline void heapify(int n){
int i;
for(i = n >> 1; i >= 0; i--){
cobor(i, n);
}
}
inline void heapsort(int n){
heapify(n);
int aux;
for(; n > 0; n--){
aux = v[n];
v[n] = v[0];
v[0] = aux;
cobor(0, n - 1);
}
}
int main(){
FILE *in = fopen("interclasari.in", "r");
int t, n = 0, x, i, j = 0;
fscanf(in, "%d", &t);
for(i = 0; i < t; i++){
fscanf(in, "%d", &x);
n += x;
for(; j < n; j++){
fscanf(in, "%d", &v[j]);
}
}
fclose(in);
heapsort(n - 1);
FILE *out = fopen("interclasari.out", "w");
fprintf(out, "%d\n", n);
for(i = 0; i < n; i++){
fprintf(out, "%d ", v[i]);
}
fclose(out);
return 0;
}