A. Access Points(单调栈&贪心)
Posted Harris-H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A. Access Points(单调栈&贪心)相关的知识,希望对你有一定的参考价值。
A. Access Points(单调栈&贪心)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = 402653189,805306457,1610612741,998244353;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define ios ios::sync_with_stdio(false),cin.tie(nullptr)
void Print(int *a,int n)
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\\n",a[n]);
template <typename T> //x=max(x,y) x=min(x,y)
void cmx(T &x,T y)
if(x<y) x=y;
template <typename T>
void cmn(T &x,T y)
if(x>y) x=y;
int n;
double solve(int a[])
stack<pair<ll,int> >s;
rep(i,1,n)
ll x=a[i],y=1;
while(!s.empty()&&s.top().x*y>=s.top().y*x)
x+=s.top().x;
y+=s.top().y;
s.pop();
s.push(x,y);
int x=n;
double ans=0;
while(!s.empty())
double val = s.top().x;ll cnt=s.top().y;
val/=cnt;
s.pop();
for(int i=x-cnt+1;i<=x;i++)
ans+=(a[i]-val)*(a[i]-val);
x-=cnt;
return ans;
int a[N],b[N];
int main()
scanf("%d",&n);
rep(i,1,n) scanf("%d%d",&a[i],&b[i]);
printf("%f\\n",solve(a)+solve(b));
return 0;
以上是关于A. Access Points(单调栈&贪心)的主要内容,如果未能解决你的问题,请参考以下文章