Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
l, r = map(int, input().split())
if l * 2 > r:
print(-1, -1)
else:
print(l, l * 2)
Idea: Roms
Tutorial
Tutorial is loading...
Solution 1 (pikmike)
for _ in range(int(input())):
n, k, z = map(int, input().split())
a = [int(x) for x in input().split()]
ans = 0
s = 0
mx = 0
for t in range(z + 1):
pos = k - 2 * t
if pos < 0:
continue
mx = 0
s = 0
for i in range(pos + 1):
if i < n - 1:
mx = max(mx, a[i] + a[i + 1])
s += a[i]
ans = max(ans, s + mx * t)
print(ans)
Solution 2 (pikmike)
for _ in range(int(input())):
n, k, z = map(int, input().split())
a = [int(x) for x in input().split()]
ans = 0
s = 0
mx = 0
for i in range(k + 1):
if i < n - 1:
mx = max(mx, a[i] + a[i + 1])
s += a[i]
if i % 2 == k % 2:
tmp = (k - i) // 2
if tmp <= z:
ans = max(ans, s + mx * tmp)
print(ans)
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (Ne0n25)
#include <bits/stdc++.h>
using namespace std;
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < int(n); ++i)
int solve(const string& s, int x, int y) {
int res = 0;
for (auto c : s) if (c - '0' == x) {
++res;
swap(x, y);
}
if (x != y && res % 2 == 1)
--res;
return res;
}
void solve() {
string s;
cin >> s;
int ans = 0;
forn(x, 10) forn(y, 10)
ans = max(ans, solve(s, x, y));
cout << sz(s) - ans << endl;
}
int main() {
int T;
cin >> T;
while (T--) solve();
}
Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
import kotlin.math.*
fun main() {
repeat(readLine()!!.toInt()) {
val (n, k) = readLine()!!.split(' ').map { it.toLong() }
val (l1, r1) = readLine()!!.split(' ').map { it.toLong() }
val (l2, r2) = readLine()!!.split(' ').map { it.toLong() }
var ans = 1e18.toLong()
if (max(l1, l2) <= min(r1, r2)) {
val rem = max(0L, k - n * (min(r1, r2) - max(l1, l2)))
val maxPossible = n * (abs(l1 - l2) + abs(r1 - r2))
ans = min(rem, maxPossible) + max(0L, rem - maxPossible) * 2
} else {
val invest = max(l1, l2) - min(r1, r2)
for (cntInv in 1..n) {
var curAns = invest * cntInv
val maxPossible = (max(r1, r2) - min(l1, l2)) * cntInv
curAns += min(k, maxPossible) + max(0L, k - maxPossible) * 2
ans = min(ans, curAns)
}
}
println(ans)
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (pikmike)
fun gcd(a: Long, b: Long): Long {
if (a == 0L) return b
return gcd(b % a, a)
}
fun main() {
repeat(readLine()!!.toInt()) {
val (m, d, w) = readLine()!!.split(' ').map { it.toLong() }
val w2 = w / gcd(d - 1, w)
val mn = minOf(m, d)
var cnt = mn / w2
println((2 * (mn - w2) - w2 * (cnt - 1)) * cnt / 2)
}
}
Idea: Roms
Tutorial
Tutorial is loading...
Solution 1 (Ne0n25)
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define forn(i, n) for (int i = 0; i < int(n); ++i)
typedef pair<int, int> pt;
const int N = 200 * 1000;
int n;
int l[N], r[N], t[N];
set<pt> st[2];
int main() {
scanf("%d", &n);
forn(i, n) scanf("%d%d%d", &l[i], &r[i], &t[i]), --t[i];
vector<pair<int, pt>> ev;
forn(i, n) {
ev.pb(mp(l[i], mp(0, i)));
ev.pb(mp(r[i], mp(1, i)));
}
sort(all(ev));
int ans = 0;
for (auto it : ev) {
int i = it.y.y;
if (it.y.x) {
int j = t[i];
int k = j ^ 1;
if (st[j].count(mp(r[i], i)) && !st[k].empty()) {
++ans;
st[k].erase(st[k].begin());
}
if (st[j].count(mp(r[i], i))) st[t[i]].erase(mp(r[i], i));
} else {
st[t[i]].insert(mp(r[i], i));
}
}
printf("%d\n", n - ans);
}
Solution 2 (Ne0n25)
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#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)
typedef pair<int, int> pt;
const int INF = 1e9;
const int N = 200 * 1000;
int n;
vector<pt> a[2];
struct segtree {
int n;
vector<int> t, ps;
segtree(int n) : n(n) {
t.resize(4 * n, -INF);
ps.resize(4 * n, 0);
}
void push(int v, int l, int r) {
if (l + 1 != r) {
ps[v * 2 + 1] += ps[v];
ps[v * 2 + 2] += ps[v];
}
t[v] += ps[v];
ps[v] = 0;
}
void upd(int v, int l, int r, int pos, int val) {
push(v, l, r);
if (l + 1 == r) {
t[v] = val;
return;
}
int m = (l + r) >> 1;
if (pos < m) upd(v * 2 + 1, l, m, pos, val);
else upd(v * 2 + 2, m, r, pos, val);
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}
void add(int v, int l, int r, int L, int R, int val) {
push(v, l, r);
if (L >= R) return;
if (l == L && r == R) {
ps[v] += val;
push(v, l, r);
return;
}
int m = (l + r) >> 1;
add(v * 2 + 1, l, m, L, min(m, R), val);
add(v * 2 + 2, m, r, max(m, L), R, val);
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}
int get(int v, int l, int r, int L, int R) {
push(v, l, r);
if (L >= R) return -INF;
if (l == L && r == R)
return t[v];
int m = (l + r) >> 1;
int r1 = get(v * 2 + 1, l, m, L, min(m, R));
int r2 = get(v * 2 + 2, m, r, max(m, L), R);
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
return max(r1, r2);
}
void upd(int pos, int val) {
return upd(0, 0, n, pos, val);
}
void add(int l, int r, int val) {
return add(0, 0, n, l, r, val);
}
int get(int l, int r) {
return get(0, 0, n, l, r);
}
};
int main() {
scanf("%d", &n);
forn(i, n) {
int l, r, t;
scanf("%d%d%d", &l, &r, &t);
a[t - 1].pb(mp(r, l));
}
forn(i, 2) sort(all(a[i]), [](pt a, pt b) {
if (a.x == b.x) return a.y > b.y;
return a.x < b.x;
});
segtree t1(sz(a[0]) + 1), t2(sz(a[1]) + 1);
t1.upd(0, 0);
t2.upd(0, 0);
int ans = 0;
for (int i = 0, j = 0; i + j < n; ) {
if (i < sz(a[0]) && (j == sz(a[1]) || a[0][i].x <= a[1][j].x)) {
int pos = upper_bound(all(a[1]), mp(a[0][i].y, -INF)) - a[1].begin() + 1;
int cur = t2.get(0, pos) + 1;
ans = max(ans, cur);
t1.upd(i + 1, cur);
t2.add(0, pos, 1);
++i;
} else {
int pos = upper_bound(all(a[0]), mp(a[1][j].y, -INF)) - a[0].begin() + 1;
int cur = t1.get(0, pos) + 1;
ans = max(ans, cur);
t2.upd(j + 1, cur);
t1.add(0, pos, 1);
++j;
}
}
printf("%d\n", ans);
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
typedef long long li;
const int N = 300043;
bool is_bridge[N];
int w[N];
int c[N];
int v[N];
vector<pair<int, int> > g[N];
vector<pair<int, int> > g2[N];
int comp[N];
li sum[N];
li dp[N];
int cnt[N];
int fup[N];
int tin[N];
int T = 0;
li ans[N];
int v1[N], v2[N];
int n, m, k;
int dfs1(int x, int e)
{
tin[x] = T++;
fup[x] = tin[x];
for(auto p : g[x])
{
int y = p.first;
int i = p.second;
if(i == e)
continue;
if(tin[y] != -1)
fup[x] = min(fup[x], tin[y]);
else
{
fup[x] = min(fup[x], dfs1(y, i));
if(fup[y] > tin[x])
is_bridge[i] = true;
}
}
return fup[x];
}
void dfs2(int x, int cc)
{
if(comp[x] != -1)
return;
comp[x] = cc;
cnt[cc] += v[x];
sum[cc] += c[x];
for(auto y : g[x])
if(!is_bridge[y.second])
dfs2(y.first, cc);
}
void process_edge(int x, int y, int m, int weight)
{
li add_dp = dp[y];
if(cnt[y] > 0 && cnt[y] < k)
add_dp = max(0ll, add_dp - weight);
cnt[x] += m * cnt[y];
dp[x] += m * add_dp;
}
void link(int x, int y, int weight)
{
process_edge(x, y, 1, weight);
}
void cut(int x, int y, int weight)
{
process_edge(x, y, -1, weight);
}
void dfs3(int x, int p)
{
dp[x] = sum[x];
for(auto e : g2[x])
{
int i = e.second;
int y = e.first;
if(y == p)
continue;
dfs3(y, x);
link(x, y, w[i]);
}
}
void dfs4(int x, int p)
{
ans[x] = dp[x];
for(auto e : g2[x])
{
int i = e.second;
int y = e.first;
if(y == p)
continue;
cut(x, y, w[i]);
link(y, x, w[i]);
dfs4(y, x);
cut(y, x, w[i]);
link(x, y, w[i]);
}
}
int main()
{
scanf("%d %d %d", &n, &m, &k);
for(int i = 0; i < k; i++)
{
int x;
scanf("%d", &x);
--x;
v[x] = 1;
}
for(int i = 0; i < n; i++)
scanf("%d", &c[i]);
for(int i = 0; i < m; i++)
scanf("%d", &w[i]);
for(int i = 0; i < m; i++)
{
scanf("%d %d", &v1[i], &v2[i]);
--v1[i];
--v2[i];
g[v1[i]].push_back(make_pair(v2[i], i));
g[v2[i]].push_back(make_pair(v1[i], i));
}
for(int i = 0; i < n; i++)
{
tin[i] = -1;
comp[i] = -1;
}
dfs1(0, -1);
int cc = 0;
for(int i = 0; i < n; i++)
if(comp[i] == -1)
dfs2(i, cc++);
for(int i = 0; i < m; i++)
if(is_bridge[i])
{
g2[comp[v1[i]]].push_back(make_pair(comp[v2[i]], i));
g2[comp[v2[i]]].push_back(make_pair(comp[v1[i]], i));
}
dfs3(0, 0);
dfs4(0, 0);
for(int i = 0; i < n; i++)
printf("%lld ", ans[comp[i]]);
puts("");
}
Super Fast!!
Day by day I feel like nobody is getting the humor anymore they are just downvoting every comment.
In the Tutorial of Problem E: I think you mean yd + x instead of xd + y as the index of the x-day of the y-month in the year.
xd+y means index y-th day of x-th month which is corresponding pair of x-th day of y-th month whose index is yd+x.
Can someone tell me why my solution for D fails? I simply decide whether to gain intersection by joining two new segments or expanding others which already cover themselves totally. Counter-examples are especially appreciated.
Here's the submission: https://mirror.codeforces.com/contest/1389/submission/88410212.
The test, your solution fails when
n = 10, k = 297
l1 = 741, r1 = 741
l2 = 20, r2 = 770
Try to fix it.
Got my error, thank you!
Is there any way to solve $$$D$$$ in $$$O(1)$$$.. maybe little case work ($$$chukles$$$), but I guess that could be done!
Yes. Use some greedy approach to choose how many segments need to be invested. This is my accepted approach https://mirror.codeforces.com/contest/1389/submission/106042967
nice, I'll try to solve it too in $$$O(1)$$$
Waiting for Div 3 :) After so many difficult rounds to increase rating :(
What is
Event Processing
I see it here and there but never find some concise tutorial on it.https://www.geeksforgeeks.org/maximum-number-of-overlapping-intervals/
In that algo the beginning and ending of intervals is treates as "events" where the count of them is incremented or decremented.
Thanks man!
I think we can just go through 45 combinations for C instead of the 100 given in the tutorial.
TLDR:
0101010 has both a 101010 and a 010101 type strings in it.
DETAILED:
Let us say the string we get after removing all other numbers but two be some 011100001001010.....
Then for the above we want it to reduce to the form : 01010101... Note, that if the reduced string ends with 1 then the answer is: size(original string)-size(reduced string). Else, the answer is: size(original string)-{size(reduced string)-1}.
Can someone explain the dynamic programming approach to problem E?
Problem E doesn't use dynamic programming. It's just math and number theory.
Cool contest!Though I've lost some points :(
.
Firstly.
What does these numbers mean? Test containing 4 numbers, but u give 6. It's not 1 test and not 2.
Secondly
X can't be negative.
In both pairs {l, r} l <= r, so max(r1, r2) >= max(l1, l2) => max(r1, r2) >= min(l1, l2)
I hope you understand
.
Sample are incorrect and occasionally it's work (but it shouldn't)
Why this code is giving TLE? https://ideone.com/wXdX3A
Lets calculate O(10 * 10 * n + 10 * n) = O(100*n) n = 2 * 10^5. We get O(2 * 10^7). Its about 10 secs on Python and about 0.2-0.3 secs on C++ (I have solve with the same asymptotics) Friendly advice. Use C++ in real competitions.
Same code gives AC in c++ but tle in python :| btw thank you.
С++ ~100 times faster than python
always use fast I/O in Python. normal I/O in python takes a lot of time. https://www.geeksforgeeks.org/python-input-methods-competitive-programming/
Hey @adedalic I have one doubt in D, You have said,
EDIT:
Now I get it, u have used a for loop (i to n), that checks by joining first i pairs and compares it with minimum each time . Thanks.
Just iterate over n and find mininum value.
I wrote a recursive solution using memoization where I checked for each I it's i-1 and i+1 and updated my answer with maximum sum Does my solution not violate the condition given in the question :. " you can't perform two or more moves to the left in a row" Can anyone please explain this that how my function goes for 2 or more left moves in a row if z is>=2 and does not violate this and gets accepted
__88418038
Could anyone please explain the dp with segment tree approach for problem F?
Here's how I understand it (I did not solve in contest), feel free to correct me if I'm wrong or there is an easier way to think about this.
First, let us sort all segments in increasing order of their right endpoints in the list $$$segs$$$. Split the segments based on type and sort them in the same fashion in the lists $$$s[t]$$$. Let $$$dp[i][j][t]$$$ be the answer assuming that the problem is restricted to the first $$$j$$$ segments in $$$segs$$$, along with the condition that we can only select the first $$$i$$$ segments in $$$s[t]$$$, and that we have used segment $$$i$$$ in $$$s[t]$$$ in our solution.
Now, to transition to $$$segs[j+1]$$$ , assuming $$$segs[j+1]$$$ is of type 1 and is the $$$k+1$$$'th segment in $$$s[1]$$$, we have the following:
Now, notice that for any new segment, the segments that do not touch it must be a prefix of the sorted segment list. Thus, these transitions are exactly range add modifications and maximum queries on two segment trees. We can easily find what range to add by using a binary search. Finally, we can eliminate the 2nd dimension with $$$j$$$ just by directly modifying the segment trees.
See my submission for implementation details.
So, for implementation purpose, we can ignore the $$$2nd$$$ dimension of the dp state you mentioned, right? So our dp state will be:
$$$dp(i,j): $$$ maximum segments if we have only considered till the ith segment of type $$$j$$$ and this one is included. And we traverse the segments on the sorted order of their right endpoints.
Yes, exactly. When I was trying to figure the solution out based on other people's code, I found the 3 dimensions to be easier (as a solution that I could actually come up with), but you have it exactly right.
Okay, sounds good. Thanks a lot.
O(1) solution for D 88447760
+1 88435747 in Kotlin
Could somebody please explain the dp approach to B?
Dp approach of B will be like that we have two choices at every index either we can move left or right, we will explore the moves and the maximum between these two options will be considered. base cases are:
1 . when we don't have any moves left (k==0), in this case, we will return the current index value.
2 . when the last move taken were left or we don't have any left moves remained or we are at the starting index of the array, then we have only one option to move right at this index.
3.else we have two option to move right and left, and take the maximum among those to the answer. you can check my solution for more clarity, 88714266.
How did you decide the states to cache? because here index,prev etc also change in every state...
Even I had the same query VaibhaveS. Could you explain it if you've understood it?
Can anyone help me with my code for Problem C ? Getting a Wrong Answer.
Your code is checking if i is at even and j is at odd positions, which is wrong. You need to find the longest SUBSEQUENCE of alternating i and j of even length. INPUT: 1 2025 YOUR OUTPUT: 1 CORRECT OUTPUT : 2
Same approach as Editorial still can't be accepted in python3 due to TLE on test 2 , what is this?
1389C-Good String 88530443
First: Submit in PyPy instead of CPython
Second: Convert the input to a list of ints once instead of converting it in each iteration.
O(1) solution for problem D Segment Intersections.
If anyone is interested in the explanation then comment.
interested
There can only be three initial configurations for the two intervals:
1> One completely overlaps the other.
2> There is some partial intersection b/w them.
3> They are disjoint.
Now, let us tackle them one by one.
1>> We will have an initial I which can be calculated easily, thereafter if the current I is smaller than k we can increase the intersection length by one for all n pair of segments in just 1 step and we can do this untill all the pairs have become equal It is obvious that we cannot increase I by more than 1 in a single operation, So this is optimal. Now if still I is less than k we only have one way to increase I by increasing the segment lengths of a pair this will give us a +1 for each 2 step. Also, we can do this indefinitely.
2>> Its the same as the first one except that the initial I has to be calculated differently.
3>> Initial I=0 , Now how can we increase I? we first have to make a pair of segment meet each other while doing this our I remains the same, lets call this cost as gap. Now as soon as the segments meet we can start having a bunch of +1s . Let us calculate how many +1s we can get in a row, Suppose the worst case when initially both segments are points, then we will have no of +1s equal to the no of +0s (we used to fill the gap) . And in every other case the no of +1s will be greater than +0s , Remember this. Now let us see what choice do we have after using up these two operations , At this point we reach a situation analogous to one explained earlier in 1>> where we will be getting +1 for every 2 steps. We have all the tools ready . Let us expand this for n. I claim that in an optimal final solution we will have a situation where x of the n segments are completely overlapping and at most 1 pair of segments which has been extended by a 2 step operation or has a partial overlap , Also x will be maximum. Suppose a case where x is not the maximum then we have a case where x-1 segments have complete overlap and 1 segment with a partial overlap or an extended 2 step operation segment. We can simply rule out the first case because we can never compensate for the loss of one completely overlapping segment with a partial one. As for the other case the no of 2 step operation will be greater than the complete overlap of the segments as compensation. This is where we use the fact we proved earlier, that we can get a complete overlapping with total no of steps less than or equal to twice the size of the segments, which will always be at least as efficient as the +2 step operation and hence we can carve out a completely overlapping segment from this segment without loosing optimality (for the lack of better word).
Thanks, man for the effort! stating 3rd case more mathematically -
suppose we have to make 'rem' out of 'n' segs, let 'ex' be the amount to 'activate' the seg (= max(l1,l2)-min(r1,r2)), and 'tot' (= max(r2,r1)-min(l1,l2)) we can get by doing 1 inc per 1 move operation (+1/1 ops) after 'activation'.
Its oBvious that we have to 'activate' the 1st seg and do +1/1 ops If still we have something remaining (let it be 'x')
now if 'x' >= 'tot' then its better to activate another seg and get tot why? because 'tot'+'ex' <= 'tot'*2 as 'ex' <= 'tot'
if 'x' < 'tot' then we decide on the basis of which is bigger, 'x'+'ex' or 'x'*2
so how is this o(1) -> after the 1st activation, we can activate segs until reqd amount < 'tot' and then we decide between 'activation'+'+1/1 ops' and '+1/2 ops' for the last one.
Yeah at one point I thought the way I described it isn't going to help anyone, happy to see that you got it... P.S. yours seems much formal and clear.
88381812
Try to write the code in the code snippet. Yours is very unreadable and takes a lot of space in the comments
EDIT: For your problem, a string is good if it has all same characters in case or even length, or same parity indexed characters are same in case of odd length.
Can anyone provide the Test case 2 — 19th input for task B. Most people had this error "expected 218 found 216". If not exact same test case, then maybe something similar?
I don't know the exact test case but you should also handle for the case k=0 explicitly because my solution got accepted after I make a case for k==0.
in problem B, the tutorial assumes that the left steps will always be accompanied by right steps as they have been taken in pairs but it may so happen that the last step will be a left step, so in that case the tutorial fails and so does the solution in the following test case
18 11 4 11 19 18 19 19 5 14 15 17 4 10 9 8 17 9 2 15 10
The tutorial assumes left steps are always preceded by a right step; Hence the order (right,left).
oh thanks... i had misunderstood it
The idea of G is quite similar to this problem Museum Tour
Very interesting:
Undirected Graph -> find Bridges -> Tree -> Can be solved using DP
Directed Graph -> find SCC -> DAG -> Can be solved using DP
Not expecting DP in second problem.
88352400 for probblem C , can someone please tell me what's wrong in my code?
include
include
using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while(T--){ list adjlist[10]; string s; cin >> s; for (int i = 0 ; i < s.size() ; i++) adjlist[s[i] — '0'].push_back(i); int ans = 0; for (int i = 0 ; i < 10 ; i++) ans = max(ans , (int)adjlist[i].size()); for (int i = 0 ; i < 10 ; i++){ for (int j = 0 ; j < 10 ; j++){ if (i == j) continue; int cnt = 0; for(auto it1 = begin(adjlist[i]),it2 = begin(adjlist[j]); it2 != end(adjlist[j]) && it1 != end(adjlist[i]); ++it2){ if (*it2 > *it1) ++cnt , ++it1; } ans = max(ans , 2 * cnt); } } cout << s.size() — ans << '\n'; } }
I have tried to make editorial for questions A-E . please have a look. Language :- Hindi
https://www.youtube.com/watch?v=S_-BaaH7P80&list=PLrT256hfFv5X1GNpiqEh5njN_WJmQu8Q6
Great Tutorial!!
In problem C, if we are using brute force, for all 100 combinations it is O(n) then it becomes overall O(100*n) which is O(10^7) and with that we have 1000 test cases also, so how does it even work? At first i didn't do it because i thought maybe it will cause TLE, but I realized that they have done the same thing.
https://mirror.codeforces.com/contest/1389/submission/162863152 what is wrong in my code, pls help
Brainless segment tree solution for problem F:
Implementation: link