From 9c061b8b28b48612b8abbf5209783fdd2bc04fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Thu, 31 Oct 2024 14:54:29 +0100 Subject: aps1dn1: osvetlitev --- "\305\241ola/aps1/dn/osvetlitev/Makefile" | 4 +++ "\305\241ola/aps1/dn/osvetlitev/in.txt" | 9 ++++++ "\305\241ola/aps1/dn/osvetlitev/resitev.cpp" | 46 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 "\305\241ola/aps1/dn/osvetlitev/Makefile" create mode 100644 "\305\241ola/aps1/dn/osvetlitev/in.txt" create mode 100644 "\305\241ola/aps1/dn/osvetlitev/resitev.cpp" (limited to 'šola/aps1') diff --git "a/\305\241ola/aps1/dn/osvetlitev/Makefile" "b/\305\241ola/aps1/dn/osvetlitev/Makefile" new file mode 100644 index 0000000..2d78386 --- /dev/null +++ "b/\305\241ola/aps1/dn/osvetlitev/Makefile" @@ -0,0 +1,4 @@ +program: resitev.cpp + g++ -Wall -Wextra -pedantic -Wformat-security -std=c++20 -o$@ $< +clean: + rm program diff --git "a/\305\241ola/aps1/dn/osvetlitev/in.txt" "b/\305\241ola/aps1/dn/osvetlitev/in.txt" new file mode 100644 index 0000000..76b90fe --- /dev/null +++ "b/\305\241ola/aps1/dn/osvetlitev/in.txt" @@ -0,0 +1,9 @@ +30 +7 +10 2 +23 2 +14 1 +4 1 +14 4 +11 5 +1 2 diff --git "a/\305\241ola/aps1/dn/osvetlitev/resitev.cpp" "b/\305\241ola/aps1/dn/osvetlitev/resitev.cpp" new file mode 100644 index 0000000..0a17f31 --- /dev/null +++ "b/\305\241ola/aps1/dn/osvetlitev/resitev.cpp" @@ -0,0 +1,46 @@ +#include +#include +#include +struct event { + int pos; + bool tip; // true za začetek, false za konec +}; +int compar_events (const void * a, const void * b) { + if (((struct event *) a)->pos == ((struct event *) b)->pos) + return 0; + if (((struct event *) a)->pos < ((struct event *) b)->pos) + return -1; + return 1; +} +int main (void) { + struct event events[20000]; + int M, N, x, d; + scanf("%d %d", &M, &N); + for (int i = 0; i < N; i++) { + scanf("%d %d", &x, &d); + events[2*i].pos = x-d >= 0 ? x-d : 0; + events[2*i].tip = true; + events[2*i+1].pos = x+d <= M ? x+d : M; + events[2*i+1].tip = false; + } + qsort(events, 2*N, sizeof events[0], compar_events); + int osv = 0; + int depth = 0; + int start; + for (int i = 0; i < 2*N; i++) { + // fprintf(stderr, "pos=%d\ttip=%d\n", events[i].pos, events[i].tip); + if (events[i].tip == true) { + if (depth == 0) + start = events[i].pos; + depth++; + } + if (events[i].tip == false) { + depth--; + if (depth == 0) + osv += events[i].pos - start; + } + } + if (depth != 0) + fprintf(stderr, "depth == %d\n", depth); + printf("%d\n", M-osv); +} -- cgit v1.2.3