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;
}

Is the Anker 733...

Is the Anker 733 GaNPrime Worth It? Our 6-Month...

Anker 733 65W Wall...

Anker 733: Wall Mode vs Battery Mode — Detailed...

Is the Anker 733...

Anker 733 for Steam Deck and Nintendo Switch: Gaming...

How Many Times Can...

How Many Charges Does the Anker 733's 10,000mAh Battery...

Best USB-C Fast Charging...

Best USB-C Cables for the Anker 733 GaNPrime 65W The...

Anker 733 Power Bank...

Anker 733 Overheating Test: ActiveShield 2.0 Under Stress Heat is...

Is the Anker 733 65W Power Bank Worth It? Real World Long-Term Review

Is the Anker 733 GaNPrime Worth It? Our 6-Month Real-World Review After 6 months of daily use as a primary travel and commuting charger, here's...

Anker 733 65W Wall Charger Mode vs Battery Mode: Power Output Differences

Anker 733: Wall Mode vs Battery Mode — Detailed Power Comparison The Anker 733 operates in two distinct modes with meaningfully different power outputs. Understanding...

Is the Anker 733 Good for Steam Deck and Nintendo Switch Gaming On the Go?

Anker 733 for Steam Deck and Nintendo Switch: Gaming Power Test Portable gaming on Steam Deck and Nintendo Switch has created a new use case...

How Many Times Can the Anker 733 10,000mAh Battery Recharge Your Phone?

How Many Charges Does the Anker 733's 10,000mAh Battery Provide? 10,000mAh sounds like a lot of power — but real-world charges differ from raw math....

Best USB-C Fast Charging Cables to Pair with Your Anker 733

Best USB-C Cables for the Anker 733 GaNPrime 65W The Anker 733 is only as fast as the cable connecting it to your device. Using...

Anker 733 Power Bank Overheating Test: Does ActiveShield 2.0 Keep It Cool?

Anker 733 Overheating Test: ActiveShield 2.0 Under Stress Heat is the enemy of both charging efficiency and battery longevity. We put the Anker 733's ActiveShield...

Is the Anker 733 Power Bank Heavy? Weight and Travel Portability Test

Anker 733 Weight and Portability: Is It Too Heavy to Carry Daily? At 326g (11.5 oz), the Anker 733 is heavier than a standalone GaN...

How Long Does It Take to Fully Recharge the Anker 733 Wall Charger?

How Long Does the Anker 733 Take to Recharge Itself? The Anker 733's genius is that it can charge itself while plugged into a wall...

Can You Charge 3 Devices at Once with the Anker 733 65W Power Bank?

Anker 733: Charging 3 Devices Simultaneously The Anker 733's three-port configuration (2× USB-C + 1× USB-A) makes it technically capable of charging three devices at...