Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
for _ in range(int(input())):
x, y = map(int, input().split())
print("YES" if y >= -1 else "NO")
1989B - Подстрока и подпоследовательность
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string a, b;
cin >> a >> b;
int n = a.size(), m = b.size();
int ans = n + m;
for (int i = 0; i < m; ++i) {
int j = i;
for (auto c : a) {
if (j < m && c == b[j]) ++j;
}
ans = min(ans, n + m - (j - i));
}
cout << ans << '\n';
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto& x : a) cin >> x;
for (auto& x : b) cin >> x;
int x = 0, y = 0, neg = 0, pos = 0;
for (int i = 0; i < n; ++i) {
if (a[i] > b[i]) {
x += a[i];
} else if (a[i] < b[i]) {
y += b[i];
} else {
neg += (a[i] < 0);
pos += (a[i] > 0);
}
}
int ans = -1e9;
for (int i = -neg; i <= pos; ++i)
ans = max(ans, min(x + i, y + (pos - neg - i)));
cout << ans << '\n';
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (adedalic)
fun main() {
val (n, m) = readln().split(" ").map { it.toInt() }
val a = readln().split(" ").map { it.toInt() }
val b = readln().split(" ").map { it.toInt() }
val c = readln().split(" ").map { it.toInt() }
val mx = a.maxOrNull()!! + 1
val best = IntArray(mx) { 1e9.toInt() }
val calc = IntArray(mx) { 0 }
for (i in a.indices)
best[a[i]] = minOf(best[a[i]], a[i] - b[i])
for (v in 1 until mx)
best[v] = minOf(best[v], best[v - 1])
for (v in 1 until mx)
if (v >= best[v])
calc[v] = 2 + calc[v - best[v]]
var ans = 0L
for (v in c) {
var cur = v
if (cur >= mx) {
val k = (cur - mx + 1 + best[mx - 1]) / best[mx - 1]
ans += 2L * k
cur -= k * best[mx - 1]
}
ans += calc[cur]
}
println(ans)
}
1989E - Расстояние до различных
Идея: BledDest
Разбор
Tutorial is loading...
Решение (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(x < 0) x += MOD;
return x;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
int main()
{
int n;
cin >> n;
int k;
cin >> k;
vector<vector<int>> dp(n + 1, vector<int>(k + 1));
dp[0][0] = 1;
vector<int> sum(k + 1);
sum[0] = 1;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= k; j++)
{
int& d = dp[i + 1][min(j + 1, k)];
d = add(d, sum[j]);
if(i >= 2 && i != n - 1)
d = add(d, -dp[i - 1][j]);
}
for(int j = 0; j <= k; j++)
sum[j] = add(sum[j], dp[i + 1][j]);
}
cout << dp[n][k] << endl;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
struct edge{
int v, u;
};
vector<vector<int>> g, tg;
vector<int> ord;
vector<char> used;
vector<int> clr;
void ts(int v){
used[v] = true;
for (int u : g[v]) if (!used[u])
ts(u);
ord.push_back(v);
}
void dfs(int v, int k){
clr[v] = k;
for (int u : tg[v]) if (clr[u] == -1)
dfs(u, k);
}
vector<long long> rk, p;
long long sum;
vector<long long*> where;
vector<long long> val;
long long cost(int x){
return x == 1 ? 0ll : x * 1ll * x;
}
int getp(int a){
return a == p[a] ? a : getp(p[a]);
}
void unite(int a, int b){
a = getp(a), b = getp(b);
if (a == b) return;
if (clr[a] != clr[b]) return;
if (rk[a] < rk[b]) swap(a, b);
where.push_back(&sum);
val.push_back(sum);
sum -= cost(rk[a]);
sum -= cost(rk[b]);
sum += cost(rk[a] + rk[b]);
where.push_back(&rk[a]);
val.push_back(rk[a]);
rk[a] += rk[b];
where.push_back(&p[b]);
val.push_back(p[b]);
p[b] = a;
}
vector<edge> e;
vector<long long> ans;
void calc(int l, int r, const vector<int> &cur){
if (l == r - 1){
ans.push_back(sum);
return;
}
int m = (l + r) / 2;
for (int i : cur) if (i < m){
int v = getp(e[i].v), u = getp(e[i].u);
for (int w : {v, u}) if (used[w]){
g[w].clear();
tg[w].clear();
clr[w] = -1;
used[w] = false;
}
g[v].push_back(u);
tg[u].push_back(v);
}
ord.clear();
for (int i : cur) if (i < m){
for (int v : {getp(e[i].v), getp(e[i].u)}) if (!used[v]){
ts(v);
}
}
reverse(ord.begin(), ord.end());
int k = 0;
for (int v : ord) if (clr[v] == -1){
dfs(v, k);
++k;
}
int tim = val.size();
for (int i : cur) if (i < m) unite(e[i].v, e[i].u);
vector<int> tol, tor;
for (int i : cur){
if (i >= m)
tor.push_back(i);
else if (getp(e[i].v) == getp(e[i].u))
tol.push_back(i);
else
tor.push_back(i);
}
calc(m, r, tor);
while (int(val.size()) > tim){
*where.back() = val.back();
where.pop_back();
val.pop_back();
}
calc(l, m, tol);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m, q;
cin >> n >> m >> q;
e.resize(q);
forn(i, q){
int x, y;
char c;
cin >> x >> y >> c;
--x, --y;
if (c == 'R')
e[i] = {y + n, x};
else
e[i] = {x, y + n};
}
rk.resize(n + m, 1);
p.resize(n + m);
iota(p.begin(), p.end(), 0);
used.resize(n + m, 0);
clr.resize(n + m, -1);
g.resize(n + m);
tg.resize(n + m);
vector<int> cur(q);
iota(cur.begin(), cur.end(), 0);
calc(0, q + 1, cur);
ans.pop_back();
reverse(ans.begin(), ans.end());
for (long long x : ans) cout << x << '\n';
return 0;
}
Fast tutorial
Interesting problemset
For problem D I kind of simulated the process by brute force, submission: 267758186
Notice that
In my solution I simulate the process by going from lower operation cost $$$a_i - b_i$$$ and higher material cost $$$a_i$$$ to higher operation cost and lower material cost. On each step remaining metals with amount of ingots $$$c_k < a_i$$$ are useless for me, and any metals with $$$c_k \geq a_i$$$ will be reduced to metals with remaining quantity less than $$$a_i$$$, but at least $$$b_i$$$.
I was expecting this solution to get hacked or fail system tests, but somehow it's still standing (although I've seen some similar solutions getting hacked). After pondering on why did it not get TLE, I realized that each unique remaining value of $$$c_k$$$ will be considered at most once, and since there are at most $$$10^6$$$ unique values of $$$c_k$$$ the total time complexity will be amortized.
This solution is a result of me gaslighting myself that there are no more than $$$O(\sqrt{max(a_i)})$$$ unique differences $$$a_i - b_i$$$ (which is obviously not true).
I did exactly the same, also waited to be hacked and then understood..
can you please explain why you were able to use each value of ck atmost once? also, why does using lower_bound work here, shouldn't you use maximum amount of ingots for the best case (minimum difference)
Let's go over from a brute force solution to a more optimized one step by step:
Iterating over all kinds of weapons and then for each weapon iterating over all kinds of ingots is $$$O(N \cdot M)$$$
Now let's group different kinds of ingots with the same remaining quantity together, since if you can calculate the amount of xp for a single kind of metal with remaining $$$c_k$$$ ingots, you can do the same for $$$X$$$ kinds of metals with $$$c_k$$$ remaining ingots. You could store counts in an array or in a map. Since there could be $$$M$$$ unique values of $$$c_k$$$ you still would be iterating over all kinds of metals, leading to $$$O(N \cdot M)$$$ complexity
Now get rid of the values which we cannot smelt anything from, on each step it's useless to consider values $$$c_k$$$ lower than $$$a_i$$$, this is where
lower_bound
comes in. Also notice that after each smelting operation all values $$$c_k \geq a_i$$$ will get mapped to some values strictly less than $$$a_i$$$. Picture it like this: you pick the largest $$$a_0$$$ first, you erase all values $$$c_k \geq a_0$$$, then you pick second largest value $$$a_1$$$ and erase all values $$$a_1 \leq c_k < a_0$$$, so on and so forth. In total you will not iterate over more than $$$max(a_i) = 10^6$$$ unique values of $$$c_k$$$ (probably even less under given constraints).I did what you stated in 267754245, but it got hacked :( Could anyone tell me what I missed in my solution?
yeah, i also do the same thing and waited to be hacked. As the blogger said, this complexity is hard to think through on the field. But as long as we find this similar to the monotone queue optimization DP property, coupled with memory search, it is easy to amorize the complexity. If you were a hack, you would have to make each a minus b different and find a number, and then take the remainder of each of these A minus b's different, which is obviously impossible to do.
actually the first observation isn't it better to use the high initial material cost first ? like because that would let us have more ingots retained finally because of b ? better for future use. similarly for second one too we can argue the same its better to try using the higher cost aj if thats not possible to craft then go to aj having lower cost , for future benefit .
I was solving this today and my initial idea was the same but i was not sure that this would work since my mind was occupied by the n * m time complexity which would give tle. Nice solution btw.
Detailed Video Editorial | English Language
B : https://youtu.be/izeasCQM6Mc
C : https://youtu.be/K9Lj5WFtwXc
Can anyone explain me why lcs was not working in problem B I was able to solve but still confused where it could have gone wrong
lcs does not work because as per the question we need to find the longest contiguous substring of b that occurs as a subsequence in a. for example if a="ab" and b="sasb" the lcs is "ab" with length 2 but as we can see it is not useful for us as in our final string there must be a 's' between 'a' and 'b'. the answer string would be "sabsb" with length 5.
Actually you needed to find the longest contiguous substring of b that occurs as a subsequence in a. It can be done using LCS algorithm only, it just need 1 little modification in the else part. Instead of finding max(dp[i — 1][j], dp[i][j — 1]), we can do this
can you elaborate it aa bit more?i was trying the same thing but i don't know how this works.
I don't understand, why not use greedy for values of x <= A in D?
actually we can get accepted by using greedy.
here's the link.
I calculated the string using a bruteforce. It seems to be missing a testcase. What is it that I am missing in this: https://mirror.codeforces.com/contest/1989/submission/267683386
you are ignoring the order when using the hashmap, for this test case for example:
the answer should be 5
Sorry the link was wrong, could you check this solution: https://mirror.codeforces.com/contest/1989/submission/267710157
in the first while you ignore only characters from b that do not appear in a, this is not necessarily optimal, for example in this case:
your code gives 6, when it should be 5.
even if "ac" appears in the string a, it is better to ignore this prefix in the optimal solution.
Got it now. Thanks!
267914972 can anyone please explain why this might be giving a TLE? Is that the priority queue? I used it as a max-heap as I believe I should use maximum possible ingots each time, for the minimum difference in crafting and smelting
i did the same and got tle.
problem B test case sample can drive you insane they can work with any wrong algorithm you come up with
So LCS is one of the wrong algorithms?
yes, I used LCS earlier and got scammed
Actually you needed to find the longest contiguous substring of b that occurs as a subsequence in a. It can be done using LCS algorithm only, it just need 1 little modification in the else part. Instead of finding max(dp[i — 1][j], dp[i][j — 1]), we can do this
I got it.The ordinary LCS cannot tell which string is substring and which is subsequence.
can anyone please explain why i'm getting wrong answer on B 267919922
The order of the elements in the string are important by just counting the elements, you are ignoring the order .
As usual, BledDest blesses us with high-quality problems!
Can someone please help me with D. I can't figure out why this is giving TLE on testcase 5
267937823
Are D tests weak? My solution uses memoization with a vector of maps and passes in 2.8 seconds. My solution relies on the fact that if the net costs are very high or very low, then all of the metal is expended quickly. For this reason I think the worst case would be on the order of O(log n sqrt(max) n)? Submission: https://mirror.codeforces.com/contest/1989/submission/267939272
my solution for question e was this : https://mirror.codeforces.com/contest/1989/submission/267952069
tried solving B using dp, why doesn't it work?
https://mirror.codeforces.com/contest/1989/submission/267759332
If you are using LCS logic,it will not work in this question.
Actually you needed to find the longest contiguous substring of b that occurs as a subsequence in a. It can be done using LCS algorithm only, it just need 1 little modification in the else part. Instead of finding max(dp[i — 1][j], dp[i][j — 1]), we can do this
267826797 ques b) can anyone why its not giving right output or on which test case it fails
bc bxbc
I tried to solve Problem C, I am calculating
x, y pos & neg
in similar way as shown in the editorial. after that i tried greedy approach, i got rid of the for loop and made bothx and y
equal by doing this.now we have a simple case where
x==y
which can be solved easily by doing thisEDIT: ACCEPTED
For people why LCS is not working for problem b actually you needed to find the longest contiguous substring of b that occurs as a subsequence in a. It can be done using LCS algorithm only, it just need 1 little modification in the else part. Instead of finding max(dp[i — 1][j], dp[i][j — 1]), we can do this
F can be solved by using the well-known Radecki algorithm
For problem D i have written same solution as given in tutorial but it is giving TLE on test case 9
267975922 can anyone justify?
no fastIO
got accepted thanks!
[resolved]
For D, I think I'm doing the same thing that's mentioned in the tutorial but I'm getting TLE. Can someone please help me here? I don't know what's causing the TLE. https://mirror.codeforces.com/contest/1989/submission/268040829
I got it. IO was slow :( Adding
ios::sync_with_stdio(false);
resolved itBro you are a lifesaver. Thank you.
Can anyone tell me how to do B by graphs?
Appreciate the Todd Howard reference in Q4
Hey can someone give me a case for which my solution fails My solution — a.size + b.size — longestCommonSubsequence(a,b)
cax crxd
// Online C++ compiler to run C++ program online
include <bits/stdc++.h>
using namespace std;
define ll long long
void solve(){ string a,b; cin>>a>>b;
ll n=a.size(); ll m=b.size(); ll i=0,j=0; ll ans=n; vectorvis(n,false); int match=0; while(j<m){ if(i>=n){ i=match; // i++; ans++; j++; }
}
cout<<ans<<endl;
}
int main() {
cin>>t; while(t--){ solve(); }
}
can anyone plz tell where i m getting wrong???
// Online C++ compiler to run C++ program online
include <bits/stdc++.h>
using namespace std;
define ll long long
void solve(){ string a,b; cin>>a>>b;
ll n=a.size(); ll m=b.size(); ll i=0,j=0; ll ans=n; vectorvis(n,false); int match=0; while(j<m){ if(i>=n){ i=match; // i++; ans++; j++; }
}
cout<<ans<<endl;
}
int main() {
cin>>t; while(t--){ solve(); }
}
can anyone plz tell me where i m wrong???
any one knows why the DP on problem C is wrong
is it not always optimal to take the maximum min at each state ? I tried making a simple dp[n][2] dp[i][0] means to take the first option and extend from the maximum min from dp[i-1] and dp[i][0] means to take the second option and also extend max min from dp[i-1]
can some help why this is wrong ? here is my submission https://mirror.codeforces.com/contest/1989/submission/268130928
hey you got the answer? i was using the same approach but so far no luck with dp
no it's not solvable with dp it's not always optimal to only extend the maximum min, sometimes you need to extend a non optimal sub answer to reach the optimal final answer so it's not dp at all I guess. try to think greedy .
Can anyone please explain why this Code is not working? I can't find where I have made a mistake. Is my approach wrong?
Amazing editorial of D!!!
can someone tell why this submission is wrong 268881012 for problem C
Was it intentional that O(nlogn) solutions of D Fail or is my implementation just garbage? python code
Someone please explain why this solution is taking so long for larger values of c = 1e9, even after optimisation. It is supposed to run in
O(max(a_i)*n)
time but it's not.In question C. Can I not do dp here. Like checking for all the possibilities and then taking the most appropriate solution.
In my case i did the solution that is from awoo(specifically the first 1), is this considered a bad practice??
Here is the code: