https://ac.nowcoder.com/acm/contest/8282/C
题意:多次操作使一个数变成旁边两个数的平均值,求序列中数字和最小是多少
来自chsyao的详解:
多次操作使一个数变成旁边两个数的平均值,求序列中数字和最小是多少。
可以发现当一段区间变成等差数列后就不能再进行修改了。
问题变为选定若干等差数列的首项末项,使数列和最小。
又注意到一段区间能变成等差数列当且仅当这段区间除了首项末项以外的数都比变成等差数列后对应位置上的数大。
于是可以转化问题,有 n 个点,坐标分别为 (i,ai)。
把一个区间覆盖成等差数列相当于在首尾的点之间连线。
发现要求的实际上就是这些点的下凸壳。
个人理解:每次去构造总是朝着使序列整体下降去构造,通过平面图去画发现最后变成的是一个下凸包。
然后就是上凸包,去学了一下凸包。
最后要求的是极点之间的点落到红线上的距离总和。由于每一个点的首尾是等差的,根据等差中项的话和直接就是首尾相加/2*中间的点的个数。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e6+200;
typedef long long LL;
double a[maxn];
LL n;
LL tot=0;///tot为凸包的个数
struct point
{
double x,y;
}p[maxn],CHP[maxn];///CHP为凸包最后所构成的点
bool cmp(point A,point B)
{
if(A.x==B.x) return A.y<B.y;
return A.x<B.x;
}
double xmul(point a,point b,point c)///叉积
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double Andrew()
{
sort(p+1,p+1+n,cmp);
tot=0;
for(LL i=1;i<=n;i++)///计算下凸包
{
while(tot>1&&xmul(CHP[tot-1],CHP[tot],p[i])<=0)
--tot;
CHP[++tot]=p[i];
}
double ans=0;
for(LL i=2;i<=tot;i++)
{
ans+=(CHP[i].x-CHP[i-1].x+1)*(CHP[i].y+CHP[i-1].y)/2.00-CHP[i].y;
}
ans+=(double)CHP[tot].y;
LL k=tot;
for(LL i=n-2;i>=1;i--)///计算上凸包
{
while(tot>k&&xmul(CHP[tot-1],CHP[tot],p[i])<=0)
--tot;
CHP[++tot]=p[i];
}
if(n>1) tot--;
return ans;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n;
for(LL i=1;i<=n;i++) {
cin>>a[i];
p[i].x=i;
p[i].y=a[i];
}
if(n==1){
cout<<a[1]<<endl;
return 0;
}
else if(n==2){
cout<<a[1]+a[2]<<endl;
return 0;
}
else{
double c=Andrew();
printf("%.10f\n",c);
}
return 0;
}