AcWing 1281. 宠物收养所
原题链接
中等
作者:
Fau
,
2021-03-24 08:24:16
,
所有人可见
,
阅读 362
这么可爱的题的为什么没有set暴力的题解呢
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define debug(a) cout << #a << " = " << a << endl;
using namespace std;
typedef long long ll;
const int mod = 1E6;
const ll INF = 0x3f3f3f3f3f3f3f3f;
set<ll> st;
int main()
{
st.insert(-INF), st.insert(INF);
int n; cin >> n;
int res = 0, flag = -1;
rep(i, n) {
int a, b; scanf("%d %d", &a, &b);
if (a == flag || st.size() == 2) st.insert(b), flag = a;
else {
auto index = st.lower_bound(b);
ll val1 = *index, val2 = *--index;
ll cha1 = abs(b - val2), cha2 = abs(b - val1); //reverse
if (cha1 <= cha2) res = (res + cha1) % mod;
else ++index, res = (res + cha2) % mod;
st.erase(index);
}
}
printf("%d\n", res);
return 0;
}