We would like to thank all the participants who helped us and Codeforces to overcome the 40.000 registered users for the first time
Idea: diskoteka
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int q;
cin >> q;
while (q--) {
int n, m;
cin >> n >> m;
vector<string> carpet(n);
for (int i = 0; i < n; ++i) {
cin >> carpet[i];
}
string vika = "vika";
int fnd = 0;
for (int i = 0; i < m; ++i) {
bool check = false;
for (int j = 0; j < n; ++j) {
if (carpet[j][i] == vika[fnd]) {
check = true;
}
}
if (check) {
++fnd;
if (fnd == 4) {
break;
}
}
}
if (fnd == 4) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}
Idea: diskoteka
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
vector<int> a;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
if (i && a.back() > x) {
a.push_back(1);
}
a.push_back(x);
}
cout << a.size() << "\n";
for (int el : a)
cout << el << " ";
cout << "\n";
}
return 0;
}
Idea: playerr17
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (a[1] != n) {
cout << "NO" << '\n';
continue;
}
vector<int> b;
for (int i = n; i >= 1; i--) {
while (b.size() < a[i]) {
b.push_back(i);
}
}
bool meow = true;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i - 1]) {
cout << "NO" << '\n';
meow = false;
break;
}
}
if (meow) {
cout << "YES" << '\n';
}
}
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
int l = 0, r = min<int>(2e9, 2 * n);
while (r - l > 1) {
int m = (l + r) >> 1;
// m = x + y, answer = x + 2 * y
if (m * (m - 1) / 2 + m < n) {
l = m;
} else {
r = m;
}
}
int y = n - r * (r - 1) / 2;
if ((r + 1) * r / 2 <= n) {
cout << min(r + y, r + 1 + n - (r + 1) * r / 2) << "\n";
} else {
cout << r + y << "\n";
}
}
return 0;
}
1862E - Kolya and Movie Theatre
Idea: pavlekn
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int t;
cin >> t;
for (int _ = 0; _ < t; ++_) {
int n, m, d;
cin >> n >> m >> d;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int ans = 0;
set<pair<int, int>> s;
int sum = 0;
for (int i = 0; i < n; ++i) {
int cur = sum + a[i] - d * (i + 1);
ans = max(ans, cur);
if (a[i] > 0) {
s.insert({a[i], i});
sum += a[i];
if (s.size() >= m) {
sum -= (s.begin()->first);
s.erase(s.begin());
}
}
}
cout << ans << endl;
}
return 0;
}
1862F - Magic Will Save the World
Idea: diskoteka
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int q;
cin >> q;
while (q--) {
int w, f, n;
cin >> w >> f >> n;
vector<int> s(n);
int sum_s = 0;
for (int i = 0; i < n; ++i) {
cin >> s[i];
sum_s += s[i];
}
vector<bool> dp(sum_s + 1);
dp[0] = true;
for (int i = 0; i < n; ++i) {
for (int w = sum_s; w - s[i] >= 0; --w) {
dp[w] = dp[w] || dp[w - s[i]];
}
}
int ans = 2e9;
for (int i = 0; i <= sum_s; ++i) {
if (dp[i]) {
ans = min(ans, max((i + w - 1) / w, (sum_s - i + f - 1) / f));
}
}
cout << ans << "\n";
}
return 0;
}
Idea: diskoteka
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
if (n == 1) {
int q;
scanf("%d", &q);
while (q--) {
int pos, val;
scanf("%d %d", &pos, &val);
cout << val << " ";
}
cout << "\n";
continue;
}
multiset<int> aset;
for (int i = 0; i < n; ++i) {
aset.insert(a[i]);
}
multiset<int> deltas;
for (auto it = ++aset.begin(); it != aset.end(); ++it) {
auto prev = it;
--prev;
deltas.insert(*it - *prev);
}
int q;
scanf("%d", &q);
while (q--) {
int pos, val;
scanf("%d %d", &pos, &val);
auto it = aset.find(a[pos - 1]);
auto nxt = it, prev = it;
++nxt; --prev;
if (it == aset.begin()) {
deltas.erase(deltas.find(*nxt - *it));
} else if (it == --aset.end()) {
deltas.erase(deltas.find(*it - *prev));
} else {
deltas.erase(deltas.find(*nxt - *it));
deltas.erase(deltas.find(*it - *prev));
deltas.insert(*nxt - *prev);
}
aset.erase(it);
aset.insert(val);
it = aset.find(val);
nxt = it, prev = it;
++nxt; --prev;
if (it == aset.begin()) {
deltas.insert(*nxt - *it);
} else if (it == --aset.end()) {
deltas.insert(*it - *prev);
} else {
deltas.insert(*nxt - *it);
deltas.insert(*it - *prev);
deltas.erase(deltas.find(*nxt - *prev));
}
a[pos - 1] = val;
cout << *--aset.end() + *--deltas.end() << " ";
}
cout << "\n";
}
return 0;
}
Code of problem C is giving WA on test 1 and it is written that ai <= n ( There won't be any memory issues since all ai≤n ) but in the problem it is given that ai is upto 1e9
yes but the first element is N and the numbers are sorted in decreasing order so all numbers are at most N.
thanks for the help
Why there are only 3 comments, even though the edt was published 8 hours ago?
because the editorial has not been attached to the round so only those people who search the recent actions will get it
To be honest, the solution was not very good.
If someone prefers video explanations. Here is my Live Screencast of solving problems [A -> F] (with commentary).
PS: Don't judge me by my current rating :(
.
Helpful.
Can anyone explain me the thought process of C, I cannot seem to understand from the editorial :(
Which part did you had trouble understanding ?
The main part
This part
while (b.size() < a[i])
why are we doing this?U can try to draw an example and ovserve, it's easy to find that when you rotate the original shape, the 'height' (the value of a[i]) is the wide(the number of 'i's) of the new shape. just do it by using this code.(maybe my code is clearly enough and U can have a look)
your code are kind of different from the editorial. But I think they are same in thoughts
thx, I might caught you up after this contest.(XD)
alright. see you in next div1 + 2, I must become Blue earlier than you!
Oh two funny friends
in third question i am getting --> wrong answer Answer contains longer sequence [length = 10000], but output contains 9969 elements. does anyone know how to fix this?
show me your code plz
https://codeforces.net/contest/1862/submission/220417160
Actually, it's index of pre out of bound. So I change it into map and got accepted. https://codeforces.net/contest/1862/submission/220468121
Thank you :)
When will we get the ratings for the problems we've solved? Im new here and just started giving contests so im sorry i dont properly know how things happen and how long it takes
after maybe 5 hours the main tests will run which will take roughly 2 hours to complete, after that it takes another 2-4 hours for the rating changes to update. You can use an extension like cf predictor to predict your rating change accurately.
I'm so bad that I only thought of the dichotomy of O (n log n) for problem C. I'm sorry for my poor English
220231839 Maybe there is another way to solve D.
No... sqrt() takes logarithmic time complexity, the same as binary search...
Thanks for your comment! I didn't know this until you told me.
There are many grammar typos in Problem D if someone wants to correct them for future people upsolving it.
Why is it giving WA if I use Combination to solve problem D?
Combinatorics isn't entirely wrong, but requires some observations.
Take the sample of making 179 different ice creams as an example. If we use a combinatorics approach, we find that; 19C2 = 171 and 20C2 = 190
However, note that the question asks for 179 to be the exact number of ice creams; therefore 190 is in fact incorrect as there are TOO MANY flavours.
The work-around is the observation that we can increment the number of flavours by 1 simply by doubling up on a same-kind flavour. Thus, we can use combinatorics to find the lowerbound with distinct flavours, and then double up on each flavour until we end up with the correct answer.
My solution is attached for clarity: https://codeforces.net/contest/1862/submission/220240409
Note that you must optimise your starting point to prevent TLE
Actually, problem F can be solved using random algorithm, which means you can randomly swap several monsters and defeat them from left to right using water mana first and then fire mana. After approximately 10000 times of attempts, you may pass all the tests, and it runs really fast.
RIP scam failed
Hello (from RHEXAOC), don't be upset (too much) about system retesting. I (as well as you) enjoy nondeterministic approaches.
You basically, changed, so-called, knapsack, which is, taking advantage of natural numbers, to sampling a permutation of monsters. There are too many permutations but the specific projection of that permutations to { yes, no } allowed you to pass preliminary tests. I made a test by myself that your program behaved differently than my program. I would be surprised in other case, that's why.
As a bonus let me teach you a knapsack, if you wish. Here we, as I said before, take advantage, that a problem is defined on natural numbers. Which allows us to allocate a cell to every sum. So we can store in that sell a maximum cost. In this problem there are no costs just sum.
Now we have bits set on every possible sum. In case you don't see: when we don't take any element, we have all sums { 0 }; than we take an element and we have { 0, a }, then we take the next element and sums become { 0, a, b, a + b } and so on.
In general case of knapsack, for all possible sums, we store a maximal cost, not just one bit. I guess it is somehow possible to do the opposite (for all costs store something...). You might enjoy a math notation of knapsack problem for some purposes of your own. That's it.
My D question is WA9 at C++20, but the code passes when I hand it over to C++17, can any god help me to explain the reason of this condition, thanks!
C++20
C++17
use sqrtl not sqrt
Is this round unrated?
It's rated, see this comment on the rules. I think the ratings will be updated in a few hours, about 6-8 hours, maybe before that.
In D, if x represents 2 balls shouldn't we seek to minimise 2x+y? I am not getting the part with the inequalities:(
what is y?
I think you misunderstood a little bit here (the editorial isn't that clear as well, in my opinion) $$$\newline$$$ We're not minimizing $$$x+y$$$, we're maximizing it. Let assume that we've found a suitable value of $$$x+y$$$, denote this value $$$x+y=m$$$. Then solving for $$$x$$$ and $$$y$$$, we get $$$x=n-\frac{m(m-1)}{2}$$$, $$$y=\frac{(m+1)m}{2}-n$$$ and $$$2x+y=n-\frac{m(m-3)}{2}$$$. $$$\newline$$$ (Keep in mind that $$$m$$$ has to be chosen such that $$$x,y$$$ are non-negative, this will be important later)$$$\newline$$$ Now consider the function $$$f(x)=\frac{x(x-3)}{2}$$$, we want to maximize it. Because $$$f(1)=f(2)$$$ , we can just ignore the case where $$$m=1$$$ and just consider $$$m \geq 2$$$. Since $$$f(x)$$$ is strictly increasing for $$$x\geq 2$$$, we just have to maximize $$$m$$$ now. From $$$x \geq 0$$$ and $$$n=x+\frac{m(m-1)}{2}$$$ we get $$$n\geq \frac{m(m-1)}{2} (*)$$$. $$$\newline$$$ Let $$$m_0$$$ be the largest integer such that that when plugging $$$m=m_0$$$ in $$$(*)$$$, the inequality is true. Our correct answer is obtained when $$$m=m_0$$$ and we can prove it, $$$2x+y$$$ is minimized because $$$m$$$ is maximized, $$$x$$$ is obviously non-negative, $$$y$$$ has to be positive, because if $$$y\leq 0$$$ then $$$n\geq \frac{(m_0+1)m_0}{2}=\frac{(m_0+1)(m_0+1-1)}{2}$$$. $$$\newline$$$ This will contradict our choice of $$$m_0$$$ at $$$(*)$$$ because now the largest such integer $$$m$$$ is $$$m=m_0+1$$$, not $$$m=m_0$$$ and we're done
This proof is quite hard to think during the time of the contest !
I agree! I believe the author created the problem base on a famous result in math that is the function $$$f(x,y)=x+\frac{(x+y)(x+y+1)}{2}$$$ is a bijection from $$$\mathbb{N^2}\to \mathbb{N}$$$. But in the end, it's not a math competition so proofs aren't that necessary as long as you're doing the right things even if you can't prove it :D
does this famous result have a name if I want to learn more about it and those like it?
Sorry for replying late but if you're still interested, you can search for "Bijection from NxN to N" like in this post: https://math.stackexchange.com/questions/2781594/bijective-function-from-n-to-n-x-n
just get minimum value x such that x(x-1)/2 <= n
//number of ways of choosing 2balls from x balls
now add the diff //repeated balls
int n; cin >> n;
int x = sqrt(2 * n);
if (x * (x + 1) <= 2 * n) x++;
int diff = n — (x * (x- 1) / 2);
cout << x + diff << endl;
Can someone tell me why greedy approach doesn't work for F.
The problem breaks down to checking whether all the items can be filled inside 2 bags of different sizes.
Here's what I thought:
- Sort the items by their weight in descending order.
- Iterate over the items array and put them inside the bag which has more space left.
sin_shark Consider the bag sizes to be — 14 and 10.The items are- 10,8 and 6. If we use greedy approach,1st item goes in bag 1 (new size=14-10=4),2nd item goes in bag 2(new size=10-8=2) ,3rd item will be left as bag are of sizes 4 and 2.
Instead, we can put 2nd and 3rd item in bag 1 and 1st item in bag 2.(works)
Thanks, but can you give an intuitive idea on why it wouldn't work.
Can anyone explain to me the first part of the tutorial of G?
Lets say the sorted array is
a1 a2 a3
3 2 1
As you can see we are adding 2 to a2 and 3 to a1.It means the difference between the adjacent elements is reduced by 1 when we add this AP to sorted sequence. This will go on until the maximum difference between the adjacent elements is reduced to zero. Then we will have same elements and we can remove one element.
Thus the answer is maximum element + maximum difference.
Hope it helps !!
Can anyone tell me, why Online Judge is giving different output than my local machine for following code:
220249582
Its because you have used sqrt. Use sqrtl for long long integers. You can also use binary search to find the square root.
Hi, in problem F, the knapsack method that is used, wont the number of computations be of the order 10^8, since knapsack is of time complexity O(N*SUM). How is it accepted then?
10 ^ 8 operations work fine on CF.
Then whats the limit in CF, I am pretty confused rn.
I created dp[100*1e6] in F problem but this gives me memory limit exceed why? https://codeforces.net/contest/1862/submission/221041512
this is because due to binary search it will become almost 30*dp[1e8] which will definately get TLE or MLE
What about E?I can't able to solve E.Can u give me hint ?
For every positive movie show we will take at most m-1 positive shows before it and subtract currentIndex*cnt from the sum because of the penalty.
TL is 4s, even with 1s I think it can (barely) pass with optimizations Not to mention there's the bitset optimization
I just tested it, you can easily pass without bitsets in only 109ms 220410300
Problem F: Can some one tell me why isn't the tutorial solution giving TLE as it is order of O(sum*n) i:e O(10^8), or am I wrong...
It is guaranteed that the sum of n over all test cases does not exceed 100.
So the total time complexity is O(10^8) as u said, and this fits perfectly for the time constraint :D
Because
vector<bool>
can be understood asbitset
, they are both very fast and have a complexity ofO(n*sum/64)
.vector does not support shift operations thus you can't get 1/64 constant factor improvement which is possible when using bitset.
This is a very common scenario for
bitset
.Problem B,
For b=[4,6,3] why a=[4,6,3,3] is not a valid sequence ?
It is a valid sequence.
In third question i am getting --> wrong answer Answer contains longer sequence [length = 10000], but output contains 9969 elements. does anyone know how to fix this?
how to do E ques by dp (knapsack maybe dp[currIdx][prevIdx][moves_till_now])
To be precise, this is recursion.
Can someone please explain as to why commented code is giving TLE for G.
Thanks in advance for the help :).
Can somebody explain that dp state in the solution for problem F?
$$$dp[i]$$$ means that if we can get the value i from the sum of any number in s(the vector in the solution).
map<int, bool> ex; what does
ex
define in your solutionIt means the sum exists(can be made by adding any number of monster's value).
Note that the total strength of the monsters is given to us. Therefore, it is enough for us to spend as much of the available water mana as possible, so that there is enough fire mana left for the remaining monsters. This is a well-known knapsack problem.
Can anyone tell me which well-known knapsack (variation) this is?
0-1 knapsack.
Can you please explain why is it a 0-1 knapsack.
Where are weights and capacities etc. which are parts of knapsack.
I can't understand.
Also, If possible suggest some resources(problems) to learn these types of implicit knapsack.
The total sum of the monsters' strength is the knapsack' s capacity. Each strength of the monster is both its weight and capacity.
Then in my first solution(220277552) which is MLE dp[i][j] means The maximum amount of capacity that the first i monsters can occupy without exceeding the capacity j.Then I used scrolling array optimization in my second solution(220282293) and AC.
Then we can get a set including the sum of the strength by selecting any number of monsters from all and adding their strength together.
Then we can enumerate all the divisions of the sum of the strengths of all monsters into two parts, and use the set we just got to judge whether it can be divided in this way, and constantly update the least amount of time, that is, the answer.
I don't have good advice on the specific problems of some implicit knapsack, you can search the Internet to find them.
include <bits/stdc++.h>
using namespace std;
long long knapsack(long long W, vector& weights, vector& values) { long long n = weights.size();
}
long long minTimeToDefeatMonsters(long long w, long long f, vector& monsters, vector& values) { long long totalWeight = accumulate(monsters.begin(), monsters.end(), 0LL); long long low = 0, high = totalWeight / f + 1;
}
int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);
} CAN YOU PLEASE HELP ME , THIS SOLUTION GIVING TLE ON TESTCASE 29 , HOW CAN I OPTIMIZE IT EVEN FURTHER
can someone explain the TC of problem F.
problem G is bad because of its TL and n = 1 case
For problem D
just get minimum value x such that x(x-1)/2 <= n //number of ways of choosing 2balls from x balls
now add the diff //repeated balls
int n; cin >> n;
int x = sqrt(2 * n);
if (x * (x + 1) <= 2 * n) x++;
int diff = n — (x * (x- 1) / 2);
cout << x + diff << endl;
Why we take min(2e9, 2 * n) in D?
Don't confuse brother you can look at my code or other high rated programmer. My code — 262501894
I misunderstood problem C. In this problem, for a very long time, I was stuck by calculating the total no of diff balls that are required to get different pairs>=n. I was considering that to be my answer, but here I was asked to calculate the minimum no of balls required to get exactly n different ice-creams not more than that. So for this using binary search I calculated the minimum no of different balls required to get the different pairs>=n. Let's say I got res if (res*(res-1))/2==n then this res will be my final answer. Otherwise, I need to do some manipulations. I will go do res--, now this res will make less no of pairs than n find out how much n exceeds this, assume it to be temp. We will be requiring this difference many balls. Hence, in this case our final answer will be (res+temp).
can anyone tell that i am correct or not in approach for 1862B- Sequence Game where the output is For each test case, output two lines. In the first line, output a single integer the length of the sequence (2). In the second line, output
integers 1,2,3,, (1109) the assumed sequence that Vika could have written on the first piece of paper. since the output sequence is can be equal to n length so input sequence can also be answer everytime as n <= m <= 2n given in the question, it can be possible that the he thought the same sequence already for Custom Input 1 2 3 4 5 Custom Output 1 2 3 4 5