Editorial for Số điểm cao nhất
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
\(\color{red}{\text{Spoiler Alert}_{{}_{{}^{{}^{v2.0}}}}}\)
\(\color{red}{\text{Khuyến khích bạn đọc trước khi đọc phần lời giải xin hãy thử code ra thuật của mình dù nó có sai hay đúng}}\)
\(\color{red}{\text{Sau đó từ phần bài giải và thuật toán trước đó mà đối chiếu, rút nhận xét với thuật của mình và thu được bài học (không lãng phí thời gian đâu).}}\)
\(\color{orange}{\text{Hint 1 <Brute-force>}}\)
- Kết quả bài toán là \(res = \underset{i \in [1, n]}{\Sigma}\ \underset{j \in [1, i)}{\Sigma}(HP_i \times HP_j)\)
\(\color{orange}{\text{Hint 2 <Dynamic-programming>::<Partial-sum>}}\)
- Biến đổi toán học ta được
\(res = \underset{i \in [1, n]}{\Sigma}\ \underset{j \in [1, i)}{\Sigma}(HP_i \times HP_j)\)
\(= \underset{i \in [1, n]}{\Sigma}(HP_i \times \underset{j \in [1, i)}{\Sigma}(HP_j))\)
Gọi \(S_n = \underset{i \in [1, n]}{\Sigma}HP_i\)
Ta có \(res = \underset{i \in [1, n]}{\Sigma}(HP_i \times S_{i - 1})\)
\(\color{orange}{\text{Hint 3 <Dynamic-programming>::<Partial-sum>, <Online Solving>}}\)
- Gọi \(S = prev_S + a_i\), ta có thể không cần lưu mảng \(S\)
\(\color{green}{\text{Preference __ Code }}\): Approach
\(^{^{\color{purple}{\text{Complexity : }} O()\ \color{purple}{\text{time}}\ ||\ O()\ \color{purple}{\text{memory}}}}\)
C++
int main()
{
int res = 0;
int sum = 0;
for (int n = readInt(); n--; )
{
int x;
cin >> x;
res = (res + (1LL * x * sum) % MOD) % MOD;
sum = (sum + x) % MOD;
}
cout << res;
return 0;
}
Comments