题目相关
题目链接
KEYENCE Programming Contest 2021 A 题,https://atcoder.jp/contests/keyence2021/tasks/keyence2021_a。
Problem Statement
Snuke has two number sequences
a
a
a and
b
b
b, each of length
N
N
N. The
i
i
i-th number of
a
a
a is
a
i
a_i
ai, and that of
b
b
b is
b
i
b_i
bi.
Using these sequences, he has decided to make a new sequence
c
c
c of length
N
N
N. For each
n
n
n such that
1
≤
n
≤
N
1≤n≤N
1≤n≤N,
c
n
c_n
cn, the
n
n
n-th number of
c
c
c, is the maximum value of
a
i
b
j
a_ib_j
aibj for a pair
(
i
,
j
)
(i,j)
(i,j) such that
1
≤
i
≤
j
≤
n
1≤i≤j≤n
1≤i≤j≤n. Formally, we have
c
n
=
m
a
x
1
≤
i
≤
j
≤
n
a
i
b
j
c_n=max_{1≤i≤j≤n}a_ib_j
cn=max1≤i≤j≤naibj.
Find
c
1
,
c
2
,
…
,
c
N
c_1,c_2,…,c_N
c1,c2,…,cN.
Input
Input is given from Standard Input in the following format:
N
a1 a2 ... aN
b1 b2 ... bN
Print N N N lines. The n n n-th line from the top should contain c n c_n cn.
Sample 1
Sample Input 1
3
3 2 20
1 4 1
Sample Output 1
3
12
20
Explaination
We have:
c
1
=
m
a
x
(
a
1
b
1
)
=
3
c_1=max(a_1b_1)=3
c1=max(a1b1)=3;
c
2
=
m
a
x
(
a
1
b
1
,
a
1
b
2
,
a
2
b
2
)
=
12
c2=max(a_1b_1,a_1b_2,a_2b_2)=12
c2=max(a1b1,a1b2,a2b2)=12;
c
3
=
m
a
x
(
a
1
b
1
,
a
1
b
2
,
a
1
b
3
,
a
2
b
2
,
a
2
b
3
,
a
3
b
3
)
=
20
c3=max(a_1b_1,a_1b_2,a_1b_3,a_2b_2,a_2b_3,a_3b_3)=20
c3=max(a1b1,a1b2,a1b3,a2b2,a2b3,a3b3)=20.
Sample 2
Sample Input 2
20
715806713 926832846 890153850 433619693 890169631 501757984 778692206 816865414 50442173 522507343 546693304 851035714 299040991 474850872 133255173 905287070 763360978 327459319 193289538 140803416
974365976 488724815 821047998 371238977 256373343 218153590 546189624 322430037 131351929 768434809 253508808 935670831 251537597 834352123 337485668 272645651 61421502 439773410 621070911 578006919
Sample Output 2
697457706539596888
697457706539596888
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
760974252688942308
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
867210459214915026
Constraints
- All values in input are integers.
- 1 ≤ N ≤ 2 × 1 0 5 1≤N≤2×10^5 1≤N≤2×105
- 1 ≤ a i , b i ≤ 1 0 9 1≤a_i,b_i≤10^9 1≤ai,bi≤109
题解报告
题目翻译
Snuke 有两个数列
a
a
a 和
b
b
b,它们的长度都是
N
N
N。数列
a
a
a 的第
i
i
i 个数据为
a
i
a_i
ai,数列
b
b
b 的第
i
i
i 个数据为
b
i
b_i
bi。
现在从这两个数列中构造一个新数列
c
c
c,数列
c
c
c 的长度也为
N
N
N,该数列的第
n
n
n 个数为
c
n
=
m
a
x
1
≤
i
≤
j
≤
n
a
i
b
j
c_n=max_{1≤i≤j≤n}a_ib_j
cn=max1≤i≤j≤naibj.
题目分析
本题是一个数学题。
如果我们直接使用循环来遍历公式
c
n
=
m
a
x
1
≤
i
≤
j
≤
n
a
i
b
j
c_n=max_{1≤i≤j≤n}a_ib_j
cn=max1≤i≤j≤naibj,这样我们将是一个双重循环。代码如下:
for (int i=1; i<=n; i++) {
for (int j=i; j<=n; j++) {
//计算cn
}
}
上面的公式,我们可以知道算法的时间复杂度为
O
(
N
2
)
O(N^2)
O(N2)。再看本题
N
N
N 的范围是
2
×
1
0
5
2×10^5
2×105,因此这样的代码必然是一个 TLE。我们需要对递推公式进行优化。
我们根据
c
n
c_n
cn 的公式,可以写出
c
n
c_n
cn 与
c
n
−
1
c_{n-1}
cn−1 的关系。
c
n
−
1
=
m
a
x
1
≤
i
≤
j
≤
n
−
1
a
i
b
j
=
m
a
x
(
a
1
b
1
,
.
.
.
,
a
1
b
n
−
1
,
a
2
b
2
,
.
.
.
,
a
2
b
n
−
1
,
.
.
.
,
a
n
−
1
b
n
−
1
)
c
n
=
m
a
x
1
≤
i
≤
j
≤
n
a
i
b
j
=
m
a
x
(
a
1
b
1
,
.
.
.
,
a
1
b
n
−
1
,
a
1
b
n
,
a
2
b
2
,
.
.
.
,
a
2
b
n
−
1
,
a
2
b
n
,
.
.
.
,
a
n
−
1
b
n
−
1
,
a
n
−
1
b
n
,
a
n
b
n
)
⇒
c
n
=
m
a
x
(
c
n
−
1
,
a
1
b
n
,
a
2
b
n
,
.
.
.
,
a
n
b
n
)
=
m
a
x
(
c
n
−
1
,
m
a
x
(
a
1
,
a
2
,
.
.
.
,
a
n
)
b
n
)
c_{n-1}=max_{1≤i≤j≤n-1}a_ib_j=max(\\ a_1b_1, ..., a_1b_{n-1},\\ a_2b_2,...,a_2b_{n-1},\\ ...,\\ a_{n-1}b_{n-1})\\ c_n=max_{1≤i≤j≤n}a_ib_j=max(\\ a_1b_1, ..., a_1b_{n-1}, a_1b_n,\\ a_2b_2,...,a_2b_{n-1},a_2b_n,\\ ...,\\ a_{n-1}b_{n-1}, a_{n-1}b_{n}, \\ a_nb_n)\\ \Rightarrow c_n=max(c_{n-1}, a_1b_n, a_2b_n,...,a_nb_n)\\ =max(c_{n-1}, max(a_1, a_2,...,a_n)b_n)
cn−1=max1≤i≤j≤n−1aibj=max(a1b1,...,a1bn−1,a2b2,...,a2bn−1,...,an−1bn−1)cn=max1≤i≤j≤naibj=max(a1b1,...,a1bn−1,a1bn,a2b2,...,a2bn−1,a2bn,...,an−1bn−1,an−1bn,anbn)⇒cn=max(cn−1,a1bn,a2bn,...,anbn)=max(cn−1,max(a1,a2,...,an)bn)
这样优化后,时间复杂度就变为
O
(
N
)
O(N)
O(N),对于
2
×
1
0
5
2×10^5
2×105 的数据来说,这个时间复杂度是足够的。
数据范围估计
由于我们需要计算 a i × b i a_i×b_i ai×bi,而本题的 a i a_i ai 和 b i b_i bi 的最大数据为 1 0 9 10^9 109,因此 a i × b i a_i×b_i ai×bi 的最大值为 1 0 9 × 1 0 9 = 1 0 18 10^9×10^9=10^{18} 109×109=1018,我们需要用 long long 来表示。
AC 代码
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN=2e5+4;
ll a[MAXN], b[MAXN], c[MAXN];
int main() {
int n;
scanf("%d", &n);
for (int i=1; i<=n; i++) {
scanf("%lld", &a[i]);
}
for (int i=1; i<=n; i++) {
scanf("%lld", &b[i]);
}
//构造c
ll maxx=a[1];
c[1]=a[1]*b[1];
for (int i=2; i<=n; i++) {
maxx = max(maxx, a[i]);
c[i] = max(c[i-1], maxx*b[i]);
}
//输出
for (int i=1; i<=n; i++) {
printf("%lld\n", c[i]);
}
return 0;
}
时间复杂度
O ( N ) O(N) O(N)。
空间复杂度
O ( N ) O(N) O(N)。