场内只做出来一个B题。。居然涨了127分。。。
A,暴力枚举。。。
n = input()
c = 1
while '8' not in str(n + c):
c += 1
print c
B,分情况讨论,如果n=0,随意发挥就好。。。。如果n=1.,假设给你一个x,那么就是x,x,3x,3x。如果n=2,那么看那个大的数是不是小于等于那个小的数的三倍,如果是,输出x, y,4x-y,3x。如果n=3,你也不知道缺哪个数,索性就都尝试一遍然后验证。。。如果n=4,验证就好。。。
#include <cstdio>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int a[5];
bool judge() {
int t[5];
for (int i = 0; i < 4; i++) t[i] = a[i];
sort(t, t + 4);
double s1 = 0, s2 = 0, s3 = t[3] - t[0];
for (int i = 0; i < 4; i++)
s1 += t[i];
s2 = t[1] + t[2];
s1 /= 4.0; s2 /= 2.0;
return s1 == s2 && s2 == s3;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
if (n == 0) {
puts("YES");
printf("1\n1\n3\n3\n");
} else if (n == 1) {
puts("YES");
printf("%d\n%d\n%d\n", a[0], a[0] * 3, a[0] * 3);
} else if (n == 2) {
if (a[1] <= 3 * a[0]) {
puts("YES");
a[3] = a[0] * 3;
a[2] = a[0] + a[3] - a[1];
printf("%d\n%d\n", a[2], a[3]);
} else puts("NO");
} else if (n == 3) {
a[3] = a[0] + a[1] - a[2];
if (judge()) {
puts("YES");
printf("%d\n", a[3]);
return 0;
}
a[3] = a[0] + a[2] - a[1];
if (judge()) {
puts("YES");
printf("%d\n", a[3]);
return 0;
}
a[3] = a[1] + a[2] - a[0];
if (judge()) {
puts("YES");
printf("%d\n", a[3]);
return 0;
}
puts("NO");
} else {
if (judge()) puts("YES");
else puts("NO");
}
}
D。。。。应该是一个RMQ+DP,dp[i]表示前i位最少能分成几块。。。。不过需要一些辅助的量,首先是之前的状态,肯定不能枚举。。所以用RMQ来处理。。。。然后是区间的极差,那么必然是需要区间最大和最小的。。。。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = (int)1e5 + 10;
int mx[maxn][32], mn[maxn][32], preLog[maxn], dp[maxn], p[maxn], a[maxn];
int n, s, l;
void rmq_init() {
preLog[1] = 0;
for (int i = 2; i <= n; i++) {
preLog[i] = preLog[i - 1];
if ((1 << preLog[i] + 1) == i) {
preLog[i] += 1;
}
}
for (int i = n; i > 0; i--) {
mx[i][0] = mn[i][0] = a[i];
for (int j = 1; (i + (1 << j) - 1) <= n; j++){
mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]);
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << j - 1)][j - 1]);
}
}
}
int query(int l, int r) {
int k = preLog[r - l + 1];
return max(mx[l][k], mx[r - (1 << k) + 1][k]) - min(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int main() {
scanf("%d%d%d", &n, &s, &l);
for (int i = 1; i <= n; i++)
scanf("%d", a + i);
rmq_init();
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
for (int i = 1; i < l; i++)
dp[i] = -1;
int st = 0;
for (int i = 1; i <= n; i++) {
bool flag = 0;
for (int j = st; j <= i - l; j++, st++) {
if (dp[j] != -1 && query(j + 1, i) <= s) {
dp[i] = dp[j] + 1; flag = 1;
break;
}
}
if (!flag) dp[i] = -1;
}
printf("%d\n", dp[n]);
return 0;
}