Add a lot more of my solutions.
I don't remember which ones doesn't pass though
This commit is contained in:
150
CSES Problem Set/Meet in the Middle.cpp
Normal file
150
CSES Problem Set/Meet in the Middle.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/* Problem URL: https://cses.fi/problemset/task/1628/ */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
ll s;
|
||||
cin >> n >> s;
|
||||
|
||||
vl fds(n);
|
||||
cin >> fds;
|
||||
|
||||
int mid = n / 2;
|
||||
|
||||
vl count;
|
||||
|
||||
function<void(int, ll)> func1 = [&](int i, ll sum) {
|
||||
if (sum > s) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (i >= mid) {
|
||||
count.push_back(sum);
|
||||
return;
|
||||
}
|
||||
|
||||
func1(i + 1, sum);
|
||||
func1(i + 1, sum + fds[i]);
|
||||
};
|
||||
|
||||
ll ans = 0;
|
||||
vl sum;
|
||||
vl c;
|
||||
|
||||
function<void(int, ll)> func2 = [&](int i, ll ss) {
|
||||
if (ss > s) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (i >= n - mid) {
|
||||
// ans += count[s - sum];
|
||||
auto itr = lower_bound(all(sum), s - ss);
|
||||
if (itr == sum.end() || *itr != s - ss) {
|
||||
return;
|
||||
}
|
||||
ans += c[itr - sum.begin()];
|
||||
return;
|
||||
}
|
||||
|
||||
func2(i + 1, ss);
|
||||
func2(i + 1, ss + fds[i + mid]);
|
||||
};
|
||||
|
||||
func1(0, 0);
|
||||
|
||||
sortv(count);
|
||||
ll prev = count[0];
|
||||
int co = 1;
|
||||
|
||||
nrep(i, 1, count.size()) {
|
||||
if (prev == count[i]) {
|
||||
co++;
|
||||
continue;
|
||||
}
|
||||
|
||||
sum.push_back(prev);
|
||||
c.push_back(co);
|
||||
|
||||
prev = count[i];
|
||||
co = 1;
|
||||
}
|
||||
|
||||
sum.push_back(prev);
|
||||
c.push_back(co);
|
||||
|
||||
func2(0, 0);
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
Reference in New Issue
Block a user