998B - Cutting - CodeForces Solution


dp greedy sortings *1200

Please click on ads to support us..

Python Code:

def cutting(n, b, array):
    odd, even = 0, 0
    for val in array:
        if val % 2 == 0: even += 1
        else: odd += 1
    
    new_odd, new_even = 0, 0
    if array[0] % 2 == 0: new_even += 1; even -= 1
    else: new_odd += 1; odd -= 1
            
    cost = []
    for i in range(1, len(array) - 1):
        if array[i] % 2 == 0: new_even += 1; even -= 1
        else: new_odd += 1; odd -= 1
        if odd == even and new_odd == new_even:
            cost.append(abs(array[i+1]-array[i]))
    cost.sort()
    cuts = 0
    for c in cost:
        if c>b: break
        b-=c
        cuts+=1
    print(cuts)


n, b = input().split(' ')
n, b = int(n), int(b)
array = input().split(' ')
array = [int(val) for val in array]
cutting(n, b, array)

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int main () {
	int n, b;
	cin >> n >> b;
	int a[n];
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	int zj = 0, fr = 0;
	vector <int> ekh;
	for (int i = 0; i < n; i++) {
		if (a[i] % 2 == 0) zj ++;
		if (a[i] % 2 == 1) fr ++;
		if (zj == fr && i != n - 1) {
			ekh.push_back(abs(a[i] - a[i + 1]));
		}
	}
	int ans = 0;
	sort (ekh.begin(), ekh.end());
	for (int i = 0; i < ekh.size(); i++){
		if(b >= 0 && ekh[i] <= b){
			b -= ekh[i];
			ans++;
		}
	}
	cout << ans;
}
    		 		 	 	 	  		  		 	 	 			


Comments

Submit
0 Comments
More Questions

1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes