题意:中文题。。
分析:很显然我们将E定在一个端点处是最优的。定在哪呢?扫一遍咯,同时用两个标记标记E前面的左端点个数和右端点个数和一个E后面的左端点总和sum即可。记得ans和sum都要用long long 即可。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int MAX=151;
const int MOD=1000007;
const int MOD1=100000007;
const int MOD2=100000009;
const int INF=2100000000;
const double EPS=0.00000001;
typedef long long ll;
typedef unsigned long long ull;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node {
int b,w;
}a[2*N];
int cmd(node x,node y) {
if (x.w!=y.w) return x.w<y.w;
return x.b<y.b;
}
int main()
{
int i,n,l,r,k=0,whe;
ll sum=0,ans;
scanf("%d", &n);
for (i=1;i<=n;i++) {
scanf("%d%d", &l, &r);
sum+=(ll)l;
k++;a[k].b=0;a[k].w=l;
k++;a[k].b=1;a[k].w=r;
}
sort(a+1,a+k+1,cmd);
ans=whe=0;l=0;r=0;
for (i=1;i<=k;i++) {
if ((ll)(l-r)*a[i].w+sum>ans) {
whe=a[i].w;ans=(ll)(l-r)*a[i].w+sum;
}
if (a[i].b) r++;
else { l++;sum-=a[i].w; }
}
printf("%d %lld\n", whe, ans);
return 0;
}
/*
2
5 10
20 25
*/