1704B - Luke is a Foodie - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import *
from collections import defaultdict, deque, Counter
import math, string
from heapq import *
from operator import add
from itertools import accumulate

BUFSIZE = 8192
sys.setrecursionlimit(10 ** 5)


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        import os

        self.os = os
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            self.os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")


inf = float("inf")
en = lambda x: list(enumerate(x))
ceil_ = lambda a, b: (a + b - 1) // b


ii = lambda: int(input())
r = lambda: map(int, input().split())
rr = lambda: list(r())


def solve():
    n , x = r()
    arr = rr()

    brr = []

    for i in arr:
        brr += sorted([-x + i, x + i]),
    
    x = [-inf, inf]
    ans = 0
    
    for i,j in brr:
        if not (i > x[1]) and not (j < x[0]):
            x[0] = max(x[0],i)
            x[1] = min(x[1],j)
        else:
            x = [i,j]
            ans += 1
        
        x = sorted(x)
    
    print(ans)

for i in range(1, ii() + 1):
    solve()

C++ Code:

#include <iostream>
int main()
{
    int t;
    std::cin>>t;
    for(int i=1;i<=t;i++)
    {
        int n;
        std::cin>>n;
        int x;
        std::cin>>x;
        int a[n];
        for(int j=0;j<n;j++)
        {
            std::cin>>a[j];
        }
        int minNoChange = 0;
        int min = a[0];
        int max = a[0];
        for(int j=1;j<n;j++)
        {
            if(a[j]<min)
            {
                min = a[j];
            }
            if(a[j]>max)
            {
                max = a[j];
            }
            if (max%2==0 && min%2==0 || max%2==1 && min%2==1)
            {
                if(((max-min)/2)>x)
                {
                    min = a[j];
                    max = a[j];
                    minNoChange++;
                }
            }
            else
            {
                if(max%2==0)
                {
                    if(((max-(min-1))/2)>x)
                    {
                        min = a[j];
                        max = a[j];
                        minNoChange++;
                    }
                }
                else
                {
                    if((((max+1)-min)/2)>x)
                    {
                        min = a[j];
                        max = a[j];
                        minNoChange++;
                    }
                }
            }
        }
        std::cout<<minNoChange<<std::endl;
    }
}


Comments

Submit
0 Comments
More Questions

1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality
610A - Pasha and Stick
1200A - Hotelier
1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move