Codeforces Round #387 (Div. 2), problem: (D) Winter Is Coming Solution in C/C++

Codeforces Round #387 (Div. 2), problem: (D) Winter Is Coming Solution in C/C++

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
	int *pa = (int *) a;
	int *pb = (int *) b;

	return *pb - *pa;
}

int main() {
	int n, k, i, head, tail, a, b;
	static int aa[200000], bb[200000];

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	a = b = 0;
	head = -1;
	for (i = 0; i < n; i++)
		if (aa[i] < 0) {
			if (head == -1)
				head = a;
			else if (a > 0)
				bb[b++] = a;
			a = 0;
		} else
			a++;
	tail = a;
	if (head == -1)
		printf("0\n");
	else {
		int m, c1, c2, c;

		qsort(bb, b, sizeof(*bb), compare);
		/* case 1: end winter tires */
		m = n - head;
		c1 = 1;
		for (i = 0; i < b && m > k; i++) {
			m -= bb[i];
			c1 += 2;
		}
		if (m > k)
			c1 = INT_MAX;
		/*case 2: end summer tires */
		m = n - head - tail;
		c2 = 2;
		for (i = 0; i < b && m > k; i++) {
			m -= bb[i];
			c2 += 2;
		}
		if (m > k)
			c2 = INT_MAX;
		c = c1 < c2 ? c1 : c2;
		printf("%d\n", c == INT_MAX ? -1 : c);
	}
	return 0;
}

Codeforces Round #387 (Div. 2), problem: (D) Winter Is Coming Solution in C/C++

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
	int *pa = (int *) a;
	int *pb = (int *) b;

	return *pb - *pa;
}

int main() {
	int n, k, i, head, tail, a, b;
	static int aa[200000], bb[200000];

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	a = b = 0;
	head = -1;
	for (i = 0; i < n; i++)
		if (aa[i] < 0) {
			if (head == -1)
				head = a;
			else if (a > 0)
				bb[b++] = a;
			a = 0;
		} else
			a++;
	tail = a;
	if (head == -1)
		printf("0\n");
	else {
		int m, c1, c2, c;

		qsort(bb, b, sizeof(*bb), compare);
		/* case 1: end winter tires */
		m = n - head;
		c1 = 1;
		for (i = 0; i < b && m > k; i++) {
			m -= bb[i];
			c1 += 2;
		}
		if (m > k)
			c1 = INT_MAX;
		/*case 2: end summer tires */
		m = n - head - tail;
		c2 = 2;
		for (i = 0; i < b && m > k; i++) {
			m -= bb[i];
			c2 += 2;
		}
		if (m > k)
			c2 = INT_MAX;
		c = c1 < c2 ? c1 : c2;
		printf("%d\n", c == INT_MAX ? -1 : c);
	}
	return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Putin approves takeover of French and Swiss giants’ Russian assets. Why now?

In a major development regarding foreign corporate ownership in Russia, President Vladimir Putin has authorized a decree placing the local subsidiaries of prominent Swiss...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!