-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum_No_Of_Toys.cpp
73 lines (68 loc) · 1.38 KB
/
Maximum_No_Of_Toys.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const int N=1e6+5;
int fr[N];
class tree{
public:
vector<long long> bit;
tree(){
bit=vector<long long>(N);
}
void add(int node,int v)
{
for(;node<N;node+=(node&(-node)))
bit[node]+=v;
}
long long get(int node)
{
long long x=0;
for(;node>0;node-=(node&(-node)))
x+=bit[node];
return x;
}
};
tree obj1,obj2;
class Solution{
public:
vector<int> maximumToys(int N,vector<int> A,int Q,vector<vector<int>> Queries){
vector<int> res;
for(auto i:A){
fr[i]++;
}
for(int i=0;i<A.size();i++){
obj1.add(A[i],A[i]);
obj2.add(A[i],1);
}
for(auto i:Queries){
long long C=i[0];
for(int j=2;j<i.size();j++){
obj1.add(A[i[j]-1],-A[i[j]-1]);
obj2.add(A[i[j]-1],-1);
}
long long lw=1,hg=1e6;
long long pos=1e6;
while(lw<=hg){
int mid=(lw+hg)/2;
if(obj1.get(mid)>=C){
pos=mid;
hg=mid-1;
}
else{
lw=mid+1;
}
}
long long ans=obj2.get(pos-1);
long long mx=min((C-obj1.get(pos-1))/pos,(long long)fr[pos]);
ans+=mx;
res.push_back(ans);
for(int j=2;j<i.size();j++){
obj1.add(A[i[j]-1],A[i[j]-1]);
obj2.add(A[i[j]-1],1);
}
}
for(int i=0;i<A.size();i++){
obj1.add(A[i],-A[i]);
obj2.add(A[i],-1);
fr[A[i]]--;
}
return res;
}
};