Tutorial is loading...
Authors: MikeMirzayanov and geranazavr555
Authors' solution
def solve(a, b, c, d):
return max(0, d - (b - a)) + max(0, d - (c - b))
def main():
a, b, c, d = map(int, input().split())
a, b, c = sorted((a, b, c))
print(solve(a, b, c, d))
main()
Tutorial is loading...
Author: MikeMirzayanov
Authors' solution
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
vector<pair<char,int>> split(string s) {
char c = s[0];
int cnt = 1;
vector<pair<char,int>> result;
auto ss = s.c_str();
for (int i = 1; i <= int(s.length()); i++) {
if (ss[i] != c) {
result.push_back({c, cnt});
c = s[i];
cnt = 1;
} else
cnt++;
}
return result;
}
int main() {
int n;
cin >> n;
forn(tt, n) {
string s, t;
cin >> s >> t;
auto sa(split(s)), ta(split(t));
bool ok = sa.size() == ta.size();
if (ok)
forn(i, sa.size())
if (sa[i].first != ta[i].first || sa[i].second > ta[i].second)
ok = false;
cout << (ok ? "YES" : "NO") << endl;
}
}
Tutorial is loading...
Authors: MikeMirzayanov, cannor147 and geranazavr555
Authors' solution
#include <bits/stdc++.h>
using namespace std;
const int T = 100;
int main() {
int n, m;
cin >> n >> m;
int sum = 0;
vector<int> t(n), count(T + 1, 0);
for (int i = 0; i < n; i++) {
cin >> t[i];
}
for (int i = 0; i < n; i++) {
int d = sum + t[i] - m, k = 0;
if (d > 0) {
for (int j = T; j > 0; j--) {
int x = j * count[j];
if (d <= x) {
k += (d + j - 1) / j;
break;
}
k += count[j];
d -= x;
}
}
sum += t[i];
count[t[i]]++;
cout << k << " ";
}
}
Tutorial is loading...
Authors: MikeMirzayanov, cannor147 and geranazavr555
Authors' solution
#include <bits/stdc++.h>
using namespace std;
const int T = 100;
int main() {
int n, m;
cin >> n >> m;
int sum = 0;
vector<int> t(n), count(T + 1, 0);
for (int i = 0; i < n; i++) {
cin >> t[i];
}
for (int i = 0; i < n; i++) {
int d = sum + t[i] - m, k = 0;
if (d > 0) {
for (int j = T; j > 0; j--) {
int x = j * count[j];
if (d <= x) {
k += (d + j - 1) / j;
break;
}
k += count[j];
d -= x;
}
}
sum += t[i];
count[t[i]]++;
cout << k << " ";
}
}
Tutorial is loading...
Author: MikeMirzayanov
Authors' solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int>> b(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
b[i] = {x, i};
}
sort(b.begin(), b.end());
vector<int> d(n - 1);
for (int i = 1; i < n; i++) {
d[i - 1] = b[i].first - b[i - 1].first;
}
bool f = true;
for (int i = 2; i < n - 1; i++) {
f &= d[i] == d[1];
}
if (f) {
cout << b[0].second + 1;
return 0;
}
f = true;
for (int i = 2; i < n - 1; i++) {
f &= d[i] == b[2].first - b[0].first;
}
if (f) {
cout << b[1].second + 1;
return 0;
}
int answer;
f = false;
for (int i = 1; i < n - 1; i++) {
if (d[i] == d[0]) continue;
if (f) {
cout << -1;
return 0;
} else {
if (i == n - 2) {
cout << b[n - 1].second + 1;
return 0;
} else {
if (b[i + 2].first - b[i].first == d[0]) {
answer = b[i + 1].second;
f = true;
i++;
} else {
cout << -1;
return 0;
}
}
}
}
if (f) {
cout << answer + 1;
} else {
cout << b[n - 1].second + 1;
}
}
Tutorial is loading...
Authors: MikeMirzayanov, cannor147 and geranazavr555
Authors' solution
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
int min(int a, int b) {
return (a < b) ? a : b;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
int t;
cin >> t;
vector<string> lines(2000, "");
vector<int> vertical_min(26, 2001), vertical_max(26, -1);
vector<int> horizontal_min(26, 2001), horizontal_max(26, -1);
for (int k = 0; k < t; k++) {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> lines[i];
}
for (int b = 0; b < 26; b++) {
vertical_min[b] = 2001;
horizontal_min[b] = 2001;
vertical_max[b] = -1;
horizontal_max[b] = -1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (lines[i][j] != '.') {
int b = (int) (lines[i][j] - 'a');
vertical_min[b] = min(vertical_min[b], i);
vertical_max[b] = max(vertical_max[b], i);
horizontal_min[b] = min(horizontal_min[b], j);
horizontal_max[b] = max(horizontal_max[b], j);
}
}
}
bool f = true, q = false;
int count = 26;
for (int b = 25; b >= 0; b--) {
if (vertical_max[b] == -1) {
if (!q) {
count--;
continue;
} else {
vertical_min[b] = vertical_min[b + 1];
vertical_max[b] = vertical_max[b + 1];
horizontal_min[b] = horizontal_min[b + 1];
horizontal_max[b] = horizontal_max[b + 1];
continue;
}
}
q = true;
if (vertical_max[b] == vertical_min[b]) {
for (int j = horizontal_min[b]; j <= horizontal_max[b]; j++) {
if (lines[vertical_max[b]][j] == 'a' + b || (q && lines[vertical_max[b]][j] == '*')) {
lines[vertical_max[b]][j] = '*';
} else {
f = false;
break;
}
}
} else if (horizontal_max[b] == horizontal_min[b]) {
for (int i = vertical_min[b]; i <= vertical_max[b]; i++) {
if (lines[i][horizontal_max[b]] == 'a' + b || (q && lines[i][horizontal_max[b]] == '*')) {
lines[i][horizontal_max[b]] = '*';
} else {
f = false;
break;
}
}
} else {
f = false;
}
if (!f) {
break;
}
}
if (f) {
cout << "YES" << endl;
cout << count << endl;
for (int i = 0; i < count; i++) {
cout << vertical_min[i] + 1 << " " << horizontal_min[i] + 1 << " " << vertical_max[i] + 1 << " " << horizontal_max[i] + 1 << endl;
}
} else {
cout << "NO" << endl;
}
}
}
Tutorial is loading...
Authors: MikeMirzayanov, cannor147 and geranazavr555
Authors' solution
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define sqr(a) ((a) * (a))
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
template<class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
return out << "(" << a.x << ", " << a.y << ")";
}
template<class A> ostream& operator << (ostream& out, const vector<A> &a) {
out << "[";
for (auto it = a.begin(); it != a.end(); ++it) {
if (it != a.begin())
out << ", ";
out << *it;
}
return out << "]";
}
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
const int INF = 1e9;
const li INF64 = 1e18;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;
mt19937 rnd(time(NULL));
mt19937_64 rnd64(time(NULL));
const int N = 100 * 1000 + 11;
const int MSK = 1 << 9;
int n, m;
int cnt[MSK];
vector<pt> pz[N];
bool read() {
if (scanf("%d %d", &n, &m) != 2)
return false;
forn(i, n) {
int k;
scanf("%d", &k);
int msk = 0;
forn(j, k) {
int pos;
scanf("%d", &pos);
--pos;
msk |= (1 << pos);
}
++cnt[msk];
}
return true;
}
void solve() {
forn(i, m) {
int c;
scanf("%d", &c);
int msk = 0;
int k;
scanf("%d", &k);
forn(j, k) {
int pos;
scanf("%d", &pos);
--pos;
msk |= (1 << pos);
}
pz[msk].pb(mp(c, i));
sort(all(pz[msk]));
while (sz(pz[msk]) > 2) pz[msk].pop_back();
}
int ans = 0, cost = 2 * INF + 1;
pt res = mp(-1, -1);
forn(p1, MSK) forn(p2, MSK) {
int curcost = 0;
int pos1, pos2;
if (p1 == p2) {
if (sz(pz[p1]) < 2) continue;
curcost = pz[p1][0].x + pz[p1][1].x;
pos1 = pz[p1][0].y;
pos2 = pz[p1][1].y;
} else {
if (sz(pz[p1]) == 0 || sz(pz[p2]) == 0) continue;
curcost = pz[p1][0].x + pz[p2][0].x;
pos1 = pz[p1][0].y;
pos2 = pz[p2][0].y;
}
int curans = 0;
int curmask = p1 | p2;
forn(pp, MSK) {
if ((curmask & pp) == pp) {
curans += cnt[pp];
}
}
if (curans > ans || (curans == ans && curcost < cost)) {
ans = curans;
cost = curcost;
res = mp(pos1, pos2);
}
}
cout << res.x + 1 << " " << res.y + 1 << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int tt = clock();
#endif
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
#ifdef _DEBUG
while (read()) {
#else
if (read()) {
#endif
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
}
Tutorial is loading...
Authors: MikeMirzayanov, cannor147 and geranazavr555
Authors' solution
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
const int M = 1000000007;
const int N = 16;
int d[1 << N][4];
int main() {
int n, T;
cin >> n >> T;
vector<int> durs(n), types(n);
forn(i, n) {
cin >> durs[i] >> types[i];
types[i]--;
}
int result = 0;
d[0][3] = 1;
forn(mask, 1 << n)
forn(lst, 4) {
forn(j, n)
if (types[j] != lst && ((mask & (1 << j)) == 0))
d[mask ^ (1 << j)][types[j]] = (d[mask ^ (1 << j)][types[j]] + d[mask][lst]) % M;
int sum = 0;
forn(i, n)
if (mask & (1 << i))
sum += durs[i];
if (sum == T)
result = (result + d[mask][lst]) % M;
}
cout << result << endl;
}
Tutorial is loading...
Authors: MikeMirzayanov, cannor147 and geranazavr555
Authors' solution
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
const int M = 1000000007;
const int N = 51;
const int S = 2501;
void inc(int& a, int d) {
a += d;
if (a >= M)
a -= M;
}
int a[N][S];
int bc[N][N][S];
int ways[N][N][N][4];
int main() {
int n, T;
cin >> n >> T;
vector<int> cnts(4);
vector<int> durs(4);
a[0][0] = bc[0][0][0] = 1;
forn(i, n) {
int dur, type;
cin >> dur >> type;
type--;
if (type == 0) {
for (int cnts0 = cnts[0]; cnts0 >= 0; cnts0--)
forn(durs0, durs[0] + 1)
inc(a[cnts0 + 1][durs0 + dur], a[cnts0][durs0]);
} else {
for (int cnts1 = cnts[1]; cnts1 >= 0; cnts1--)
for (int cnts2 = cnts[2]; cnts2 >= 0; cnts2--)
forn(durs12, durs[1] + durs[2] + 1)
inc(bc[cnts1 + (type == 1)][cnts2 + (type == 2)][durs12 + dur],
bc[cnts1][cnts2][durs12]);
}
cnts[type]++;
durs[type] += dur;
}
ways[0][0][0][3] = 1;
vector<int> c(3);
for (c[0] = 0; c[0] <= cnts[0]; c[0]++)
for (c[1] = 0; c[1] <= cnts[1]; c[1]++)
for (c[2] = 0; c[2] <= cnts[2]; c[2]++)
forn(lst, 4)
if (ways[c[0]][c[1]][c[2]][lst] != 0) {
forn(nxt, 3)
if (nxt != lst && c[nxt] + 1 <= cnts[nxt]) {
vector<int> cn(c);
cn[nxt]++;
inc(ways[cn[0]][cn[1]][cn[2]][nxt], ways[c[0]][c[1]][c[2]][lst]);
}
}
vector<int> f(N + 1, 1);
forn(i, N)
f[i + 1] = ((long long) f[i]) * (i + 1) % M;
int result = 0;
for (c[0] = 0; c[0] <= cnts[0]; c[0]++)
forn(durs0, durs[0] + 1)
if (T - durs0 >= 0)
for (c[1] = 0; c[1] <= cnts[1]; c[1]++)
for (c[2] = 0; c[2] <= cnts[2]; c[2]++) {
long long extra = (long long)(a[c[0]][durs0]) * bc[c[1]][c[2]][T - durs0] % M;
forn(i, 3)
extra = extra * f[c[i]] % M;
forn(lst, 3)
if (c[lst] > 0)
inc(result, extra * ways[c[0]][c[1]][c[2]][lst] % M);
}
cout << result << endl;
}
very good contest!
How to solve C2 if 't' constraint would have been t<=10^5 (without segment tree and treap) ?
Use two Priority Queues, in one keep those that pass and in the other those that fail. Before printing the result, you have to fail as students as needed to sum less than M. Then, while the greatest from the pq that passes is greater than the smallest of the pq that fails, swap them. Finally, while you can pass the smallest from the pq that fail for free, do it.
My submission during contest: 55775417
So you're pushing/popping same element multiple times. How are you sure this will work in the given time constraint? Can you please explain a bit more?
I have used a similar method but couldn't get the answer ...
What I am is doing deleting removing the greatest from the pq till the sum is less than M and then adding the smallest from the pq that fails until the sum is than M ...
Can someone help me know why won't this method work?
Thanks...
I solved this with a similar approach here: 55809687, but I believe this solution also depends on the low t constraint. The amount of elements that can be popped from the priority queue at the end depends on the possible difference in answers between two subsequent indices. With the current constraints, the possible difference between t values of two indices is 99, and since each t value must be at least 1, the difference in answer can be at most 99. If you were to have t go up to m, you could construct a case where you alternate t values between 1 and m. For the cases where t is 1, you only need to remove the t values that are equal to m, and thus the answer is current index / 2. For the cases where t is equal to m, you need to remove all previous values and thus the answer is the current index. Here, the amount of elements popped and pushed is O(n^2) and will not pass.
In your code you have written saco.top(); inside the while loop. What is the value of saco.top() when i=0 and you haven't put anything inside it. Can you help explain the code ?
when i=0 then u r correct that there will be nothing in pq but u will never go inside that while as first element will always be less than M. also this is mentioned in problem statement ** It's guaranteed that all values of ti are not greater than M. **
I used two BIT arrays to find the answer, 55812830
$$$su$$$ stores the prefix sum,
$$$seen$$$ stores the sorted idx,
https://codeforces.net/contest/1185/submission/55759936
Top programmers usually have the best solutions. He basically puts the students in a multiset (multiset automatically sorts things in O(logN)) and if the sum is bigger than M, he subtracts the biggest students in the multiset and prints the answer. Just doing this would give TLE so he also deletes students from his multiset if the sum is above M. For example, take this case:
first sum is 80, so no need to fail any students, output is 0.
second sum is 120, so we need to fail one student, output is 1. Since the sum is now bigger than M, and it will always be bigger than M from now forward, its in our best interest to elements from the multiset until the sum is smaller than M.
This solution would I think work regardless of the constraint of $$$t$$$
No, I don't think this solution works for large $$$t$$$.
e.g. N = 20000, t = 10000, M = 10000
and the array are
1...(repeat 10000 times) 10000... (repeat 10000 times)
Maybe maintain an array that stores the sum after each $$$i$$$,and then when sum > M, we use binary search to find the first sum in the array that would get the current sum below M, maybe do $$$int index = lower__bound(sum_array.begin(),sum_array.end(),current_student)-sum_array.begin()$$$ ? Because sum-current_student is less than M. And then whatever index we get, we add it to every other result. This seems like a TLE as well tbh, but I'm too lazy to figure out a better solution.
Can you please explain why this approach gives TLE isn't the complexity n(lgn+ n)
I have used two Fenwick Trees + Binary Search. It will work for t <= 2e7. Code: 55863688
Could u explain ur code a little bit for what purpose are u using Fenwick tree
nice contest!!
Hey can someone help me in F Two Pizzas:
In the editorial it has been written: "Then, we should go through all possible masks, that could be reached by two different pizzas.
For each such mask we should keep two pizzas with the smallest total cost, which gives us that mask (i. e. their bitwise-or is equal to our current mask)."
My question is how to choose two pizzas of min-cost that generate a particular mask without going back to brute-force which would, of course, be a TLE.
this solution may help you.
Thanks mate
Though G1 was a bit easier than F, it's still a really good contest for div 2. The score distribution is a bit weird since i saw someone who solved 8 problems have a higher rank than someone who solved all.
I solved G2 by using a $$$O(n^3T^2)$$$ solution. I think it's incorrect but I can't hack it.
Can someone help me hack it?
can somebody tell me why my code is incorrect? https://codeforces.net/contest/1185/submission/55763542 it is easy to read
For problem D cant we maintain a difference array after sorting the initial array of pairs . now we traverse the difference array and maintain a count of number of different elements in the difference array .
if there is only one element say 1,1,1,1... then we can output first or last index since it is in ap already , if the difference array is something like 2 2 1 1 then we output the first occurrence of the changed difference that is index 3 in the above array if the difference array is something like 1 2 3 .... then -1.
is this approach correct ?
Your second case will fail on test 1 2 4 6 8 Difference array will be 1 2 2 2 and your output will be 2 instead of 1.
Could u also explain how u solved Nick an array(CF round 569 Problem B)
yes that was pretty simple one if you convert all elements to non negative integer that is either zero or positives. Now you just need to sort the array and then make elements negative pairwise so that you can get maximum product at the end. If your array length is odd then you don't need to add any condition you just need to leave the last element as it will handle itself the maximum product. For example if your array is -2 -4 -1 0 0 1 2 3 then after converting them to non negatives it becomes 1 3 0 0 0 1 2 3 now after sorting and making the elements pairwise negative you will get 0 0 0 1 1 2 3 3 --> -1 -1 -1 -2 -2 -3 -4 -4 now you can restore your array order by sorting it with index for that you can store it in pair.
Thank u so much for sharing this approach. My approach was count number of positive and negative elements in array and if both positive and negative elements are even or odd make all elements positive elements negative. Else make all positive elements negative except the last one but it is giving me a wrong answer. Can u explain y my logic is wrong?
Consider the following example array[]={-2 -1 2 1} according to your approach neg_count==2 and pos_count==2 here both are even hence you will perform the operation on all elements and your new array will be [3 0 -3 -2] which leads to zero product which is incorrect.
Check my submission history :|
in solution of problem D, i don't really understand why we need to find the maximum difference between two elements in sequence maxd = max(maxd, a[i + 1].first — a[i].first) and how we to to know if we do that, it will correct ?
Please, try one else. One of authors added another solution, which wasn't coincidental with editorial. I hope, it will help you.
Could someone elaborate more on the $$$3^9$$$ bruteforce to get the number of satisfied people for each mask?
Or as in this submission: 55810335, Why is making the outer loop as the loop over the bits, and the inner loop as the loop over the masks stops double counting. I don't seem to get what it is trying to achieve with order.
Here's the for loop, $$$B$$$ is a const int which is equal to $$$9$$$ and $$$cntHappy$$$ has the number of satisfied people for each mask and at first contains the count of the input masks.
Can Anyone Please Explain how to solve C2 problem in given time constraints. I am not able to understand the solution.
Use greedy approach,let's say u have available A marks out of M (u need to pass a student that have index i so A=M-marks[i] ).u want to fill Max amount of students in A so u check count of student having marks 1 to 100 (which, u are maintaining count). Now greedily fill A, so u will get maximum number of passing students.
Can anyone please explain this particular line of code of problem c2 posted by author and please also explain the reason behind this line.
k += (d + j — 1) / j;
I will try to explain it. So, according to the editorial, this line will be run if we have situation, when $$$s_i + t_i - count_k \cdot k \le M$$$. And in this situation just $$$a_i + count_k$$$ may be not mimimal answer (but we want to output the minimal answer).
So we need to free some $$$s_i$$$ minutes. We know that $$$k$$$ students' durations equal $$$count_k$$$. And for the minimal answer we need to add only $$$\lceil \frac{s_i}{k} \rceil$$$ to $$$a_i$$$. Let me rewrite this line in editorial's naming:
$$$a_i += (s_i + k - 1) / k$$$
Note that in programming `(A + B — 1) / B' is equivalent to $$$\lceil \frac{A}{B} \rceil$$$
Problem E
Getting error on test case 5 :
wrong answer actual picture doesn't equal to expected [test case 35]
Can anyone help me with that?
1 2 ca
You are getting wrong answer here.
Can anyone explain the two BIT approach in C2?
I can't understand G2 editorial ... any help?
I can't understand the author's solution code of problem B(Email from Polycarp). Can anyone help me? TIA
i think i am late but still... split function's work is to convert a string for e.g. hhelllo to h 2, e 1, l 3, o 1 now if u obtain this equivalent for both strings you can easily check if at each index, the alphabet is same and count for string t >= count of string s for that alphabet
D's test cases are weak..
some sols are failing at-
6
1 2 5 8 11 13
ANS= -1