summaryrefslogtreecommitdiffstats
path: root/inf/rtkš/2.c
blob: 63f0953fe725a3056c99d880a230722b3fb8b0ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MIN(a,b) ((a) > (b) ? (b) : (a))
#define MAX(a,b) ((a) < (b) ? (b) : (a))
struct stolpec {
	int položaj;
	int višina;
	int izmeril;
};
int primerjaj_stolpca (const void * a, const void * b) {
	const struct stolpec * c = (const struct stolpec *) a;
	const struct stolpec * d = (const struct stolpec *) b;
	return d->višina - c->višina;
}
void natisni (int n, const struct stolpec * s) {
	for (int i = 0; i < n; i++) {
		printf("%d: ", s[i].položaj);
		for (int j = 0; j < s[i].višina; j++)
			printf("@");
		printf("\n");
	}
}
int voda (int n, struct stolpec * s) {
	struct stolpec razv[n];
	memcpy(razv, s, n*sizeof *s);
	qsort(razv, n, sizeof *s, primerjaj_stolpca);
	natisni(n, s);
	printf("---------\n");
	natisni(n, razv);
	int povr = 0;
	for (int i = 0; i < n-1; i++) {
		int max_pol = MAX(razv[i].položaj, razv[i+1].položaj);
		int min_pol = MIN(razv[i].položaj, razv[i+1].položaj);
		if (max_pol - min_pol > 1) {
			printf("med stoplcema z indeksoma %d in %d\n", min_pol, max_pol);
			for (int j = min_pol+1; j < max_pol; j++) {
				if (s[j].izmeril)
					continue;
				int tapovr = razv[i+1].višina - s[j].višina;
				if (tapovr > 0)
					povr += tapovr;
				s[j].izmeril++;
				printf("izmeril stoplec indeks %d - povr je %d ... min_viš je %d, s[j].višina je %d\n", j, povr, razv[i+1].višina, s[j].višina);
			}
		}
	}
	return povr;
}
int main (int argc, char ** argv) {
	int n = argc-1;
	struct stolpec * stolpci = calloc(n, sizeof *stolpci);
	for (int i = 0; i < n; i++) {
		stolpci[i].položaj = i;
		stolpci[i].višina = atoi(argv[i+1]);
	}
	return voda(n, stolpci);
}