Skip to content

Commit 424c5e2

Browse files
committed
CF Update
1 parent 49140e2 commit 424c5e2

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

Codeforces/1300/263-2-B.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int main(int argc, char const *argv[])
2020
long long answer = 0;
2121
for(int i = 0 ; i < 26 && k != 0 ; ++i){
2222
int can = min(cards[i], k);
23-
if(can <= cards[i]){ // if I can't take more than what cards[i] has, then double the quantity
23+
if(can <= cards[i]){ // if I can't take more than what cards[i] has, then square the quantity
2424
answer += 1LL * can * can;
2525
} else{ // I can take more than what cards[i] has
2626
answer += can;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
// is p a sub-sequence of modified
8+
bool isSubsequence(const string &modified, const string &p){
9+
int index = 0;
10+
for(int i = 0 ; i < modified.length() && index < p.length() ; ++i){
11+
if(modified[i] == p[index]){
12+
++index;
13+
}
14+
}
15+
return index == p.length();
16+
}
17+
18+
bool ok(const string &t, const string &p, const vector <int> &perm, int value){
19+
string modified{t};
20+
for(int i = 0; i < value ; ++i){
21+
modified[perm[i] - 1] = '#'; // any dummy character
22+
}
23+
return isSubsequence(modified, p);
24+
}
25+
26+
int binary_search(const string &t, const string &p, const vector <int> &perm){
27+
int low = 0, high = t.length() - 1;
28+
int ans = -1;
29+
while(low <= high){
30+
int mid = (low + high) / 2;
31+
if(ok(t, p, perm, mid)){
32+
ans = mid;
33+
low = mid + 1;
34+
} else{
35+
high = mid - 1;
36+
}
37+
}
38+
return ans;
39+
}
40+
41+
int main(int argc, char const *argv[])
42+
{
43+
// ios_base::sync_with_stdio(false);
44+
// cin.tie(0);
45+
string t, p;
46+
cin >> t >> p;
47+
vector <int> perm(t.length());
48+
for(int i = 0 ; i < perm.size() ; ++i){
49+
cin >> perm[i];
50+
}
51+
cout << binary_search(t, p, perm) << '\n';
52+
return 0;
53+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
bool ok(const vector <int> &g, int k, long long councils){
8+
long long sum = 0;
9+
for(int i = 0 ; i < (int) g.size() ; ++i){
10+
long long limit = min(1LL * g[i], councils);
11+
sum += limit;
12+
}
13+
return sum / councils >= k;
14+
}
15+
16+
long long binary_search(const vector <int> &g, int k){
17+
long long low = 1, high = 1e11;
18+
long long ans = 0;
19+
while(low <= high){
20+
long long mid = (low + high) / 2;
21+
if(ok(g, k, mid)){
22+
ans = mid;
23+
low = mid + 1;
24+
} else{
25+
high = mid - 1;
26+
}
27+
}
28+
return ans;
29+
}
30+
31+
int main(int argc, char const *argv[])
32+
{
33+
int k;
34+
cin >> k;
35+
int n;
36+
cin >> n;
37+
vector <int> g(n);
38+
for(int i = 0 ; i < n ; ++i){
39+
cin >> g[i];
40+
}
41+
cout << binary_search(g, k) << '\n';
42+
return 0;
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
bool ok(int b, int s, int c, int nb, int ns, int nc, int pb, int ps, int pc, long long r, long long value){
7+
// with the ingredients I already have, I can make 'minimum' amount of burgers
8+
int minimum = min({b == 0 ? 0 : nb / b, s == 0 ? 0 : ns / s, c == 0 ? 0 : nc / c});
9+
nb -= b * minimum;
10+
ns -= s * minimum;
11+
nc -= c * minimum;
12+
if(minimum >= value) return true;
13+
value -= minimum;
14+
// now I cannot make any more burgers without buying
15+
// I have to make the remaining 'value' amount of burgers with 'r' rubles
16+
if(nb < value * b){ // need extra bread
17+
long long bn = value * b - nb; // this amount of extra bread is needed
18+
if(r < bn * pb) return false;
19+
r -= bn * pb;
20+
}
21+
if(ns < value * s){ // need extra sausage
22+
long long sn = value * s - ns;
23+
if(r < sn * ps) return false;
24+
r -= sn * ps;
25+
}
26+
if(nc < value * c){ // need extra cheese
27+
long long cn = value * c - nc;
28+
if(r < cn * pc) return false;
29+
}
30+
return true;
31+
}
32+
33+
long long binary_search(int b, int s, int c, int nb, int ns, int nc, int pb, int ps, int pc, long long r){
34+
long long low = 0, high = 1e13;
35+
long long ans = -1;
36+
while(low <= high){
37+
long long mid = (low + high) / 2;
38+
if(ok(b, s, c, nb, ns, nc, pb, ps, pc, r, mid)){
39+
ans = mid;
40+
low = mid + 1;
41+
} else{
42+
high = mid - 1;
43+
}
44+
}
45+
return ans;
46+
}
47+
48+
int main(int argc, char const *argv[])
49+
{
50+
string recipe;
51+
cin >> recipe;
52+
int b = 0, s = 0, c = 0;
53+
for(int i = 0 ; i < recipe.length() ; ++i){
54+
if(recipe[i] == 'B') ++b;
55+
else if(recipe[i] == 'S') ++s;
56+
else ++c;
57+
}
58+
int nb, ns, nc;
59+
cin >> nb >> ns >> nc;
60+
int pb, ps, pc;
61+
cin >> pb >> ps >> pc;
62+
long long r;
63+
cin >> r;
64+
cout << binary_search(b, s, c, nb, ns, nc, pb, ps, pc, r) << '\n';
65+
return 0;
66+
}

0 commit comments

Comments
 (0)