#include <bits/stdc++.h>usingnamespacestd;
constint MAX = 16;
int sum[MAX];
int c[MAX];
int n, l, r, x;
bool check(int state) {
int sum = 0, mind = 0xfffffff, maxd = -1;
for (int i = 0; i < n; ++i) {
if (state & 1) {
sum += c[i];
if (c[i] > maxd) {
maxd = c[i];
}
if (c[i] < mind) {
mind = c[i];
}
}
state >>= 1;
}
if (sum < l || sum > r || (maxd - mind) < x) {
returnfalse;
}
returntrue;
}
int main() {
while (~scanf(" %d %d %d %d", &n, &l, &r, &x)) {
for (int i = 0; i < n; ++i) {
scanf(" %d", c + i);
}
sort(c, c + n);
memset(sum, 0, sizeof(sum));
sum[0] = c[0];
for (int i = 1; i < n; ++i) {
sum[i] = sum[i - 1] + c[i];
}
int ans = 0;
for (int i = 0; i < (1 << n); ++i) {
if (check(i)) {
ans++;
}
}
printf("%d\n", ans);
}
return0;
}
A. Two Substrings题意:问一个字符串中是否同时存在AB和BA,其中AB和BA不能共用同一个B或A。题解:扫一遍即可。可以考虑如下策略:先找AB,然后从B后的位置继续找BA;再另按BA-AB的顺序找一遍即可。复杂度O(n)O(n)。窝的代码则直接暴力出所有的AB和BA位置,只要出现不重叠的两个就OKOK~数据不大,随便搞。参考代码:#include <bits/stdc++.h