Đăng nhập để hỏi chi tiết


Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
import math
a = int(input())
b = list(map(int, input().split()))
c = [0] * a
d = [0] * (a + 1)
for e in range(a):
c[e] = math.log(b[e])
d[e + 1] = d[e] + c[e]
f = [0] * a
for g in range(1, a + 1):
h = -1e18
i = 1
for j in range(1, min(101, g + 1)):
k = d[g] - d[g - j]
l = k - math.log(math.factorial(j))
if l > h:
h = l
i = j
f[g - 1] = i
print(' '.join(map(str, f)))
Hãy giúp mọi người biết câu trả lời này thế nào?
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN = 2e6 + 5;
double log_fact[MAXN];
int a[MAXN];
int cost[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
// Tiền xử lý log(factorial)
log_fact[0] = 0;
for (int i = 1; i <= n; ++i)
log_fact[i] = log_fact[i - 1] + log(a[i]);
vector<int> res(n + 1);
int l = 1;
double max_score = -1e18;
for (int k = 1; k <= n; ++k) {
double sum_log = 0;
max_score = -1e18;
int max_len = 1;
for (int i = k, j = k; i >= 1; --i) {
sum_log += log(a[i]);
int len = j - i + 1;
double score = sum_log - log_fact[len];
if (score > max_score) {
max_score = score;
max_len = len;
}
}
cout << max_len << ' ';
}
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin