Remove the Bracket
题目描述:
RSJ has a sequence
Now he is interested in the value
Please help him find the minimum possible value
输入描述:
Each test contains multiple test cases. The first line contains an integer
The first line of each test case contains two integers
The second line contains
It is guaranteed that the sum of
输出描述:
For each test case, print the minimum possible value of
样例:
input:
10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480
output:
0
18
32
11
14
0
16
0
40000000000
2700826806
Note:
In the first test case,
In the second test case,
AC代码:
题目中公式
对于
由于
所以原式就变成了关于
时,可以取最值 和 时,可以取最值 和
这两种情况都保证满足
这样可以采取DP
采用一个数组
- 当
时, 取最小值时的结果 - 当
时, 取最大值时的结果
而
最后结果取
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 2e5 + 10; LL f[N][2]; LL x[N], y[N]; void solve() { int n, s; cin >> n >> s; for(int i = 1; i <= n; i ++) { int m; cin >> m; if(i == 1 || i == n) { x[i] = y[i] = m; } else if(m <= s) { x[i] = 0, y[i] = m; } else { x[i] = min(s, m - s), y[i] = max(s, m - s); } } f[1][0] = f[1][1] = 0; for(int i = 2; i <= n; i ++) { f[i][0] = min(f[i - 1][0] + y[i - 1] * x[i], f[i - 1][1] + x[i - 1] * x[i]); f[i][1] = min(f[i - 1][0] + y[i - 1] * y[i], f[i - 1][1] + x[i - 1] * y[i]); } cout << f[n][0] << '\n'; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; cin >> T; while(T --) solve(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】