1282A - Temporarily unavailable
Idea: MikeMirzayanov Preparation: MikeMirzayanov
Editorial
Tutorial is loading...
Solution (MikeMirzayanov)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
int main() {
int t;
cin >> t;
forn(tt, t) {
int a, b, c, r;
cin >> a >> b >> c >> r;
int L = max(min(a, b), c - r);
int R = min(max(a, b), c + r);
cout << max(a, b) - min(a, b) - max(0, R - L) << endl;
}
}
1282B1 - K for the Price of One (Easy Version), 1282B2 - K for the Price of One (Hard Version)
Idea: MikeMirzayanov, unreal.eugene Preparation: Supermagzzz
Editorial
Tutorial is loading...
Solution (Supermagzzz)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main() {
int cntTest;
cin >> cntTest;
for (int test = 0; test < cntTest; test++) {
int n, p, k;
cin >> n >> p >> k;
int pref = 0;
int ans = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
for (int i = 0; i <= k; i++) {
int sum = pref;
if (sum > p) break;
int cnt = i;
for (int j = i + k - 1; j < n; j += k) {
if (sum + a[j] <= p) {
cnt += k;
sum += a[j];
} else {
break;
}
}
pref += a[i];
ans = max(ans, cnt);
}
cout << ans << "\n";
}
}
Idea: AdvancerMan Preparation: Supermagzzz
Editorial
Tutorial is loading...
Solution (Supermagzzz)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main() {
int cntTest;
cin >> cntTest;
for (int test = 0; test < cntTest; test++) {
ll n, t, a, b;
cin >> n >> t >> a >> b;
vector<pair<ll, ll>> v;
vector<int> hard(n);
int cntA = 0, cntB = 0;
for (int i = 0; i < n; i++) {
cin >> hard[i];
if (hard[i]) {
cntB++;
} else {
cntA++;
}
}
for (int i = 0; i < n; i++) {
ll time;
cin >> time;
v.push_back({time, hard[i]});
}
v.push_back({t + 1, 0});
sort(v.begin(), v.end());
ll cnt1 = 0, cnt2 = 0;
ll ans = 0;
for (int i = 0; i <= n; i++) {
ll need = cnt1 * a + cnt2 * b;
ll has = v[i].first - 1 - need;
if (has >= 0) {
ll canA = min((cntA - cnt1), has / a);
has -= canA * a;
ll canB = min((cntB - cnt2), has / b);
ans = max(ans, cnt1 + cnt2 + canA + canB);
}
int l = i;
while (l < v.size() && v[l].first == v[i].first) {
if (v[l].second) {
cnt2++;
} else {
cnt1++;
}
l++;
}
i = l - 1;
}
cout << ans << "\n";
}
}
Idea: unreal.eugene Preparation: unreal.eugene
Editorial
Tutorial is loading...
Solution (Darui99)
#include <bits/stdc++.h>
using namespace std;
int f(string s) {
cout << s << endl;
int w;
cin >> w;
if (w == 0)
exit(0);
return w;
}
int main() {
const int N = 300;
int st = f(string(N, 'a'));
int n = 2 * N - (st + f(string(N, 'b')));
string t(n, 'a');
int A = N - st, B = n - A;
st = B;
for (int i = 0; i < n - 1; i++) {
t[i] = 'b';
if (f(t) > st)
t[i] = 'a';
else
st--;
}
if (st)
t.back() = 'b';
f(t);
}
Idea: MikeMirzayanov, AdvancerMan Preparation: AdvancerMan
Editorial
Tutorial is loading...
Solution (AdvancerMan)
#include <bits/stdc++.h>
using namespace std;
void dfs_order(int u, int p, vector<vector<int>> const& g, vector<int> &order) {
for (auto v : g[u]) {
if (v != p) {
dfs_order(v, u, g, order);
}
}
order.push_back(u);
}
void get_order(map<pair<int, int>, vector<int>> const& in, int n, vector<int> &order) {
vector<vector<int>> g(n);
for (auto e : in) {
auto vs = e.second;
if (vs.size() == 2) {
g[vs[0]].push_back(vs[1]);
g[vs[1]].push_back(vs[0]);
}
}
dfs_order(0, -1, g, order);
}
void dfs_polygon(int u, vector<vector<int>> const& g, vector<bool> &used, vector<int> &res) {
if (used[u]) {
return;
}
used[u] = true;
res.push_back(u);
for (auto e : g[u]) {
dfs_polygon(e, g, used, res);
}
}
void get_polygon(map<pair<int, int>, vector<int>> const& in, int n, vector<int> &polygon) {
vector<vector<int>> g(n);
for (auto e : in) {
if (e.second.size() == 1) {
auto edge = e.first;
g[edge.first].push_back(edge.second);
g[edge.second].push_back(edge.first);
}
}
vector<bool> used(n);
dfs_polygon(0, g, used, polygon);
}
void get_polygon(vector<vector<int>> const& in, int n, vector<int> const& order, vector<int> &polygon) {
vector<pair<int, int>> listp(n, {-1, -1});
auto last = in[order.back()];
for (int i = 0; i < 3; i++) {
listp[last[i]] = {last[(i + 1) % 3], last[(i + 2) % 3]};
}
for (int i = (int) order.size() - 2; i >= 0; i--) {
auto x = in[order[i]];
int j = 0;
while (listp[x[j]] != pair<int, int>{-1, -1}) {
j++;
}
int x1 = x[j], x2 = x[(j + 1) % 3], x3 = x[(j + 2) % 3];
if (listp[x2].second != x3) {
swap(x2, x3);
}
listp[x1] = {x2, x3};
listp[x2].second = x1;
listp[x3].first = x1;
}
polygon.push_back(0);
int now = listp[0].second;
while (now != 0) {
polygon.push_back(now);
now = listp[now].second;
}
}
void out(vector<int> const& v) {
for (auto e : v) {
cout << e + 1 << ' ';
}
cout << endl;
}
void solve() {
int n;
cin >> n;
vector<vector<int>> in(n - 2, vector<int>(3));
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < 3; j++) {
cin >> in[i][j];
in[i][j]--;
}
sort(in[i].begin(), in[i].end());
}
map<pair<int, int>, vector<int>> mp;
for (int i = 0; i < n - 2; i++) {
auto tri = in[i];
for (int j = 0; j < 2; j++) {
for (int k = j + 1; k < 3; k++) {
mp[{tri[j], tri[k]}].push_back(i);
}
}
}
vector<int> order;
get_order(mp, n, order);
vector<int> polygon;
get_polygon(in, n, order, polygon);
// get_polygon(mp, n, polygon); // another solution
out(polygon);
out(order);
}
int main() {
int t;
cin >> t;
for (int t_num = 1; t_num <= t; t_num++) {
solve();
}
return 0;
}
Could anyone explain why the problem D answer for the below case is 1?
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
When can the student leave to get 1? If the student leave T=6, he can solve 3th problem, but the 2th problem makes his score 0. Am I missing something?
Petya could solve one of easy problems and leave exam at time 2.
In this case, you should prioritize easy problem over hard problem, which takes exactly 2 minutes.He can leave immediately when he solve a easy one at t=2.
Actually, it's problem C, not D.
Solution to problem B is not very clear yet. Can someone please explain it in little more detail?
Sort the items by price. Let us say we wanted to buy only all of the first i items by paying the minimum price possible, call this minimum price needed dp[i].
If i >= k-1 (0 based index).
We will definitely have to buy the most expensive item but on purchasing that we can get items from i-k+1 to i-1 for free.
So dp[i] = cost of item i + dp[i-k]. If dp[i] <= budget, our answer is atleast i+1.
If i < k-1 the case is simpler, check the code for this case.
Also if there are n items on sale, there is no point of purchasing an expensive item but leaving out a cheaper one. In essence if you buy some ith item you will definitely buy first i-1 items also. Let me know if you'd like a proof for this too.
https://codeforces.me/contest/1282/submission/67533829
Thanks , for the explanation. Can you also explain what is said in the editorial,I think it's a little different from this(dp) approach?
I feel it's almost the same.
Not sure though.
dp is kinda parallel counting for all possible prefixes, while solution from editorial is serial: try first prefix, then run with step k; try second prefix, then run with step k, and so on.
Thanks, now i got it.
Wanted to ask that for i=1 to k-1 (in ur case: 0 to k-2), we can't apply the offer bcz. we don't have exactly K items to buy. So, in that case shouldn't it be 1 item(not applying the offer). Your code looks like that we can buy less than K items. Can you clarify if we can buy less than K items or not?
We can definitely buy a single item without using the offer. If you buy say g items, g < k, then you have to pay sum of individual cost of those items equivalent to buying each of them one at a time without using the offer.
In my code dp[i] where i < k-1 is simply the prefix sum.
Let me know if this clears your doubt.
You have both the option of applying the offer or not applying it.
can you explain more how offer is used and not used in both the conditions (i >= k-1 and i < k-1 )
You may only use the offer if you buy exactly k items. Say K=5, you want to buy 7 items, you can buy 2 items individually without using an offer and 5 items on offer. Not sure what exactly you are trying to ask but still hope it helps.
t=1 n=5 p=6 k=2 A=1 2 4 5 7
for this case if we buy price of 5 we have {5,4,1} as purchased items and the optimal buy would be {4,2,1} ,here both types of purchase are optimal.So how can we be assured of the fact that higher purchase will not lead to best result
Hi kartik8800 could you please elaborate the proof that after sorting if I buy some ith item I will definitely buy first i-1 items also
Try proving it on the following lines :
There exists no way to get exactly X items for a cheaper price than the way in which you get the first X items.
Think of the three cases : cheapest way to buy X items when X < K, X=K and when X >= K.
Proving for X<K and X=K should be trivial.
For X>=K you can try proving by contradiction. Assume there exists a way to get exactly X items in which you buy some item j with j > X and then try to contradict this.
Let me know if you're stuck and I'll give more thought to the formal proof and if you do come up with a proof do share.
Can you please tell how to prove this fact. I am unable to come up with a proof.
My solution for problem E is similar but definitely much shorter, the main idea is to take an arbitrary triangle to remove it and recursively solve on the three sides, I have a list with the order of vertices and other for the order of triangles, in my recursive function I have a fixed side(edge) then I find one arbitrary triangle with that edge and solve similarly for both sides
Crispy and tasty code. With inline functions too. Nice.
similar with my Solution XD
I have another approach for $$$D$$$.
Assume length of $$$s$$$ is $$$l$$$. First, query with $$$t$$$ = "a" and let the response be $$$r$$$. There are 2 cases:
1- string $$$s$$$ consisted only of "b" letters, then $$$l = r$$$.
2- string $$$s$$$ had at least one "a" in it, then $$$l = r + 1$$$.
Now query with $$$t$$$ of length $$$r$$$ consisting only of "b" letters and let the response be $$$e$$$. If $$$e = 0$$$ then terminate, otherwise, You have 2 pieces of information:
1- $$$l = r + 1$$$.
2- The distance between string $$$s$$$ and string full of "b" letters of length $$$l - 1$$$ is $$$e$$$.
3- $$$s$$$ has at least one "a" in it.
Now, let $$$t$$$ be a string of length $$$l$$$ of only "b" letters. The distance between $$$t$$$ and $$$s$$$ is also gonna be $$$e$$$ (I think the only case where this is not true if string $$$s$$$ consists only of "b" letters, which we know it already doesn't. Would love a proof for this tho).
Now just like the rest of the solution in the editorial, iterate over $$$t$$$ from $$$1$$$ to $$$l$$$ and try to turn the character to "a" and query. If the distance in the response is less than $$$e$$$, then update $$$t$$$ and $$$e$$$. keep doing this until the response is $$$0$$$
For the question, Price of one easy version i tried to solve it using modification of knapsack but it gave tle on pretest 3. Did i miss something, code is as below:-
You can optimize this to linear time, since the values of all of the items are equal. For example, since an item with cost 10 and another with cost 5 are always equal value, you always want to take the one with the smaller cost.
In solution to problem B, the sort function is used which takes o(n*log(n)) time complexity. So how come the solution has linear time complexity? Please explain!
It is not, but since the $$$a_i$$$s are only from 1 to 10^4, we can use radix sort or counting sort to sort the numbers in $$$O(10^4)$$$ time
Update : As the number of testcases is large($$$10^4$$$), so it won't probably pass, so nevermind, just use quicksort.
but the editorialist solution uses builtin quicksort function brother
We can do, the solution is not linear though, it indeed is $$$O(nlog n)$$$
It will take 10^4 iteration per test case,which will give worse time complexity than nlogn.
Not at all, it would give us $$$O(10^4+n)$$$ time which is linear in n
It will surely take O(10^4) to sort the array per test case, and total no test case can be max 10^4. so here worse time complexity is O(10^8) which is worse than nlogn.
Ahh I see, then it sure is not linear, thanks for correcting :)
radix sort is $$$O(n \log(A))$$$ where $$$A$$$ is logarithm of number of possible values. counting sort is $$$O(n)$$$.
You can do $$$O(n)$$$ here, but I came up only with offline solution. It's useless.
Here is comparison:
Someone Please Help me Correct my Solution For Problem B. I am unable to find which case I am missing . It would be of great help. My Solution :- (https://codeforces.me/contest/1282/submission/67578229)
PROBLEM B
Can someone Please Explain this Test Case?
4 9 2
2 3 4 5
How the answer is 4?
You can buy goods worth 4 & 5 for 5 coins and goods worth 2 & 3 for 3 coins.Hence answer is 4
Is it allowed to do this operation multiple times?
Yes as long as you have ample amount of money left . You can buy as many items as you want
Thanks.
it is written in question**Vasya can perform one of the following operations as many times as necessary**
Yes exactly
Can you please explain again in detail? I didn't get what you said.
As we can take 2,3 as one pair and 4,5 as another pair so re money is max(2,3)+max(4,5) i.e. is 8 which is less than 9 so we have one unit of money left.. Since we have no other toy this is our final ans;
First buy 5 and then you get 4 for free and then buy 3 you get 2 for free so total cost = 8 but you have 9 and so you buy 2 goods and get 2 for free
Could anyone explain problem B in more detail
first sort the prices.let dp[i]= min cost to buy i items.for i<k-1, dp[i] is the prefix sum of prices since we cannot use the offer.else dp[i]=a[i]+dp[i-k] . solution
My submission for C giving WA. Someone please help!! Edit:: I think I got my mistake..
In problem C, why is the answer of this testcase $$$1$$$ ?
The same question has already been answered above: https://codeforces.me/blog/entry/72461?#comment-567159
Start solving an easy problem at t = 0. It'll be finished at t = 2. Then you can leave.
For the first problem(temporarily unavailable) in the fifth test case a=-10 b=20 c=-17 r=2 , the coverage will be from [-15,-19].so a to b must added, the answer must be 31, if I'm wrong case someone explain why I'm wrong.
The time taken to travel from a to b is 30 units right? Cause you travel from 0 to 1 in 1 unit time
Please find the error in my code i don't know what's going wrong here My Submission for Problem D
For Problem B. first sort all the goods by its price.and take this array as p[]; then let f(x) means the least money he should pay to buy first x goods. so if x <= k , f(x) = f(x-1) + p[x] if x > k , f(x) = min(f(x-1) + p[x],f(x-k) + p[x]) // use offer or not then iterater all the f(x),find the most x satifies f(x) <= the money I have. does I make this problem more complex?
problem b order is $$$O(N^2)$$$ ?
I was also confused initially
But notice you visit every index just once so the order is O(n).
yes, order is (k) * (n / k) = n
can someone explain C approach briefly?
In tutorial of problem 1282D - Enchanted Artifact:
Consider an arbitrary string $$$t$$$ of length $$$l$$$ and let the answer to its query be $$$q$$$. Then if we replace the letter $$$t_i$$$ with the opposite one (from
a
tob
or fromb
toa
), then we may have one of two situations:$$$q$$$ decreased by $$$1$$$, then the letter $$$t_i$$$ after the change coincides with the letter $$$s_i$$$.
otherwise the letter $$$t_i$$$ before the change matches the letter $$$s_i$$$.
I think that this is not truth. For example:
$$$s=$$$
abababababababababab
$$$t=$$$
babababababababababa
Edit distance is $$$q=2$$$ (delete the first character and insert the last one).
Let's replace some
a
in the middle withb
(the letter $$$t_i$$$ after the change coincides with the letter $$$s_i$$$):$$$t'=$$$
bababababbbababababa
Edit distance is $$$q'=3$$$ (replace this letter, delete the first character, and insert the last one).
The letter $$$t_i$$$ after the change coincides with the letter $$$s_i$$$, but $$$q$$$ didn't decreased by $$$1$$$.
Am I right?
does the author means the t is all letter 'a' at first.
so it can't be
babababababababababababa
it can only bebabababababaaaaaaaaaaaaa
so convert from a to b will make difference?Maybe, but he didn't write it.
but in his codes, he inits the t in this method. so maybe this is what he actually means.
BTW, how can this be proved?
I think because the
s
andt
has same length. So the quickest way to transform fromt
tos
is to replace eacha
tob
where the char in them are different. if a change from 'a' to 'b' make the distance greater(we need more step to make maket
equal tos
cause our change), it means this position should be 'a', otherwise, it should be 'b'.You are right. Actually, these statements are not true in general case, but they are valid if your initial string $$$t$$$ is monochrome (consists of letters of a single type) and has the same length so as $$$s$$$. Moreover, it is valid when string $$$t$$$ has a common prefix with $$$s$$$ and the remaining part is still monochrome. It is also can be proven (by induction I guess) that during these actions deletions and insertions are not necessary operations, because what can be done with these operations can also be done with changing letters in no more operations. I'll make the editorial more clean to understand soon.
Additionally,
it looks likewhen we are increasing a valid prefix in string $$$t$$$ on an initial monochrome string, the edit distance is actually equal to the Hamming distance.Can you prove these facts or post a link to the proofs for the same, as proving things is as much necessary in a contest(especially for greedy problems) as is intuition?
I would like very much a proof too :)
Yeah I think you are right, take this case also: Edit distance between (aaaa , baba) is 2. The edit distance between (abaa , baba) is also two.Even though only one character has been changed from a to b.
Could anyone explain why the solution of problem C answer for the below case is 2?
3 5 2 100
0 0 1
5 5 5
If student leave at 5, he must solve all the problem. Am I misunderstanding?
he can leave at 4, solve problem 1 and problem 2, which costs 4 minutes.he doesn't need to leave at 5
Gotcha! Thank you.
explain test case no 8 of 1282C — Petya and Exam how it is 1 it ans should be zero
I think it should be 0 too.
I am getting WA on test 2 on C.
https://codeforces.me/contest/1282/submission/67600070
Can Someone Please Help.
Thanks
instead of starting from the beginning, I have for once solved all the questions and then am deciding from the end, If the solution is permissible or not(depending on time limit, and if the next question is mandatory within that limit)
Based on your editorial of problem E, I think one can do simpler things to solve the problem:
Here, we have found $$$p$$$ and $$$q$$$ independently.
Code: 67604806
I dont get why the answer to test case 8 of problem c is 1 and not 0. In that test case the problem with lowest mandatory time is the third one, so if we want to solve one or more problems we must solve this one, but because the time it takes to solve this problem is 6 seconds, and there is a problem which its mandatory time is 6, we can not solve the third problem without solving this one or getting a zero score.So why is the answer to this test case 1? I am probably missing something here but i cant figure it out. Can someone please help me? edit* I found out how it can be 1.
did u find the answer?
Solve an easy question and leave the exam
But after solving s become 6 which is equal to next ti,so it became mandatory,how can he leave the xm
queryforces
My soln. is similar to one which is mentioned in editorial and it is passing for problem B1 but failing for problem B2. I am not able to find out the mistake. Please help.
I think
sum += ar[i]
should be replaced withsum = ar[i]
.please, can anyone tell me that why in b2 editorial solution first loop run upto only k ? why ? please, anyone reply guys.
Because if you buy greater than $$$k$$$ items without using the offer, you would spend more money, rather buy $$$k$$$ items using the offer and buy others individually.
For me personally, finding permutation of vertices was far easier than finding an order of cuts.
Do I have correct idea for problem E?
https://codeforces.me/contest/1282/submission/67669011
Idea:
Store vertices like graph. 1 piece = 6 new edges. Sort vertices by number of neighbors. On each step poll vertex V with smallest number of neighbors. V must have only two neighbors. Delete V from its neighbors. Add these two neighbors back to priority queue. Repeat while there wont be only two vertices.
Why does the link to the editorial of this round appear twice? https://codeforces.me/contest/1282
Can someone figure out what's wrong with this solution for D? 67670033
It has a similar idea as given in the tutorial.
unreal.eugene
Nice testcase 67 in D.
-3 1 2 0 how is the answer to this query for problem A 4?shouldn't it be 5? while travelling : -3 -2 -1 0 1 ,nowhere is tower signal available.
same Confusion!! Did you find the answer?
I got the idea. We need to count distance and not number of points. So, -3 to -2 -> 1 -2 to -1 -> 1 -1 to 0 -> 1 0 to 1 -> 1. So, answer is 4.
Well if that so in this test case answer should have been 6 : 1 10 7 1 : as, 1->2,2->3,3->4,4->5,(available for 6,7 and 8)8->9,9->10.
Invalid distance are 6->7 and 7->8. valid pairs 1->2,2->3,3->4,4->5,5->6,8->9,9->10. hence,answer is 7.
Yes you are right, thankyou
Could anyone help me to see why I got RE? TIA! 68362298
I doubt that the checker of Problem D calculates incorrect Edit Distance when $$$|t|>|s|$$$ because 68363205 got WA on test 1. Can anyone help me about this? Thanks.
can someone explain the algorithm to problem B2
dp solution for B2 : https://codeforces.me/contest/1282/problem/B2