Rick is really fond of fruit juices, but he is bored of their traditional flavours. Therefore, he has decided to mix as many of them as possible to obtain something entirely new as a result.
He has N glasses, numbered from 0 to N-1, each containing a different kind of juice. The J-th glass has capacity[J] units of capacity and contains juice[J] units of juice. In each glass there is at least one unit of juice.
Rick want to create a multivitamin mix in one of the glasses. He is going to do it by pouring juice from several other glasses into the chosen one. Each of the used glasses must be empty at the end (all of the juice from each glass has to be poured out).
What is the maximum number of flavours that Rick can mix?
Write a function:
int solution(vector<int> &juice, vector<int> &capacity);
that, given arrays juice and capacity, both of size N, returns the maximum number of flavours that Rick can mix in a single glass.
Examples:
1. Given juice = [10, 2, 1, 1] and capacity = [10, 3, 2, 2], your function should return 2. Rick can pour juice from the 3rd glass into the 2nd one.
2. Given juice = [1, 2, 3, 4] and capacity = [3, 6, 4, 4], your function should return 3. Rick can pour juice from the 0th and 2nd glasses into the 1st one.
3. Given juice = [2, 3] and capacity = [3, 4], your function should return 1. No matter which glass he chooses, Rick cannot pour juice from the other one into it. The maximum number of flavours in the chosen glass is 1.
4. Given juice = [1, 1, 5] and capacity = [6, 5, 8], your function should return 3. Rick can mix all juices in the 2nd glass.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of arrays juice and capacity is an integer within the range [1..1,000,000,000];
- arrays juice and capacity have the same length, equal to N;
- for each J juice[J] ≤ capacity[J].
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
for(int i=0; i<size(); i++) {
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L,)
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid <)
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0]
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
}
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
} else {
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num =
} else {
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num =
} else {
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num =
} else {
total
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+1;
} else {
total = psum[mid]
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+1;
} else {
total = psum[mid]-V[i].first;
num = mid;
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+1;
} else {
total = psum[mid]-V[i].first;
num = mid;
}
if (total )
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid;
} else {
total = psum[mid]-V[i].first;
num = mid;
}
if (total )
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid;
}
if (total )
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total )
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks)
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
} else {
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int maks = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
func.cpp: In function 'int solution(std::vector<int>&, std::vector<int>&)': func.cpp:13:28: error: 'sort' was not declared in this scope sort(V.begin(), V.end()); ^ func.cpp:22:25: error: 'size' cannot be used as a function for(int i=0; i<size(); i++) { ^ func.cpp:29:17: error: 'total' was not declared in this scope total = psum[mid]; ^~~~~ func.cpp:30:17: error: 'num' was not declared in this scope num = mid+2; ^~~ func.cpp:32:17: error: 'total' was not declared in this scope total = psum[mid]-V[i].first; ^~~~~ func.cpp:33:17: error: 'num' was not declared in this scope num = mid+1; ^~~ func.cpp:35:17: error: 'total' was not declared in this scope if (total <= V[i].second) { ^~~~~ func.cpp:36:34: error: 'num' was not declared in this scope maks = max(maks, num); ^~~
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<int> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size(); i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
func.cpp: In function 'int solution(std::vector<int>&, std::vector<int>&)': func.cpp:13:28: error: 'sort' was not declared in this scope sort(V.begin(), V.end()); ^ func.cpp:22:25: error: 'size' cannot be used as a function for(int i=0; i<size(); i++) { ^
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
func.cpp: In function 'int solution(std::vector<int>&, std::vector<int>&)': func.cpp:13:28: error: 'sort' was not declared in this scope sort(V.begin(), V.end()); ^
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
// you can use includes, for example:
#include <algorithm>
#include <pair>
#include <utility>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
func.cpp:3:16: fatal error: pair: No such file or directory #include <pair> ^ compilation terminated.
// you can use includes, for example:
#include <algorithm>
#include <vector>
#include <utility>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
// you can use includes, for example:
#include <algorithm>
#include <vector>
#include <utility>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]-j));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
// you can use includes, for example:
#include <algorithm>
#include <vector>
#include <utility>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]-juice[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
// you can use includes, for example:
#include <algorithm>
#include <vector>
#include <utility>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]-juice[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
// you can use includes, for example:
#include <algorithm>
#include <vector>
#include <utility>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
vector<pair<int,int> > V;
int size = juice.size();
for(int i=0; i<size; i++) {
V.push_back(make_pair(juice[i], capacity[i]-juice[i]));
}
sort(V.begin(), V.end());
vector<long long> psum(size);
psum[0] = V[0].first;
for(int i=1; i<size; i++) {
psum[i] = psum[i-1] + V[i].first;
}
int ans = 0;
for(int i=0; i<size; i++) {
int L=0;
int R=size-1;
int maks = 0;
while(L<=R) {
int mid = (L+R)/2, num;
long long total;
if (mid < i) {
total = psum[mid];
num = mid+2;
} else {
total = psum[mid]-V[i].first;
num = mid+1;
}
if (total <= V[i].second) {
maks = max(maks, num);
L = mid+1;
} else {
R = mid-1;
}
}
ans = max(ans, maks);
}
return ans;
}
The solution obtained perfect score.
Tests in which the glass with the biggest capacity is not the one that should be picked as the base.
Tests in which the glass with the largest empty space is not the picked as the base.