题目链接:https://atcoder.jp/contests/abc127/tasks/abc127_f

题目大意

  初始状态下$f(x) = 0$,现在有 2 种模式的询问,第一种以“1 a b”的形式,需要进行操作$f(x) = f(x) + |x - a| + b$;第二种以“2”的形式,求使得 f(x) 取得最小值的 x 取值和 f(x) 值,如果有多个 x,输出任意一个即可。

分析

  考虑第一种询问已经出现了 k 次,现在遇到第二种询问。此时$f(x) = \sum_{i = 1}^k (|x - a_i| + b_i)$,可以发现$b_i$完全是独立于 x 的,所以只要讨论$\sum_{i = 1}^k |x - a_i|$的最小值即可。
  于是问题就退化成在数轴上找一个点,使得它到给定的 k 个点的距离之和最短。
  当点个数为奇数,最优解是中位数。 
  当点个数为偶数,最优解是位于中间的两个数中任取一个。
  所以这个问题的本质就是让你动态找中位数,维护中位数可以用双堆。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int Q;
PQIMax maxH;
PQIMin minH;
LL sumB, sumLA, sumRA, ans; int main(){
INIT();
cin >> Q;
while(Q--) {
int x, a, b;
cin >> x;
if(x == ) {
cin >> a >> b;
sumB += b;
if(maxH.size() == minH.size()) {
if(maxH.empty()) {
maxH.push(a);
sumLA += a;
}
else if(a >= maxH.top()) {
minH.push(a);
sumRA += a;
}
else {
maxH.push(a);
sumLA += a;
}
}
else if(maxH.size() > minH.size()) {
if(a >= maxH.top()) {
minH.push(a);
sumRA += a;
}
else {
maxH.push(a);
sumLA += a;
sumLA -= maxH.top();
minH.push(maxH.top());
sumRA += maxH.top();
maxH.pop();
}
}
else {
if(a < minH.top()) {
maxH.push(a);
sumLA += a;
}
else {
minH.push(a);
sumRA += a;
sumRA -= minH.top();
maxH.push(minH.top());
sumLA += minH.top();
minH.pop();
}
}
}
else {
int x = maxH.top();
ans = sumRA - sumLA + sumB;
if(maxH.size() > minH.size()) ans += maxH.top();
else if(maxH.size() < minH.size()) {
ans -= minH.top();
x = minH.top();
}
cout << x << " " << ans << endl;
}
}
return ;
}

最新文章

  1. .NET WEB项目的调试发布相关
  2. [翻译] 聚集索引表 VS 堆表
  3. 图解集合5:不正确地使用HashMap引发死循环及元素丢失
  4. JQuery-文档处理&amp;选择器
  5. 黑马程序员——JAVA基础之反射
  6. [WinAPI] API 7 [判断光驱内是否有光盘]
  7. SVN提交数据失败问题(提示 svn:MKACTIVITY ... 403 Forbidden )
  8. [转载]PHP 字符串替换中文
  9. 【转】EditText获取焦点不自动弹出键盘设置--失去焦点的方法,不错
  10. Dyanmics CRM您无法登陆系统。原因可能是您的用户记录或所属的业务部门在Microoft Dynamics CRM中已被禁用
  11. D3D游戏降帧的动态创建D3D设备以及ShellCode HOOK玩法
  12. PHP中try{}catch{}的具体用法详解
  13. Xcode iOS布局autolayout和sizeclass的使用
  14. Dependency Walker的替代品Dependencies
  15. 【题解】JSOIWC2019 Round3
  16. fork多线程进程时的坑(转)
  17. 比赛总结——atcoder beginner contest 109
  18. 绘图:Matplotlib
  19. android 获取http请求json数据
  20. Visio2010如何安装

热门文章

  1. 谈html mailto(电子邮件)实际应用
  2. (转)短信vs.推送通知vs.电子邮件:app什么时候该用哪种方式来通知用户?
  3. CF 1097D - Hello 2019 D题: Makoto and a Blackboard
  4. 实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历
  5. [经典]Python 一篇学会多线程
  6. 4. Jmeter主界面的介绍
  7. 拾遗:git pull 与 push 远程分支与本地分支顺序识别问题
  8. 网络编程(四)——基于udp协议的套接字socket、socketserver模块的使用
  9. adapter设计模式
  10. HBase启动错误提示别的机器60000已经存在