当前位置:网站首页>GYM103660H. Distance
GYM103660H. Distance
2022-07-19 15:11:00 【HeartFireY】
GYM103660H.Distance
Topic ideas
Definition d i s t ( l , r , x ) dist(l, r, x) dist(l,r,x) Is a point x x x To the interval [ l , r ] [l, r] [l,r] Distance of ( If included, the distance is 0 0 0). seek x x x bring ∑ i = 1 k d i s t ( l i , r i , x ) \sum^k_{i = 1} dist(l_i, r_i, x) ∑i=1kdist(li,ri,x) Minimum .

It's not hard to find out , In fact, for a given set of intervals , Find a vertical line , Make it cross as many intervals as possible , And the distance from the unpaved interval should be as small as possible . Consider how to deal with areas that have not been crossed :
- Initially, we insert an interval , At this time, the axis is in the interval , Insert an endpoint into the left and right stacks ;
- For the newly inserted interval , If the right end point is more left than the rightmost end of the current left , It means that the axis should move to the left , Make it cross the newly inserted interval ;
- If the newly inserted interval , If the left end point is more right than the leftmost end of the current right , It means that the axis should move to the right , Make it cross the newly inserted interval ;
- otherwise , Axis does not need to move , Because the left and right queues are in balance . Then insert the left and right endpoints into the left and right queues respectively .
Code
#include <bits/stdc++.h>
#pragma gcc optimize(2)
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 10;
priority_queue<int, vector<int>, less<int>> leftq;
priority_queue<int, vector<int>, greater<int>> rightq;
inline void solve(){
int n = 0, ans = 0; cin >> n;
leftq.emplace(-INT_MAX);
rightq.emplace(INT_MAX);
for(int i = 1; i <= n; i++){
int l, r; cin >> l >> r;
if(r < leftq.top()){
ans += leftq.top() - r;
rightq.emplace(leftq.top()); leftq.pop();
leftq.emplace(l), leftq.emplace(r);
} else if(l > rightq.top()){
ans += l - rightq.top();
leftq.emplace(rightq.top()); rightq.pop();
rightq.emplace(l), rightq.emplace(r);
} else {
leftq.emplace(l), rightq.emplace(r);
}
cout << ans << endl;
}
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
- Leetcode 1296. Divide the array into a set of continuous numbers (provide an idea)
- 一次函数 T1744 963字符写法
- Chapter 1 preliminary knowledge
- kube-proxy & Service & Endpoint
- MySQL installation
- Li Hongyi machine learning introduction -2022.07.11
- Achieve the effect of software login account by authorizing wechat ~ ~ unfinished
- Leetcode 1275. Trouver le vainqueur de "Jingzi"
- 2、MYSQL介绍
- C - Matrix Chain Multiplication(栈的应用)
猜你喜欢
随机推荐
ICML2022 | 幾何多模態對比錶示學習
End repeated development and personalize the login system in twoorthree times
Damn it, why is there less space on the USB flash drive? It's the EFI partition. Delete it
Li Hongyi machine learning 2022.07.15 -- error
兩種虛擬機的比較
微信小程序9-发布代码
UVA340 Master-Mind Hints
TDesign compositionapi refactoring path
分布式事务的性能设计
5-21 interceptor
B树
Force deduction 912 sorting array notes
Oracle - 锁
Zabbix实现对Redis的监控
微信小程序8-云函数
009 面试题 SQL语句各部分的执行顺序
Top domestic experts gathered in Guangzhou to discuss the safety application of health care data
Li Hongyi machine learning -- return to July 13, 2022
第1章 预备知识
Characteristics of DMA mode








