Skip to content

Commit 19bfe6f

Browse files
committed
18111
1 parent 4be55eb commit 19bfe6f

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

solvedac_class_2/18111.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int ft_compare(const void *a, const void *b)
5+
{
6+
if (*((int *)a) < *((int *)b))
7+
return (1);
8+
else if (*((int *)a) > *((int *)b))
9+
return (-1);
10+
else
11+
return (0);
12+
}
13+
14+
int main()
15+
{
16+
int n, m, b, min_height, max_height, temp_b, min_time, result_height;
17+
int *map;
18+
int *time_by_height;
19+
20+
scanf("%d %d %d", &n, &m, &b);
21+
map = (int *) malloc(sizeof(int) * (n * m));
22+
min_height = 256;
23+
max_height = 0;
24+
for (int i = 0; i < (n * m); ++i)
25+
{
26+
scanf("%d", (map + i));
27+
if (*(map + i) > max_height)
28+
max_height = *(map + i);
29+
if (*(map + i) < min_height)
30+
min_height = *(map + i);
31+
}
32+
qsort(map, n * m, sizeof(int), ft_compare);
33+
if (min_height > 0)
34+
--min_height;
35+
if (max_height < 256)
36+
++max_height;
37+
time_by_height = (int *) malloc(sizeof(int) * (max_height - min_height + 1));
38+
for (int i = 0; i < max_height - min_height + 1; ++i)
39+
{
40+
temp_b = b;
41+
*(time_by_height + i) = 0;
42+
for (int j = 0; j < (n * m); ++j)
43+
{
44+
if (*(map + j) > (min_height + i))
45+
{
46+
*(time_by_height + i) += (*(map + j) - (min_height + i)) * 2;
47+
temp_b += (*(map + j) - i);
48+
}
49+
else if (*(map + j) < (min_height + i))
50+
{
51+
if (((min_height + i) - *(map + j)) <= temp_b)
52+
{
53+
*(time_by_height + i) += ((min_height + i) - *(map + j));
54+
temp_b -= ((min_height + i) - *(map + j));
55+
}
56+
else
57+
{
58+
*(time_by_height + i) = -1;
59+
break ;
60+
}
61+
}
62+
}
63+
}
64+
min_time = 2147483647;
65+
for (int i = 0; i < max_height - min_height + 1; ++i)
66+
{
67+
if (*(time_by_height + i) > -1 && *(time_by_height + i) <= min_time)
68+
{
69+
min_time = *(time_by_height + i);
70+
result_height = i + min_height;
71+
}
72+
}
73+
printf("%d %d", min_time, result_height);
74+
free(map);
75+
free(time_by_height);
76+
return (0);
77+
}

0 commit comments

Comments
 (0)