[HNOI2004]寵物收養所
阿新 • • 發佈:2018-12-22
傳送門
Solution
multiset練習一波?
一直都不太會用
Code
#include<bits/stdc++.h> #define ll long long #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return x*f; } #define itr set<int>::iterator #define mod 1000000 #define inf 2000000000 std::multiset<int> s;bool kind; int n,ans; int main() { n=read(); register int i,k,x; for(i=1;i<=n;i++) { k=read(),x=read(); if(!s.size()) s.insert(x),kind=k; else if(kind==k) s.insert(x); else { std::multiset<int>::iterator l,r=s.lower_bound(x); if(r==s.end()) l=--r,(ans+=x-*l)%=mod,s.erase(l); else if(r==s.begin()) (ans+=*r-x)%=mod,s.erase(r); else { l=--s.lower_bound(x); if(*r-x>=x-*l) (ans+=x-*l)%=mod,s.erase(l); else (ans+=*r-x)%=mod,s.erase(r); } } } printf("%d\n",ans%mod); return 0; }
Blog來自PaperCloud,未經允許,請勿轉載,TKS!