C. Remove the Bracket

C. Remove the Bracket

RSJ has a sequence a of n integers a1,a2,,an and an integer s. For each of a2,a3,,an1, he chose a pair of non-negative integers xi and yi such that xi+yi=ai and (xis)(yis)0.

Now he is interested in the value F=a1x2+y2x3+y3x4++yn2xn1+yn1an.

Please help him find the minimum possible value F he can get by choosing xi and yi optimally. It can be shown that there is always at least one valid way to choose them.

Input

Each test contains multiple test cases. The first line contains an integer t (1t104) — the number of test cases.

The first line of each test case contains two integers n, s (3n2105; 0s2105).

The second line contains n integers a1,a2,,an (0ai2105).

It is guaranteed that the sum of n does not exceed 2105.

Output

For each test case, print the minimum possible value of F.

Example

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, 20+01+03+04=0.

In the second test case, 51+22+22+15=18.

 

解题思路

  这题一看这么多变量不好下手,实际上要想到把其他变量固定而只考虑某个变量的取值。具体来说就是,假设除xiyi外其他的变量都是固定的值,再来求F的最小值。

  这个就好求多了,其中xiyi能够影响到的项只有yi1xi+yixi+1,而yi=aixi,因此就有yi1xi+(aixi)xi+1。由于只有xi是变量,因此可以发现这个一元线性函数,很明显要使得yi1xi+(aixi)xi+1取到最小值,那么xi的取值一定是定义域的其中一个端点(此时yi也是其定义域的一个端点)。

  xi的定义域就与ais有关系了:

  1. 如果ais,那么0xiai,因此要么是xi=0, yi=ai,要么是xi=ai, yi=0
  2. 如果ai>s,同理要么是xi=ais, yi=s,要么是xi=s, yi=ais

  因此可以发现,当其他变量固定时,要使得F取到最小值那么xiyi应该取定义域的两个端点,有两种组合。剩下的就是枚举2n种情况取最小值就可以了,因此可以考虑用状态机dp。

  定义状态f(i,0)f(i,1),其中这里的01表示xiyi取值的两种组合情况(1种情况就是对第0种的xiyi两两交换一下就可以了)。因此f(i,0)就表示前i项和且xi,yi是第0种组合的最小值,同理f(i,1)就表示前i项和且xi,yi是第1种组合的最小值。这里的前i项和是指j=2iyj1xj。状态划分就根据第i1项是哪种组合来转移就可以了,具体看状态转移方程:

{f(i,0)=min{f(i1,0)+yi1xi, f(i1,1)+xi1xi}f(i,1)=min{f(i1,0)+yi1yi, f(i1,1)+xi1yi}

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 LL x[N], y[N];
 9 LL f[N][2];
10 
11 void solve() {
12     int n, m;
13     scanf("%d %d", &n, &m);
14     for (int i = 1; i <= n; i++) {
15         LL v;
16         scanf("%lld", &v);
17         if (i == 1 || i == n) x[i] = y[i] = v;
18         else if (v <= m) x[i] = 0, y[i] = v;
19         else x[i] = v - m, y[i] = m;
20     }
21     for (int i = 2; i <= n; i++) {
22         f[i][0] = min(f[i - 1][0] + y[i - 1] * x[i], f[i - 1][1] + x[i - 1] * x[i]);
23         f[i][1] = min(f[i - 1][0] + y[i - 1] * y[i], f[i - 1][1] + x[i - 1] * y[i]);
24     }
25     printf("%lld\n", f[n][0]);
26 }
27 
28 int main() {
29     int t;
30     scanf("%d", &t);
31     while (t--) {
32         solve();
33     }
34     
35     return 0;
36 }
复制代码

 

参考资料

  TypeDB Forces 2023 (Div. 1 + Div. 2, Rated, Prizes!) Editorial:https://codeforces.com/blog/entry/112009

  TypeDB Forces 2023 (Div. 1 + Div. 2, Rated, Prizes!)(持续更新):https://www.cnblogs.com/cjjsb/p/17077371.html

posted @   onlyblues  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示