题目链接:https://codeforces.com/contest/1499/problem/C
题解
感觉有点假的一道题,尤其是那个假的结论。
不难发现我们可以把问题转换成下面这个问题。
首先我们知道
l
e
n
g
t
h
1
+
l
e
n
g
t
h
3
+
l
e
n
g
t
h
5
+
.
.
.
=
n
length_{1}+length_{3}+length_{5}+...=n
length1+length3+length5+...=n
l
e
n
g
t
h
2
+
l
e
n
g
t
h
4
+
l
e
n
g
t
h
6
+
.
.
.
=
n
length_{2}+length_{4}+length_{6}+...=n
length2+length4+length6+...=n
现在让你最小化
∑
i
=
1
k
c
i
∗
l
e
n
g
t
h
i
\sum^{k}_{i=1}c_{i}*length_{i}
∑i=1kci∗lengthi。
即最小化
c
1
∗
l
e
n
g
t
h
1
+
c
3
∗
l
e
n
g
t
h
3
+
c
5
l
e
n
g
t
h
5
+
.
.
.
c_{1}*length_{1}+c_{3}*length_{3}+c_{5}length_{5}+...
c1∗length1+c3∗length3+c5length5+...
c
2
∗
l
e
n
g
t
h
2
+
c
4
∗
l
e
n
g
t
h
4
+
c
6
l
e
n
g
t
h
6
+
.
.
.
c_{2}*length_{2}+c_{4}*length_{4}+c_{6}length_{6}+...
c2∗length2+c4∗length4+c6length6+...
结论就是对除最小值
c
i
c_{i}
ci的其他数赋值为
1
1
1,对最小值赋值为剩余的数,这样即可保证最终的和最小,结论有点假。
知道这个结论后就好办了,把上面的结论公式化之后就是
s
u
m
O
d
d
+
m
i
n
O
d
d
⋅
(
n
−
c
n
t
O
d
d
)
+
s
u
m
E
v
e
n
+
m
i
n
E
v
e
n
⋅
(
n
−
c
n
t
E
v
e
n
)
sumOdd + minOdd \cdot (n - cntOdd)+sumEven + minEven \cdot (n - cntEven)
sumOdd+minOdd⋅(n−cntOdd)+sumEven+minEven⋅(n−cntEven)。
因为我们取要求的是必须从前往后取,那么我们直接从前往后枚举维护答案即可。
代码
#include <bits/stdc++.h>
#define PI atan(1.0)*4
#define rp(i,s,t) for (register int i = (s); i <= (t); i++)
#define RP(i,t,s) for (register int i = (t); i >= (s); i--)
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define m_p make_pair
#define p_b push_back
#define ins insert
#define era erase
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define dg if(debug)
#define pY puts("YES")
#define pN puts("NO")
#define outval(a) cout << "Debuging...|" << #a << ": " << a << "\n";
#define outval2(a,b) cout << "Debuging...|" << #a << ": " << a <<"\t"<< #b << ": " << b << "\n";
#define outval3(a,b,c) cout << "Debuging...|" << #a << ": " << a <<"\t"<< #b << ": " << b <<"\t"<< #c << ": " << c << "\n";
using namespace std;
int debug = 0;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
inline int read(){
int s=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=s*10+ch-'0';
ch=getchar();
}
return s*f;
}
const int N = 2e5+7;
int c[N];
ll sum[N];
void solve(){
int n=read();
rp(i,1,n) c[i]=read(),sum[i]=sum[i-1]+c[i];
int MIN1=c[1];
int MIN2=c[2];
ll ans=1ll*n*(MIN1+MIN2);
int cnt1=1;
int cnt2=1;
rp(i,3,n){
if(i&1) MIN1=min(MIN1,c[i]),cnt1++;
else MIN2=min(MIN2,c[i]),cnt2++;
ans=min(ans,sum[i]+1ll*(n-cnt1)*MIN1+1ll*(n-cnt2)*MIN2);
}
cout<<ans<<endl;
}
int main(){
//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
//debug = 1;
#endif
//time_t beg, end;
//if(debug) beg = clock();
int T=read();
while(T--) solve();
/*
if(debug) {
end = clock();
printf("time:%.2fs\n", 1.0 * (end - beg) / CLOCKS_PER_SEC);
}
*/
return 0;
}