首页 > 代码库 > pro
pro
代码
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 101000; struct Node{ double x,y; Node(){} Node(double x,double y):x(x),y(y){} }nod[N]; Node operator + (const Node &a,const Node &b){ return Node(a.x+b.x,a.y+b.y); } Node operator - (const Node &a,const Node &b){ return Node(a.x-b.x,a.y-b.y); } double dot(const Node &a,const Node &b){ return a.x*b.x+a.y*b.y; } double cross(const Node &a,const Node &b){ return a.x*b.y-a.y*b.x; } ll ans,n,m,a[N],b[N],pre[N],num[N]; bool check(int,int); bool check(Node&,Node&,Node&); int main(){ scanf("%lld%lld",&n,&m); for (int i = 1;i <= n;i++) scanf("%lld",&a[i]); for (int i = 1;i <= m;i++) scanf("%lld",&b[i]); for (int i = 1;i <= n;i++) pre[i] = pre[i-1]+a[i]; int t = 1,w = 2; nod[1] = Node(pre[1],pre[1]-a[1]);num[1] = 1; nod[2] = Node(pre[2],pre[2]-a[2]);num[2] = 2; for (int i = 3;i <= n;i++){ Node c = Node(pre[i],pre[i]-a[i]); while (t < w && check(nod[w-1],nod[w],c)) w--; nod[++w] = c;num[w] = i; } for (int i = 2;i <= m;i++){ int l = t,r = w,mid = l + r >> 1; while (l < r){ if (check(mid,i)) l = mid + 1;else r = mid; mid = l + r >> 1; } ans += pre[num[l]]*b[i-1]-pre[num[l]-1]*b[i]; } ans += pre[n]*b[m]; printf("%lld\n",ans); return 0; } bool check(int x,int y){ // return (nod[x].y-nod[x+1].y)/(nod[x].x-nod[x+1].x)<b[y-1]/b[y]; return (nod[x+1].y-nod[x].y)*b[y]<(nod[x+1].x-nod[x].x)*b[y-1]; } bool check(Node& x,Node& y,Node& z){ return cross(y-x,z-x) <= 0; }
pro
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。