PROBLEM LINK:
Contest - Division 4
Contest - Division 3
Contest - Division 2
Contest - Division 1
DIFFICULTY:
EASY
PREREQUISITES:
PROBLEM:
There are N training plans. Training plan i has effectiveness A_i, but requires that atleast B_i other training plans have been performed before selecting this plan.
If training plans p_1, p_2,\dots,p_k are performed, the score is equal to \frac{\sum A_{p_i}}{k}. Determine the maximum possible score attainable.
EXPLANATION:
We proceed greedily.
Let S represent the set of all (previously unselected) training plans that we can select currently. Initially, S consists of all plans i where B_i=0.
Also, let set K comprise all plans that we have selected. Initially, K is empty. Finally, let ans represent the maximum score we have attained so far. Initially, ans=0.
In each move, do the following:
- select the plan in S with the largest effectiveness value; break if S is empty.
- insert this plan into K and remove it from S.
- update ans := \max(ans, \frac{\sum A_{K_i}}{|K|}).
- insert all plans i with B_i=|K| to S.
It is easy to see why this works. We pick plans one by one (till we can select no more). By the greedy criteria, it is optimal to always select a selectable plan with the highest effectiveness value. Finally, we update the best answer with the current score.
TIME COMPLEXITY:
Since each training plan is inserted/removed from the priority queue atmost once, the time complexity is therefore
per test case.
SOLUTIONS:
Editorialist’s solution can be found here.
Author's solution
To be added.
Tester's solution
// Super Idol的笑容
// 都没你的甜
// 八月正午的阳光
// 都没你耀眼
// 热爱105°C的你
// 滴滴清纯的蒸馏水
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n;
int arr[100005];
int brr[100005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
int TC;
cin>>TC;
while (TC--){
cin>>n;
rep(x,0,n) cin>>arr[x];
rep(x,0,n) cin>>brr[x];
vector<int> idx;
rep(x,0,n) idx.pub(x);
sort(all(idx),[](int i,int j){
return brr[i]>brr[j];
});
double ans=0;
int tot=0;
priority_queue<int> pq;
rep(x,0,n){
while (!idx.empty() && brr[idx.back()]==x){
pq.push(arr[idx.back()]);
idx.pob();
}
if (pq.empty()) break;
tot+=pq.top();
pq.pop();
ans=max(ans,(double)tot/(x+1));
}
cout<<fixed<<setprecision(12)<<ans<<endl;
}
}