550B - Preparing Olympiad - CodeForces Solution


bitmasks brute force *1400

Please click on ads to support us..

Python Code:

import itertools

n, l, r, x = map(int, input().split())
difficulties = [int(i) for i in input().split()]
difficulties.sort()

subSets = []
for i in range(2, n + 1):
    subSets.extend(list(itertools.combinations(difficulties, i)))

count = 0
for subSet in subSets:
    if subSet[-1] - subSet[0] < x:
        continue
    if sum(subSet) < l:
        continue
    if sum(subSet) > r:
        continue
    count += 1

print(count)

C++ Code:

#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define MP make_pair

// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);

typedef long long ll;
typedef long double ld;

using namespace std;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());


int32_t main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n,l,r,x;
	cin >> n >> l >> r >> x;

	vector<int> c(n);
	for (int i = 0; i < n; i++) {
		cin >> c[i];
	}

	int res = 0;
	for (int b = 0; b < (1<<n); b++) {
		vector<int> subset;
		for (int i = 0; i < n; i++) {
			if (b&(1<<i)) subset.PB(c[i]);
		}

		// check if subset verifies conditions
		bool atLeastTwo = (subset.size() >= 2);
		sort(subset.begin(),subset.end());
		bool diffDif = (atLeastTwo && subset[subset.size()-1] - subset[0] >= x);
		bool totalDif;
		int sum = 0;
		for (int i = 0; i < (int)subset.size(); i++) {
			sum += subset[i];
		}
		totalDif = (l <= sum && sum <= r);

		if (atLeastTwo && diffDif && totalDif) res++;
	}
	cout << res << '\n';
}


Comments

Submit
0 Comments
More Questions

1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe