思路:
三分;
代码:
#include <bits/stdc++.h> using namespace std; #define maxn 100005 #define ll long long struct DataType { ll pi,val,sumval,sumpi; bool operator<(const DataType pos)const { return pi<pos.pi; } }; struct DataType ai[maxn]; ll len,n; inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>‘9‘||Cget<‘0‘)Cget=getchar(); while(Cget>=‘0‘&&Cget<=‘9‘) { now=now*10+Cget-‘0‘; Cget=getchar(); } } ll lower(ll key) { ll l=1,r=n,res=1,mid; while(l<=r) { mid=l+r>>1; if(key<=ai[mid].pi) res=mid,r=mid-1; else l=mid+1; } return res; } ll calculate(ll key) { ll now=lower(key); ll res1=key*ai[now-1].sumval-ai[now-1].sumpi; ll res2=ai[n].sumpi-ai[now-1].sumpi-key*(ai[n].sumval-ai[now-1].sumval); return res2+res1; } int main() { in(len),in(n); for(ll i=1;i<=n;i++) in(ai[i].pi),in(ai[i].val); sort(ai+1,ai+n+1); for(ll i=1;i<=n+1;i++) { ai[i].sumval=ai[i-1].sumval+ai[i].val; ai[i].sumpi=ai[i-1].sumpi+ai[i].val*ai[i].pi; } ll l=0,r=len,mid,now,nowl,nowr; while(l<r) { mid=l+r>>1; now=calculate(mid); nowl=calculate(mid-1); nowr=calculate(mid+1); if(nowl<now) r=mid-1; else if(nowr<now) l=mid+1; else { printf("%lld\n",calculate(mid)); return 0; } } printf("%lld\n",calculate(l)); return 0; }
原文:http://www.cnblogs.com/IUUUUUUUskyyy/p/7103685.html