Pagini recente » Cod sursa (job #1277976) | Cod sursa (job #1349517) | Cod sursa (job #2124054) | Cod sursa (job #944417) | Cod sursa (job #1263332)
#include <stdio.h>
#define MAXIN 100
int h[2][MAXIN], p[2][MAXIN], ch[2][MAXIN], v[2][MAXIN];
inline void intersch(int poz, int a, int b){
int aux;
aux = h[poz][a];
h[poz][a] = h[poz][b];
h[poz][b] = aux;
aux = p[poz][a];
p[poz][a] = p[poz][b];
p[poz][b] = aux;
}
inline void cobor(int poz, int x, int dr){
int best, g = 1;
while(x * 2 < dr && g){
best = x;
g = 0;
if(h[poz][x * 2] * v[poz][p[poz][x * 2]] > h[poz][best] * v[poz][p[poz][best]])
best = x * 2;
if(x * 2 + 1 < dr){
if(h[poz][x * 2 + 1] * v[poz][p[poz][x * 2 + 1]] > h[poz][best] * v[poz][p[poz][best]])
best = x * 2 + 1;
}
intersch(poz, x, best);
if(x != best)
g = 1;
x = best;
}
}
inline void urc(int poz, int x){
while(h[poz][x] * v[poz][p[poz][x]] > h[poz][x / 2] * v[poz][p[poz][x / 2]]){
intersch(poz, x, x / 2);
x /= 2;
}
}
inline void add(int poz1, int poz2){
while((h[poz1][poz2] + 1) * v[poz1][poz2] < h[poz1][0] * v[poz1][p[poz1][0]]){
h[poz1][poz2]++;
h[poz1][0]--;
cobor(poz1, 0, poz2);
}
p[poz1][poz2] = poz2;
urc(poz1, poz2);
}
int main(){
FILE *in = fopen("lapte.in", "r");
int n, l, i;
fscanf(in, "%d%d", &n, &l);
for(i = 0; i < n; i++){
fscanf(in, "%d%d", &v[0][i], &v[1][i]);
if(i == 0){
h[0][0] = h[1][0] = l;
p[0][0] = p[1][0] = 0;
}
else{
add(0, i);
add(1, i);
}
}
fclose(in);
int max = 0, s;
for(i = 0; i < n; i++){
ch[0][p[0][i]] = h[0][i];
ch[1][p[1][i]] = h[1][i];
s = h[0][i] * v[0][i] + h[1][i] * v[1][i];
if(s > max)
max = s;
}
FILE *out = fopen("lapte.out", "w");
fprintf(out, "%d\n", max);
for(i = 0; i < n; i++){
fprintf(out, "%d %d\n", ch[0][i], ch[1][i]);
}
fclose(out);
return 0;
}