Official
B - Strictly Superior Editorial by en_translator
Try all possible \(i\) and \(j\) to check if the pair of the \(i\)-th and \(j\)-th items satisfy the conditions, and the problem can be solved.
The complexity is \(O(N^2M)\) time.
For two sorted sequences \(A\) and \(B\), one can check if any element contained in \(A\) is not contained in \(B\) with std::includes
function.
One can also use an \(M\)-bit integer to store whether feature \(k\ (1\leq k\leq M)\) is available in each item, so that the problem is solved in a total of \(O(N^2M/w)\) time, where \(w\) is the word size.
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
// Determines if product_j is superior to product_i
bool is_strictly_superior(const pair<int, vector<int>> &product_i, const pair<int, vector<int>> &product_j) {
const auto&[price_i, feature_i]{product_i};
const auto&[price_j, feature_j]{product_j};
return price_i >= price_j &&
includes(feature_j.begin(), feature_j.end(), feature_i.begin(), feature_i.end()) &&
(price_i > price_j || !includes(feature_i.begin(), feature_i.end(), feature_j.begin(), feature_j.end()));
}
// Determines if `products` has a pair with superior item
bool has_superior_pair(const vector<pair<int, vector<int>>> &products) {
return any_of(products.begin(), products.end(), [&products](const auto &product_i) {
return any_of(products.begin(), products.end(), [&product_i](const auto &product_j) {
return is_strictly_superior(product_i, product_j);
});
});
}
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, vector<int>>> products(N);
for (auto&&[price, feature] : products) {
cin >> price;
int K;
cin >> K;
feature = vector<int>(K);
for (auto &&f : feature)
cin >> f;
}
cout << (has_superior_pair(products) ? "Yes" : "No") << endl;
return 0;
}
posted:
last update: