-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaximum Number of Toys.cpp
141 lines (119 loc) · 3.11 KB
/
Maximum Number 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//{ Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
using ll = long long;
class Solution{
public:
vector<int> maximumToys(int N,vector<int> A,int Q,vector<vector<int>> Queries){
// code here
// creating an array of pair for cost and index. Then sorting.
vector<pair<int,int>> arr(N);
for(int i=0; i<N; i++) arr[i] = {A[i],i};
sort(arr.begin(),arr.end());
// creating a map for original index to new sorted index
map<int,int> mp;
for(int i=0; i<N; i++)
{
mp[arr[i].second] = i;
}
// prefix sum and binary search for maximum toys we can buy
vector<ll> prefix(N+1);
prefix[0] = 0;
for(int i=1; i<=N; i++)
{
prefix[i] = prefix[i-1] + arr[i-1].first;
}
// result vector for Q queries
vector<int> res(Q);
for(int i=0; i<Q; i++)
{
ll c = Queries[i][0];
int k = Queries[i][1];
int l = 0;
int r = N;
int ans = 0;
while(l<=r)
{
int mid = l + (r-l)/2;
if(prefix[mid]<=c)
{
ans = mid;
l = mid+1;
}
else r = mid-1;
}
if(ans <= 0)
{
res[i] = 0;
continue;
}
// index of last purchase, idx is ans-1 (0 based indexing used).
int idx = ans-1;
// remaining money, rem
ll rem = c - prefix[ans];
// set notRemoved to check if toys are broken after idx, when we try to purchase with remaining money
set<int> notRemoved;
// checking for all k toys
for(int j = 0; j<k; j++)
{
// qIdx gives the index of broken toy in sorted array
int qIdx = mp[Queries[i][j+2] - 1];
if(qIdx<=idx)
{
ans--;
rem += arr[qIdx].first;
}
else notRemoved.insert(qIdx);
}
// purchasing with remaining money
int s = idx+1;
while(s<N && arr[s].first<=rem)
{
if(!notRemoved.count(s))
{
ans++;
rem-=arr[s].first;
}
s++;
}
// storying the ans in res vector
res[i] = ans;
}
return res;
}
};
//{ Driver Code Starts.
int main() {
int t=1;
cin>>t;
for(int i=1;i<=t;i++){
int N;
cin>>N;
vector<int>A(N);
for(auto &i:A){
cin>>i;
}
int Q;
cin>>Q;
vector<vector<int>> Queries(Q);
for(int i=0;i<Q;i++){
int x,y;
cin>>x>>y;
Queries[i].push_back(x);
Queries[i].push_back(y);
for(int j=0;j<Queries[i][1];j++){
cin>>x;
Queries[i].push_back(x);
}
}
Solution obj;
auto ans =obj.maximumToys(N,A,Q,Queries);
for(auto i:ans)
cout<<i<<" ";
cout<<endl;
}
}
// } Driver Code Ends