首页 > 其他分享 >CF1787C - Remove the Bracket

CF1787C - Remove the Bracket

时间:2023-03-06 13:14:35浏览次数:53  
标签:CF1787C Remove cdot int Bracket decrease ldots

https://codeforces.com/problemset/problem/1787/C
This is the reason why the problem was named as Remove the Bracket.

\begin{aligned} \text{Product} &= a_1 \cdot a_2 \cdot a_3 \cdot \ldots \cdot a_n = \ &= a_1 \cdot (x_2+y_2) \cdot (x_3+y_3) \cdot \ldots \cdot (x_{n-1}+y_{n-1}) \cdot a_n = \ &\overset{\text{?}}{=} a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot \ldots \cdot x_{n-1}+y_{n-1} \cdot a_n. \end{aligned}

\((x_i-s)(y_i-s)\geq 0\) tells us either \(\min(x_i,y_i)\geq\) s or \(\max(x_i,y_i) \leq\) s, so pickable \(x_i\) is a consecutive range.$

Just consider \((x_i+y_i)\), remove the bracket then it turns to \ldots+ y_{i-1}\cdot x_i+y_i\cdot x_{i+1}+\ldots. When y_{i-1} = x_{i+1}, the result is constant, so we assume that \(y_{i-1} < x_{i+1}\).

If x_i does not hit the maximum, increase \(x_i\) by 1 and decrease $ y_i $ by 1, the result will increase by \(y_{i-1}\) and decrease by \(x_{i+1}\), which means decrease by \(x_{i+1}-y_{i-1}\), always better. So \(x_i\) will either hit the maximum or the minimum. Thus, each number has only two ways of rewriting that might be optimal.

This can be done with DP.

#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
long long f[N][2],x[N],y[N];
void get() {
	int i,n,s,j;
	cin>>n>>s;
	for(i=1; i<=n; i++) {
		cin>>j;
		if(i==1||i==n) x[i]=y[i]=j;
		else if(j<=s) x[i]=0,y[i]=j;
		else x[i]=s,y[i]=j-s;
	}
	f[1][0]=f[1][1]=0;
	for(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]<<endl;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T; cin>>T;
	while(T--) get();
	return 0;
}

标签:CF1787C,Remove,cdot,int,Bracket,decrease,ldots
From: https://www.cnblogs.com/zrzsblog/p/17183456.html

相关文章