Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
l1, r1, l2, r2 = map(int, input().split())
if max(l1, l2) <= min(r1, r2):
print(max(l1, l2))
else:
print(l1 + l2)
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n, m = map(int, input().split())
s = []
for j in range(n):
s.append(input())
minx = 10 ** 9
miny = 10 ** 9
for x in range(n):
for y in range(m):
if s[x][y] == 'R':
minx = min(minx, x)
miny = min(miny, y)
print('YES' if s[minx][miny] == 'R' else 'NO')
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
def can(pos, m):
k = len(pos)
x = k - m
for i in range(m + 1):
l = pos[i]
r = pos[i + x - 1]
if r - l + 1 - x <= m:
return True
return False
t = int(input())
for i in range(t):
s = input()
pos = []
n = len(s)
for i in range(n):
if s[i] == '1':
pos.append(i)
lf = 0
rg = len(pos)
while rg - lf > 1:
mid = (lf + rg) // 2
if can(pos, mid):
rg = mid
else:
lf = mid
if len(pos) == 0 or pos[-1] - pos[0] == len(pos) - 1:
print(0)
else:
print(rg)
Tutorial
Tutorial is loading...
Solution (vovuh)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
long long k;
cin >> n >> k;
vector<long long> a(n);
for (auto &it : a) {
cin >> it;
}
long long ans = 0;
for (int it = 0; it < n; ++it) {
vector<int> cnt(n);
for (int i = n - 1; i >= 0; --i) {
cnt[i] = (a[i] == 0);
if (i + 1 < n) {
cnt[i] += cnt[i + 1];
}
}
vector<long long> b = a;
long long s = accumulate(b.begin(), b.end(), 0ll);
bool ok = true;
for (int i = 0; i < n; ++i) {
if (b[i] == 0) {
long long x = (i + 1 < n ? cnt[i + 1] : 0);
b[i] = min(k, x * k - s);
if (b[i] < -k) {
ok = false;
}
s += b[i];
}
}
if (ok) {
long long pos = 0, mn = 0, mx = 0;
for (int i = 0; i < n; ++i) {
pos += b[i];
mn = min(mn, pos);
mx = max(mx, pos);
}
if (pos == 0) {
ans = max(ans, mx - mn + 1);
}
}
rotate(a.begin(), a.begin() + 1, a.end());
}
if (ans == 0) {
ans = -1;
}
cout << ans << endl;
return 0;
}
Idea: vovuh
Tutorial
Tutorial is loading...
Solution (vovuh)
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int tc;
cin >> tc;
while (tc--) {
int n;
string s[2];
cin >> n >> s[0] >> s[1];
for (int it = 0; it < 2; ++it) {
while (s[0].back() == '.' && s[1].back() == '.') {
s[0].pop_back();
s[1].pop_back();
}
reverse(s[0].begin(), s[0].end());
reverse(s[1].begin(), s[1].end());
}
n = s[0].size();
vector<vector<int>> cost(n, vector<int>(2));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j) {
cost[i][j] = (s[j][i] == '*');
}
}
vector<vector<int>> dp(n, vector<int>(2, INF));
dp[0][0] = cost[0][1];
dp[0][1] = cost[0][0];
for (int i = 0; i + 1 < n; ++i) {
dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + 1 + cost[i + 1][1]);
dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 2);
dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 1 + cost[i + 1][0]);
dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 2);
}
cout << min(dp[n - 1][0], dp[n - 1][1]) << endl;
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
vector<vector<int>> g, h;
vector<int> tin, tout, clr;
vector<vector<int>> sum(2);
int T;
int flip;
int cnt;
bool isp(int v, int u){
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
void init(int v){
tin[v] = T++;
for (int u : g[v]){
if (clr[u] == -1){
clr[u] = clr[v] ^ 1;
h[v].push_back(u);
init(u);
}
else if (tin[u] < tin[v]){
int dif = clr[v] ^ clr[u];
if (!dif){
flip = clr[v] ^ 1;
++cnt;
}
--sum[dif][u];
++sum[dif][v];
}
}
tout[v] = T;
}
int sv;
void dfs(int v){
for (int u : h[v]){
dfs(u);
if (sum[0][u] == cnt && sum[1][u] == 1){
sv = u;
flip = clr[v] ^ 1;
}
forn(i, 2) sum[i][v] += sum[i][u];
}
}
int main() {
cin.tie(0);
iostream::sync_with_stdio(false);
int t;
cin >> t;
forn(_, t){
int n, m;
cin >> n >> m;
g.assign(n, vector<int>());
h.assign(n, vector<int>());
forn(i, m){
int v, u;
cin >> v >> u;
--v, --u;
g[v].push_back(u);
g[u].push_back(v);
}
tin.resize(n);
tout.resize(n);
forn(i, 2) sum[i].assign(n, 0);
clr.assign(n, -1);
cnt = 0;
T = 0;
clr[0] = 0;
init(0);
if (cnt <= 1){
cout << "YES\n";
forn(v, n) cout << (clr[v] ^ flip);
cout << "\n";
continue;
}
sv = -1;
dfs(0);
if (sv == -1){
cout << "NO\n";
}
else{
cout << "YES\n";
forn(v, n) cout << (clr[v] ^ isp(sv, v) ^ flip);
cout << "\n";
}
}
return 0;
}
Can someone share his sliding window approach for problem C ?
Reduce the problem: find the lowest cost substring, cost = max(0's, total 1's — 1's)
The cost of the final optimal substring will either be contributed by 0's or by 1's. Let's binary search on the best answer that can be contributed by 0's and then binary search again on 1's, then we take the minimum of the two.
To check if an answer
g
is possible, we run sliding window. For example, let's say we are currently binary searching on best answer contributed by 0's. We can expand the window until we have more than g 0's in the current window. Then we can shrink it from the left until we have exactly g 0's again.here's my submission: https://codeforces.com/contest/1680/submission/157099290
Sliding window / two pointers approach without binary search, runs in O(n) time, in C.
Let our window represent the string left after deleting the first
i
elements, and thesize - j
elements from the right.We know that for a window starting at index
i
, if we're increasing the window size, then the best cost we can get is when the number of characters0
in our window is equal to characters1
outside the window. This is because if we keep increasing our window size then the number of character0
in the string will keep increasing, and a smaller window will increase the number of character1
outside the string.Then we just iterate through
i
, the beginning of each window, and keep the maximum window we can get. Wheni
goes to the next iteration, it will find the next possible window starting at the previousi
's max (to keep it O(n)), while also keeping track of the minimum cost as well.Submission: https://codeforces.com/contest/1680/submission/157038960
Great approach !! Understood completely. Thx
Can someone share all the approaches to question C as mentioned in the tutorial? It will be nice to see them all at once and compare their complexities
Dynamic programming, Greedy, Two pointers
Has anyone solved problem C with dynamic programming?
Did you ?
dynmaic programming(prefix sum) - Submission
Can you please elaborate your solution. Precisely what the last for loop means.
Refer to this COMMENT for explanation.
Can C be solved using ternary search? If yes, can someone share their solution for the same?
Here is my solution with ternary search.
157125272
I think the solution can be improved and may be done more clearly.
I also tried it using ternary search but got WA as verdict , can you help me finding the test case where it is failing ? 157066350 For every index j in the string , I am finding the optimal i so that cost is minimum using ternary search .
Binary Search Video Solution for C
green !
I used a segment tree.I think I can replace it by a monotune queue.
Problem F looks very similar to https://codeforces.com/contest/19/problem/E
For problem E: a "state machine" approach works, without any DP.
The idea: same as in the editorial, we sweep all the chips from left to right. If we reach a column with a single chip, just move it to the right and add 1 to the answer. What if we reach a column with 2 chips (whether it's 2 chips that are already there, or 1 chip is there and another has been pushed in from the left)? In this case, we push the top chip down or the bottom chip up. But we don't have to consider both possibilities yet. Just put the chip in an "indeterminate" state and resolve the state when you reach a column with exactly one chip.
Of course, this doesn't save much in terms of runtime/space — the dp solution only carries around 2 integers. But I think it's a nice way of looking at the problem.
Hey, can anyone help me with problem C. I've thought of a greedy solution whose basic idea was to remove the max number of zeros by removing min number of ones and repeat this till the string becomes empty from zeros.I tried checking with many edge cases but cannot see what is the mistake ive made. So plz can anyone help me I have commented the code for better understanding https://ideone.com/bBoj0b
Failing testcase: Ticket 7173
Thanks!! I didnt know there was some website like this
I think there is a typo in the editorial of problem E, because the transition $$$dp_{i, 1} \rightarrow dp_{i+1, 1}$$$ is considered twice. It's not really important though because it's easy to understand that the last transition is meant to be $$$dp_{i, 1} \rightarrow dp_{i+1, 0}$$$
If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints.
If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).
Finally an expert after this contest, really liked third problem
i really want to solve the problem c by myself but i am stuck for a few days. can anyone give me any insight to approach the problem
In problem C, I iterate to [0,n-1] taking only the 'i'th character at each iteration as the first character of the substring, and for finding the last position of the substring, use the binary search. so how do we binary search it! first, compute the prefix of both '0's and '1's in the string, then use those prefixes to compute the max('0's present in the substring, '1's deleted from the substring). if('0's present in the substring > '1's deleted from the substring) then shift left=mid+1; else, shift right to mid. And then compute the minimum of answer and the substring computed up to left. https://codeforces.com/contest/1680/submission/157425332
Problem F is very nice.
The final part can also be done with LCA. If you want to remove an edge in the tree, which splits it into two subtrees, the edge needs to be so the bad edges not in the original tree (ie. those non-tree edges that connect vertices of the same color) need to be in separate subtrees so we can flip the colors of one subtree.
Then the problem becomes: Given several paths on a tree, find an edge that lies on all of them. This will be the edge we remove. The intersection of two paths is empty, a vertex, or a subpath. It can be checked with a few cases using LCA whether the intersection is another path, and we repeat checking with this newer subpath.
Hey, do you think you could help me understand solution to problem F? I understand when it says we need to remove one edge such that the remaining graph is bipartite but I don't after that I don't really understand anything about how we find this edge. Thanks.
I don't understand how the dog walks in the second example of question D
Now I understand,can't ignore the order of numbers
Can anyone help finding which TC my submission fails on? https://codeforces.com/contest/1680/submission/157535516
Edit: Problem E
I did the same! IDK where I am going wrong. If you get it do tell me.
Failing testcase: Ticket 7267
CC: Agent_Lemon
Can someone please hack my program for D? Thank you in advance. https://codeforces.com/contest/1680/submission/157643976
I found a different simple solution for D.
Let $$$p[i] = \sum_{k \leq i} a[i]$$$ be where the dog is after $$$i$$$ steps.
Let $$$pre[i]$$$ denote sum of nonzero (known) steps in the first $$$i$$$ steps.
Let $$$cnt[i]$$$ denote the number of zeros in the first $$$i$$$ steps.
The problem is equivalent to maximizing $$$\max_i p[i] - \min_i p[i].$$$ Thus, for each pair of indices $$$(i,j)$$$ we check if we can make $$$p[i]$$$ the min and $$$p[j]$$$ the max or vice versa, and what optimal difference we can get with this pair. (Clearly if $$$a[i] > 0$$$ then it cannot be the min so we can ignore such cases, etc.)
We need the following inequalities to hold:
$$$ p[i] \in [pre[i] - cnt[i] \cdot k, pre[i] + cnt[i] \cdot k]$$$
$$$ p[j] \in [pre[j] - cnt[j] \cdot k, pre[j] + cnt[j] \cdot k]$$$
$$$ p[j] \leq (cnt[n] - cnt[j]) \cdot k - (pre[n] - pre[j])$$$
$$$ p[j] - p[i] \in [(pre[j] - pre[i]) - (cnt[j] - cnt[i]) \cdot k, (pre[j] - pre[i]) + (cnt[j] - cnt[i]) \cdot k]$$$
where the first two inequalities are obvious, the third inequality ensures that the dog can return to zero after reaching the maximum at $$$j,$$$ and the fourth inequality is that the dog can go from the min at $$$i$$$ to the max at $$$j.$$$
It can easily be checked whether these four inequalities hold, and find the optimal difference for this pair $$$(i,j).$$$ The overall runtime is $$$O(n^2).$$$