about summary refs log tree commit diff
path: root/others/other/lseq.c
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2017-01-12 21:35:03 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2017-01-13 11:37:17 +0700
commit1fca99158c60a94497b21ff527d4071d96c9c0f1 (patch)
tree3b6533c4c5fa4f67a6ae0fffdf042f4b46a749e6 /others/other/lseq.c
parent7c9b47ab9149d292d5493c865dfb8742a7450472 (diff)
downloadcp-1fca99158c60a94497b21ff527d4071d96c9c0f1.tar.gz
others/other: Add diffsum.{py,pas} and lseq.c
Diffstat (limited to 'others/other/lseq.c')
-rw-r--r--others/other/lseq.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/others/other/lseq.c b/others/other/lseq.c
new file mode 100644
index 0000000..c0e4e81
--- /dev/null
+++ b/others/other/lseq.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int sign(long x)
+{
+	return (x) ? ((x > 0) ? 1 : -1) : x;
+}
+
+int cmp(const void *x, const void *y)
+{
+	return sign(*(long *) x - *(long *) y);
+}
+
+int main()
+{
+	FILE *f = fopen("LSEQ.INP", "r");
+	long n, *a, i, *start, *end, length = 1, tmp;
+	char b = 0;
+
+	fscanf(f, "%ld", &n);
+	a = malloc(n * sizeof(long));
+	for(i = 0; i < n; i++)
+		fscanf(f, "%ld", a + i);
+	fclose(f);
+
+	qsort(a, n, sizeof(long), cmp);
+	start = malloc(n * sizeof(long));
+	start[0] = !*a;
+	for (i = start[0] + 1; i < n; i++)
+		if (a[i] - 1 - a[i - 1])
+			start[length++] = i;
+	start = realloc(start, length * sizeof(long));
+
+	end = malloc(length * sizeof(long));
+	end[length - 1] = n - 1;
+	for (i = 1; i < length; i++)
+		end[i - 1] = start[i] - 1;
+
+	n = 0;
+	if (*a)
+		for (i = 0; i < length; i++) {
+			tmp = end[i] - start[i] + 1;
+			n = (tmp > n) ? tmp : n;
+		}
+	else
+		for (i = 0; i < length; i++) {
+			tmp = end[i] - start[i - 1] + 1;
+			if (!i || a[end[i]] - a[start[i - 1]] - tmp)
+				tmp += start[i - 1] - start[i];
+			n = (++tmp > n) ? tmp : n;
+		}
+
+	f = fopen("LSEQ.OUT", "w");
+	fprintf(f, "%ld\n", n);
+	fclose(f);
+
+	return 0;
+}