做法
每次用最小的号来上分掉分即可
用优先队列或者multiset维护当前最小的号即可
代码
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp(aa,bb) make_pair(aa,bb) #define _for(i,b) for(int i=(0);i<(b);i++) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,b,a) for(int i=(b);i>=(a);i--) #define mst(abc,bca) memset(abc,bca,sizeof abc) #define X first #define Y second #define lowbit(a) (a&(-a)) #define debug(a) cout<<#a<<":"<<a<<"\n" typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef long double ld; const int N=100010; const int INF=0x3f3f3f3f; const int mod=1e9+7; const double eps=1e-6; const double PI=acos(-1.0); int n,m; multiset<ld> s; ld sum; void solve(){ cin>>n>>m; rep(i,1,n){ int x;cin>>x; s.insert(x); sum+=x; } while(m--){ int x;cin>>x; ld t=*s.begin(); if(x>=t){ sum-=t; sum+=(x+t)/2; s.erase(s.begin()); s.insert((x+t)/2); cout<<fixed<<setprecision(2)<<sum<<"\n"; continue; } auto p=s.lower_bound(x); if(p==s.end()){ t=*(--p); sum-=t; sum+=(x+t)/2; s.erase(p); s.insert((x+t)/2); } else{ sum-=*p; sum+=(x+*p)/2; s.erase(p); s.insert((x+*p)/2); } cout<<fixed<<setprecision(2)<<sum<<"\n"; } } int main(){ ios::sync_with_stdio(0);cin.tie(0); // int t;cin>>t;while(t--) solve(); return 0; }