0
点赞
收藏
分享

微信扫一扫

cf1083E(斜率优化+单调栈+二分)

J简文 2022-08-31 阅读 49


题意:给n个矩形,顶点为(0,0)和(xi,yi)和权值ai,要从中选若干个矩形,使得其价值最大,其价值定义为所有矩形的面积并减去选中矩形的权值和

这个可以对x先排个序,如果x增的时候y也增,那么说明有矩形包含的情况,此时在决策的时候该决策点必然不会被考虑,那么主要考虑一下y递减的情况。。

设d[i]为选i个矩形时的最大价值

d[i]=max{d[j]+y[i]*(x[i]-x[j])-a[i]}=max{d[j]-y[i]*x[j]}+x[i]*y[i]-a[i]

设2个决策点j<k,若j比k优,则

d[j]-y[i]x[j]>d[k]-y[i]*x[k]

(d[j]-d[k])/(x[j]-x[k])<y[i]

那么直接维护上凸包就可以了。。

然而发现y并不是单调的。。

所以在维护凸包后直接在凸包上做二分就行了。。

 

 

 

/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ Code is far away from bug with the animal protecting          
*          ┃ ┃ 神兽保佑,代码无bug
*          ┃ ┃           
*          ┃ ┃       
*          ┃ ┃
*          ┃ ┃           
*          ┃ ┗━━━┓
*          ┃ ┣┓
*          ┃ ┏┛
*          ┗┓┓┏━━━━━━━━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1000005
#define nm 20055
#define N 35
#define pi 3.1415926535897931
const int inf=1e9+7;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}







struct P{
ll x,y,a;
bool operator<(const P&o)const{return x<o.x;}
}a[NM];
int n,s[NM],st;
ll ans,d[NM];
double slope(int x,int y){return (d[x]-d[y])/(a[x].x-a[y].x+eps);}


int main(){
n=read();inc(i,1,n)a[i].x=read(),a[i].y=read(),a[i].a=read();
sort(a+1,a+1+n);
s[st=1]=0;
inc(i,1,n){
int t;
for(int x=1,y=st;x<=y;)
if(mid==st||slope(s[mid],s[mid+1])<a[i].y)t=s[mid],y=mid-1;else x=mid+1;
d[i]=d[t]-a[i].y*a[t].x+a[i].x*a[i].y-a[i].a;
ans=max(ans,d[i]);
while(st>1&&slope(s[st-1],i)<slope(s[st],i))st--;
s[++st]=i;
}
return 0*printf("%lld\n",ans);
}

 

 

 

 

E. The Fair Nut and Rectangles

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The Fair Nut got stacked in planar world. He should solve this task to get out.

You are given ?

rectangles with vertexes in (0,0), (??,0), (??,??), (0,??). For each rectangle, you are also given a number ??. Choose some of them that the area of union minus sum of ??

of the chosen ones is maximum.

It is guaranteed that there are no nested rectangles.

Nut has no idea how to find the answer, so he asked for your help.

Input

The first line contains one integer ?

(1≤?≤106

) — the number of rectangles.

Each of the next ?

lines contains three integers ??, ?? and ?? (1≤??,??≤109, 0≤??≤??⋅??

).

It is guaranteed that there are no nested rectangles.

Output

In a single line print the answer to the problem — the maximum value which you can achieve.

Examples

Input

Copy

3
4 4 8
1 5 0
5 2 10

Output

Copy

9

Input

Copy

4
6 2 4
1 6 2
2 4 3
5 3 8

Output

Copy

10

Note

In the first example, the right answer can be achieved by choosing the first and the second rectangles.

In the second example, the right answer can also be achieved by choosing the first and the second rectangles.

举报

相关推荐

0 条评论