[题解] LP2234 [HNOI2002] 营业额统计
Problem
自行原题面。
Solution
multiset 在 insert 时支持返回迭代器
则每次摸着迭代器找就行了。
为了简化问题,插入 inf 和 -inf 来避免 RE。
Code
// Code by ajcxsu
// Problem: LP2234
#include<bits/stdc++.h>
using namespace std;
multiset<int> x;
int tmin(int a,int b,int c) {
a=(a<b?a:b);
a=(a<c?a:c);
return a;
}
int main() {
x.insert(0x3f3f3f3f),x.insert(-0x3f3f3f3f);
int n;
cin>>n;
long long tot=0;
int na,ans;
multiset<int>::iterator it;
cin>>na;
x.insert(na);
tot+=na;
while(n--) {
cin>>na;
it=x.insert(na);
auto itb=it,itc=it;
itb--;
itc++;
if(*itb==-0x3f3f3f3f) tot+=abs(*itc-*it);
else if(*itc==0x3f3f3f3f) tot+=abs(*it-*itb);
else tot+=min(abs(*itc-*it), abs(*it-*itb));
}
cout<<tot<<endl;
return 0;
}
本文链接:https://pst.iorinn.moe/archives/sol_luogu_2234.html
许可: https://pst.iorinn.moe/license.html若无特别说明,博客内的文章默认将采用 CC BY 4.0 许可协议 进行许可☆