1891E - Brukhovich and Exams - CodeForces Solution


greedy math sortings

Please click on ads to support us..

Python Code:

import sys
import heapq
import math


def solute():
    n, k = map(int, sys.stdin.readline().strip().split())
    a_list = list(map(int, sys.stdin.readline().split()))

    def coprime(a, b):
        return math.gcd(a, b) == 1

    con_pairs = []
    con_ones = []
    special_con_ones = []

    curr = 0
    prev = 1
    con_one = 0

    divs, rems = 0, 0
    for i, num in enumerate(a_list):
        if con_one:
            if num == 1:
                con_one += 1
                continue
            else:
                con_ones.append(con_one)
                con_one = 0
                prev = num
                continue
        if num == 1:
            con_one = 1
            if curr:
                con_pairs.append(curr)
                curr = 0
            prev = 1
            continue

        if i == 0:
            prev = num
            continue

        if coprime(num, prev):
            curr += 1
        else:
            if curr:
                con_pairs.append(curr)
            curr = 0
        prev = num

    if curr:
        con_pairs.append(curr)

    if con_one:
        rems += con_one
        if all(num == 1 for num in a_list):
            return max(0, con_one - k)

    if a_list[0] == 1:
        rems += con_ones.pop(0)

    con_ones.sort()
    special_con_ones.sort()
            
    ans = 0
    divs += sum(i >> 1 for i in con_pairs)
    rems += sum(i & 1 for i in con_pairs)

    r_divs = min(divs, k)
    divs -= r_divs
    k -= r_divs

    for i in range(len(con_ones)):
        if k >= con_ones[i]:
            k -= con_ones[i]
        else:
            ans += con_ones[i] + 1

                    
    r_rems = min(rems, k)
    rems -= r_rems
    k -= r_rems

    ans = ans + 2 * divs + rems
    if ans and k:
        ans = max(0, ans - k)
    return ans

t = int(sys.stdin.readline())

for _ in range(t):
    print(solute())

C++ Code:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

constexpr int MX = 1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd() { return rng() % MX; }

const int MN = 2e5 + 5;

int n, k, a[MN];

int main() {

    int T; scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &k);
        bool all_one = true;
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            all_one &= (a[i] == 1);
        }
        if (all_one) {
            if (k >= n) printf("0\n");
            else printf("%d\n", n - k);
            continue;
        }
        int ans = 0, d1 = 0, d2 = 0;
        for (int i = 1; i < n; ++i)
            ans += __gcd(a[i], a[i + 1]) == 1;
        vector < int > one = {};
        for (int i = 1, j; i <= n; i = j + 1) {
            j = i;
            if (a[i] != 1) {
                while (j < n && __gcd(a[j], a[j + 1]) == 1 && a[j + 1] != 1)
                    ++j;
                if (i == j) continue;
                d2 += (j - i) / 2;
                d1 += (j - i + 1) % 2 == 0;
            } else {
                while (j < n && a[j + 1] == 1) 
                    ++j;
                if (i == 1 || j == n) d1 += j - i + 1;
                else one.emplace_back(j - i + 1);
            }
        }
        ans -= min(k, d2) * 2;
        k -= min(k, d2);
        sort(one.begin(), one.end());
        for (int len : one)
            if (k >= len) {
                ans -= len + 1;
                k -= len;
            } else {
                ans -= k;
                k = 0;
                break;
            }
        ans -= min(k, d1);
        printf("%d\n", ans);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas