What will be answer if there were only $$$4$$$ elements in the array?
Suppose if there were only $$$4$$$ elements in the array. Let them be $$$a \leq b \leq c \leq d$$$. Then the answer will be maximum of the three cases which are listed as follows:-
so clearly $$$2*(d+c)-2*(a+b)$$$ is the maximum.
So, to maximize this we can set $$$d$$$, $$$c$$$ as large as possible and $$$a$$$, $$$b$$$ as small as possible i.e. $$$d=a_n$$$, $$$c=a_{n-1}$$$, $$$b=a_{2}$$$ and $$$a=a_{1}$$$ where $$$a_i$$$ means $$$i^{th}$$$ element in sorted order of given array.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int testcases;
cin >> testcases;
for (int t = 1; t <= testcases; t++)
{
int n;
cin >> n;
vector<int> v(n);
for (int j = 0; j < n; j++)
{
cin >> v[j];
}
sort(v.begin(), v.end());
cout << 2 * (v[n - 1] - v[0]) + 2 * (v[n - 2] - v[1]) << endl;
}
}
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
ans = 2 * (a[n - 1] - a[0] + a[n - 2] - a[1])
print(ans)
1934B — Yet Another Coin Problem
At max how many $$$1$$$, $$$3$$$, $$$6$$$, $$$10$$$ are required?
Fact: You will never need more than $$$2$$$ ones, $$$1$$$ threes, $$$4$$$ sixes and $$$2$$$ tens.
Reason:
For $$$1$$$: Suppose if you used $$$k$$$ > $$$2$$$ ones, then you could have used one $$$3$$$ and $$$k$$$ — $$$3$$$ ones.
For $$$3$$$: Suppose if you used $$$k$$$ > $$$1$$$ threes, then you could have used one $$$6$$$ and $$$k$$$ — $$$2$$$ threes.
For $$$6$$$: Suppose if you used $$$k$$$ > $$$4$$$ sixes, then you could have used two $$$15$$$'s and $$$k$$$ — $$$5$$$ sixes.
For $$$10$$$: Suppose if you used $$$k$$$ > $$$2$$$ tens, then you could have used two $$$15$$$'s and $$$k$$$ — $$$3$$$ tens.
now since bound on their count is less, we can bruteforce on these count.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int testcases;
cin >> testcases;
for (int t = 1; t <= testcases; t++)
{
int n;
cin >> n;
int ans = 1e9;
for(int ones = 0; ones <= 2; ones++){
for(int threes = 0; threes <= 1; threes++){
for(int sixes = 0; sixes <= 4; sixes++){
for(int tens = 0; tens <= 2; tens++){
int brute_sum = 1*ones + 3*threes + 6*sixes + 10*tens;
if(brute_sum <= n && (n-brute_sum)%15 == 0){
ans = min(ans, ones + threes + sixes + tens + (n-brute_sum)/15);
}
}
}
}
}
cout << ans << endl;
}
}
When will the greedy logic of choosing the higher-valued coin first work?
Fact $$$1$$$: If coins of value $$$1$$$, $$$3$$$, $$$6$$$ and $$$15$$$ were only present the greedy logic of selecting the higher valued first would work.
Reason: We use coins of value one at most $$$2$$$ times, coins of value three at most $$$1$$$ time, coins of value six at most $$$2$$$ times (if it was used $$$3$$$ times, it would be better to use two coins $$$15 + 3$$$) But we can't use the coin of value $$$3$$$ and both coins of value $$$6$$$ simultaneously, because we would prefer just using $$$15$$$.
It means that these coins may sum up to $$$1 + 1 + 3 + 6 = 11$$$ or $$$1 + 1 + 6 + 6 = 14$$$ at max. So, we may use the value $$$15$$$ greedily, because the remaining part is less than $$$15$$$. When we are left with only the values $$$1$$$, $$$3$$$, and $$$6$$$, greedily solving is obviously correct, because each coin is a divisor of the next coin.
Fact $$$2$$$: We don't need more than $$$2$$$ ten coins.
Reason: Better to use $$$2$$$ fifteen coins instead of $$$3$$$ ten coins.
Using the above two facts it can be shown that the answer will have $$$k < 3$$$ ten coin, therefore, answer = $$$\min$$$($$$\text{answer}(n-10*k)+k$$$ assuming $$$1$$$, $$$3$$$, $$$6$$$ and $$$15$$$ coins are only present).
#include<bits/stdc++.h>
using namespace std;
int getAns(int n){
int ans=0;
ans+=n/15;
n%=15;
ans+=n/6;
n%=6;
ans+=n/3;
n%=3;
ans+=n;
return ans;
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr);
int testcases;
cin>>testcases;
for(int i=1;i<=testcases;i++){
int n;cin>>n;
if(n<10){
cout<<getAns(n)<<endl;
}else if(n<20){
cout<<min(getAns(n),getAns(n-10)+1)<<endl;
}else{
cout<<min({getAns(n),getAns(n-10)+1,getAns(n-20)+2})<<endl;
}
}
}
After querying "$$$?$$$ $$$1$$$ $$$1$$$" What do we know about the location of mines?
If the answer is $$$a_1$$$ the location of one of the mines is $$$(x, y)$$$ such that $$$x+y = a_1+2$$$.
First, we query "$$$?$$$ $$$1$$$ $$$1$$$" then you get a value $$$a_1$$$ and you know that at least one of the mine is on the line $$$x+y=a_1+2$$$.
Now we make two more queries on both ends of this line where it touches the grid. After these two queries, we get two possible locations of the mine. We query one of these positions if we receive $$$0$$$ as the answer then this location otherwise the other location is the answer.
Reason: One of that two locations contains a mine. The other mine could spoil the result of the query only from one end. If it was closer from the other mine to both of the ends, it would mean that going from one end to the other is shorter through that mine, than through the diagonal. That's impossible.
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--){
int n, m;
cin >> n >> m;
auto query = [&](int x, int y){
if (x < 1 || x > n || y < 1 || y > m)
return -1;
cout << "? " << x << ' ' << y << endl;
int z;
cin >> z;
return z;
};
int d = query(1, 1);
int lx = max(1, 2 + d - m);
int p = query(lx, 2 + d - lx);
int rx = max(1, 2 + d - n);
int q = query(2 + d - rx, rx);
if (query(lx + p / 2, 2 + d - lx - p / 2) == 0){
cout << "! " << lx + p / 2 << " " << 2 + d - lx - p / 2<<endl;
}else{
cout << "! " << 2 + d - rx - q / 2 << " " << rx + q / 2 << endl;
}
}
}
What if we made the second query as "$$$?$$$ $$$n$$$ $$$m$$$"?
If the answer is $$$a_2$$$ the location of one of the mines is $$$(x, y)$$$ such that $$$x+y = n+m-a_2$$$.
Using Hint $$$1$$$ and Hint $$$2$$$ if we get the same line equation i.e. $$$n+m-a_2=a_1-2$$$ we can just query one of the endpoints of this line and get the answer.
Otherwise, we can query "$$$?$$$ $$$1$$$ $$$m$$$" and we will get one more line perpendicular to the earlier two. One of the mines has to be on this line therefore there will be a mine either on the intersection of line we got from query $$$1$$$ and query $$$3$$$ or on the intersection of line we got from query $$$2$$$ and query $$$3$$$.
We can make our next query on any of these intersections if we get the answer as $$$0$$$, then this point is the answer otherwise the other point is the answer.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll query(ll x, ll y) {
cout << "? " << x << ' ' << y << endl;
ll d;
cin >> d;
return d;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
ll t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll sum1 = query(1, 1) + 2;
ll sum2 = n + m - query(n, m);
ll dif1 = query(1, m) + 1 - m;
auto simplify = [&](ll num, ll lim)->ll{
return max(min(num, lim), 1LL);
};
ll x1 = simplify((sum1+dif1)/2, n), y1 = simplify((sum1-dif1)/2, m);
ll x2 = simplify((sum2+dif1)/2, n), y2 = simplify((sum2-dif1)/2, m);
if (query(x1, y1)) cout << "! " << x2 << ' ' << y2 << endl;
else cout << "! " << x1 << ' ' << y1 << endl;
}
return 0;
}
1934D1 — XOR Break — Solo Version
To generate any possible $$$m$$$, it takes at most two operations.
Let's determine the achievable values of $$$m$$$ for a given $$$n$$$.
If $$$n$$$ is a perfect power of $$$2$$$, then it cannot be broken down further, and no $$$m < n$$$ is achievable. Otherwise, if $$$n$$$ has at least two set bits, let's denote the most significant bit as $$$a$$$ and the second most significant bit as $$$b$$$.
Fact 1: All $$$m$$$ values less than $$$n$$$ are achievable if their most significant bit does not lie between $$$b+1$$$ and $$$a-1$$$.
Reason: For instance, $$$1001????$$$ can be decomposed into $$$1000????$$$ and $$$0001????$$$, or $$$1001????$$$ and $$$0000????$$$. In either case, we can never flip the $$$6^{th}$$$ or $$$7^{th}$$$ bit.
Using the above fact, we can break the problem into two cases:
Case 1: If the most significant bit of $$$m$$$ is at position $$$\leq b$$$.
Perform the first operation as $$$2^{b+1} - 1$$$ and $$$n \oplus (2^{b+1} - 1)$$$ ($$$10001????$$$ -> $$$10000????$$$ and $$$000011111$$$ in binary form). Then, any submask of $$$2^{b+1} - 1$$$ can be created in the next operation.
Case 2: If the most significant bit of $$$m$$$ is at position $$$a$$$.
If the most significant bit of $$$m$$$ is at position $$$a$$$, then $$$m$$$ can be obtained in one operation as $$$m$$$ and $$$m \oplus n$$$, since $$$m < n$$$ as given in the question, and $$$n \oplus m$$$ has its most significant bit $$$< a$$$.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
ll t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll hi = 0, sec_hi = 0;
for (ll p = (1LL<<62); p > 0; p >>= 1) {
if (p & n) {
if (!hi) hi = p;
else if (!sec_hi) sec_hi = p;
}
}
bool flag = (sec_hi && ((m & hi) || (m < sec_hi*2)));
if (!flag) {
cout << -1 << '\n';
continue;
}
vector<ll> ans = {n, m};
if (!(m & hi) && !(m & sec_hi)) ans = {n, m^sec_hi, m};
cout << (ll)ans.size()-1 << '\n';
for (auto &x: ans) cout << x << ' ';
cout << '\n';
}
return 0;
}
1934D2 — XOR Break — Game Version
If both $$$p_1$$$ and $$$p_2$$$ are perfect powers of $$$2$$$, it is a losing state since you cannot perform a break operation on either of those.
If either $$$p_1$$$ or $$$p_2$$$ has a bit count of $$$2$$$, then this is a winning state.
You can force your opponent into the state described in Hint $$$1$$$ using the number which has $$$2$$$ bitcount.
Fact 1: If $$$p_1$$$ has an odd bit count, then it can only be broken into two numbers such that one has an odd bit count and the other has an even bit count.
Fact 2: If either $$$p_1$$$ or $$$p_2$$$ has an even bit count, then this is a winning state.
Reason : If either $$$p_1$$$ or $$$p_2$$$ has an even bit count, without loss of generality, assume it's $$$p_1$$$. Then break it into $$$2^{msb \text{ of } p_1}$$$ and $$$p_1 \oplus 2^{msb \text{ of } p_1}$$$, where $$$msb$$$ is the most significant bit.
If the opponent chooses $$$2^{msb \text{ of } p_1}$$$, they instantly lose (using Hint $$$1$$$), so they are forced to choose the other number with an odd bit count. From Fact $$$1$$$, we can conclude that in the next turn, the state will remain conserved for the current player.
Because we eliminate the most significant bit in every query, this game will go on for at most $$$60$$$ turns for the player who reached this position first. At one point, the player who is at this state will have a number with two set bits.
Hence, from Hint $$$2$$$, we can say this player will win. So, as Alice, you will start first if $$$n$$$ has an even number of bits and start second if it has an odd number of bits. Proceed using the strategy discussed above. So as Alice you have will start first if $$$n$$$ has even number of bits and start second if it has odd number of bits. Any proceed using the strategy discussed above.
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int testcases;cin>>testcases;
for(int testcase=1;testcase<=testcases;testcase++){
long long n;cin>>n;
long long curr=n;
int parity=0;
if(__builtin_popcountll(n)%2){
cout<<"secoNd"<<endl;
}else{
cout<<"firSt"<<endl;
parity=1;
}
int turn=0;
while(1){
if(turn%2==parity){
long long p1,p2;cin>>p1>>p2;
if (p1 == 0 && p2 == 0)
break;
if(__builtin_popcountll(p1)%2==0){
curr=p1;
}else{
curr=p2;
}
}else{
int pos=0;
for(int i=0;(1ll<<i)<curr;i++){
if(curr&(1ll<<i)){
pos=i;
}
}
long long p1=(1ll<<pos);
long long p2=curr^p1;
cout<<p1<<" "<<p2<<endl;
curr = p1;
}
turn++;
}
}
}
Fact 1: If for $$$(x, y, z)$$$ their pairwise GCDs are equal to their common GCD (this means that $$$(x, y, z)$$$ = $$$(g * X, g * Y, g * Z)$$$ where $$$(X, Y, Z)$$$ are pairwise coprime), then making an operation on them (gives $$$(g * X * Y, g * X * Z,g * Y * Z)$$$) and looking at the subsequences of size EXACTLY 2, we find all three GCD: $$$x$$$, $$$y$$$, $$$z$$$. Let's call such tuple NICE.
Result: If we can split all values in the array into independent NICE tuples, then we can just perform an operation on each of them and the problem is solved.
Fact 2: We don't touch any value $$$\leq$$$ $$$\frac{n}{2}$$$. If there is $$$x$$$ $$$\leq$$$ $$$\frac{n}{2}$$$, then $$$2 * x \leq n$$$. If we don't touch $$$x$$$, then we will always have another value that is divisible by $$$x$$$ (it's easy to see that performing an operation on a multiple of $$$x$$$ leaves us with another multiple of $$$x$$$), so we will always have GCD equal to $$$x$$$ taking a subsequence $$$(x, x * A)$$$.
Fact 3: A sequence of consecutive integers $$$(x, x+1, x+2, ..., x+11)$$$ can be partitioned into $$$4$$$ disjoint sets of size $$$3$$$, each forming a NICE tuple, if $$$(x+11) \% 4$$$ equals $$$2$$$ or $$$1$$$.
For $$$(x+11) \% 4 = 2$$$: The sets $$$(x, x+1, x+2)$$$, $$$(x+4, x+5, x+6)$$$ and $$$(x+8, x+9, x+10)$$$ are NICE because, the first and third terms are always odd, and the second term is always even. The set $$$(x+3, x+7, x+11)$$$ is NICE because it has the form of $$$2*(2*n-1)$$$, $$$2*(2*n)$$$, $$$2*(2*n+1)$$$, ensuring that the pairwise GCDs are equal to the common GCD.
For $$$(x+11) \% 4 = 1$$$: The sets $$$(x, x+4, x+8)$$$, $$$(x+1, x+2, x+3)$$$, $$$(x+5, x+6, x+7)$$$ and $$$(x+9, x+10, x+11)$$$ are NICE, the same logic like $$$(x+11) \% 4 = 2$$$ follows,
If $$$n \% 4 = 3$$$ we can do one operation as $$$(1, 2, n)$$$, and if $$$n \% 4 = 0$$$ we can do one operation as $$$(1, n-1, n)$$$. Let's group the remaining elements into the groups of size $$$12$$$, starting from the end, and continuing until we reach the number $$$\frac{n}{2}$$$.
Eventually, we can count that we used no more than $$$\lfloor \frac{n}{6} \rfloor + 5$$$ operations.
Solutions for $$$n \leq 13$$$ should be found manually.
#include<bits/stdc++.h>
using namespace std;
vector<vector<vector<int>>> pans(14);
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr);
int t;cin>>t;
pans[3]={{1,2,3}};
pans[4]={{1,3,4}};
pans[5]={{3,4,5}};
pans[6]={{1,3,5},{2,4,6}};
pans[7]={{2,4,6},{3,5,7}};
pans[8]={{2,6,8},{3,5,7}};
pans[9]={{1,3,5},{2,4,6},{7,8,9}};
pans[10]={{3,4,5},{2,6,8},{7,9,10}};
pans[11]={{2,6,8},{3,5,7},{9,10,11}};
pans[12]={{1,11,12},{6,8,10},{5,7,9}};
pans[13]={{1,12,13},{7,9,11},{6,8,10}};
for(int tt=0;tt<t;tt++){
int n;cin>>n;
if(n<=2){
cout<<-1<<endl;
}else if(n<14){
cout<<pans[n].size()<<endl;
for(auto w: pans[n]){
cout<<w[0]<<" "<<w[1]<<" "<<w[2]<<endl;
}
}else{
vector<int> v(n);
for(int i=0;i<n;i++){
v[i]=i+1;
}
vector<vector<int>> ans;
while (2*v.size()>n){
if(v.size()%4==2){
vector<int> buf;
for(int i=0;i<12;i++){
buf.push_back(v.back());
v.pop_back();
}
reverse(buf.begin(),buf.end());
ans.push_back({buf[0],buf[1],buf[2]});
ans.push_back({buf[4],buf[5],buf[6]});
ans.push_back({buf[8],buf[9],buf[10]});
ans.push_back({buf[3],buf[7],buf[11]});
}else if(v.size()%4==1){
vector<int> buf;
for(int i=0;i<12;i++){
buf.push_back(v.back());
v.pop_back();
}
reverse(buf.begin(),buf.end());
ans.push_back({buf[1],buf[2],buf[3]});
ans.push_back({buf[5],buf[6],buf[7]});
ans.push_back({buf[9],buf[10],buf[11]});
ans.push_back({buf[0],buf[4],buf[8]});
}else if(v.size()%4==3){
vector<int> buf;
buf.push_back(v.back());
v.pop_back();
buf.push_back(2);
buf.push_back(1);
reverse(buf.begin(),buf.end());
ans.push_back({buf[0],buf[1],buf[2]});
}else{
vector<int> buf;
buf.push_back(v.back());
v.pop_back();
buf.push_back(v.back());
v.pop_back();
buf.push_back(1);
reverse(buf.begin(),buf.end());
ans.push_back({buf[0],buf[1],buf[2]});
}
}
cout<<ans.size()<<endl;
for(auto w: ans){
cout<<w[0]<<" "<<w[1]<<" "<<w[2]<<endl;
}
}
}
}
first so ez
Practicing Problem: C
Can you please explain Why my code gave Idleness Time limit exceeded: Submission: https://codeforces.net/contest/1934/submission/258202514
Idrk how C++ works, sorry brosef. Maybe someone else will be kind enough to help you. Are you properly flushing your output?
This is what I need to know....... :(
You should try to delete "#define endl '\n'".
Yeah, it worked after that. Thanks
Fast editorial!
WHY DOWNWOTE???
Thanks for the fast System test and fast editorial. It was a great contest and I liked problem C. I think every contest needs an interactive problem at least.
wtf! NO. It's cringe.
So you like a mathforces round?
Ironic enough I classify C as a math problem...
How often will interactive tasks appear now? (I just didn’t notice them during the rounds before)
Interactive problems are not much but in last two contests problem C was interactive.
English editorial pls ?
I just refreshed the page and the text changed from English to Russian. Please fix it,thanks.
UPD:It was fixed,thanks!
E: First time a problem need to hardcode from n=3 to n=13
Problem C seems to essentially be a duplicate of a problem which I wrote in a previous DMOJ round, but with one fewer step. This is probably just a coincidence though.
It must be unrated
Nah honestly if you can recall a 1.5 year old problem from memory then you deserve the points.
You would be laughing right before the round I was solving https://codeforces.net/problemset/problem/1797/C with all logic done, just fixing final bugs. Still I didn't solve C during the round. Although I think I would if hadn't switched to D.
I mean, there's a Div2 Edu E a while ago thzt's literally the hard copy of APIO22 P3, lol
or a problem from 5 years ago
whats the reason behind this?
If coins of value 1, 3, 6 and 15 were only present the greedy logic of selecting the higher valued first would work.
Added the proof. Is it written clearly?
Nice Explanation, Thanks!
This part is trying to prove the greedy method works properly, and I still getting it.
Another solution for B is to do greedy by picking the highest possible denomination each time, but finally decrease the above answer by 0,1 or 2 depending upon the value of n mod 15.
Can you please explain why this works ?
because a_max(highest value) is fixed such the remainders which are 1->14 and there cases
Noticed that when we try to acheive a big n, using as many 15s seems to be right. However, the initial greedy solution may be wrong because in some situations we can take a step back and get a smaller answer.There are only two such situations: when n%15==5(5=1+1+3,20=15+5=10+10) or n%15==8(8=1+1+6,23=15+8=10+10+3).
https://codeforces.net/contest/1934/submission/249187805 I am not getting why is it giving idleness limit exceeded verdict
because it's wrong
Then why the verdict is not "Wrong Answer"?
PS: downvotes for a doubt. Crazy!
cout.flush() ?
I gave you upvote.
The third test case of $$$D2$$$ really confused me. Why are we moving first, and why is Bob not splitting $$$10$$$ into $$$8$$$ and $$$2$$$, which should be optimal according to Bob?
Also, what is
classic classic
in the input?For $$$D-2$$$, you can also bruteforce/precalculate the states by checking every $$$n$$$ : $$$a$$$, $$$b$$$ transition since $$$n$$$ is atmost $$$60$$$
Problems from this contest made me realize how low my IQ is...
True, I found these thinking problems hard for me as well.
Y'alls IQ higher than mine!
E: Are there any cute approach for $$$n$$$ is small?
I used randomized brute force(and easily found a solution, 249183723 (commentout code)), but I'm looking for simpler approach.
This isn't exactly an answer to your question, but I came up with a solution that does not require brute force (but it did require a leap of faith for smaller cases):
The basic idea is exactly as in the editorial, we only care about $$$x > \frac{n}{2}$$$.
We will try to make tuples with these numbers starting from $$$\frac{n+2}{2}$$$, we will only consider consecutive numbers.
Let $$$(x_1, x_1 + 1, x_1 +2)$$$ be the tuple that we are considering in this moment:
If $$$x_1$$$ is odd, then we can apply the operation as in the editorial and continue with the next numbers.
If not, then one of $$$x_1$$$ and $$$x_1+2$$$ is not divisible by $$$4$$$. Let's assume that $$$x_1$$$ satisfies this property. Then we will apply the operation to the tuple $$$(\frac{x_1}{2}, x_1+1, x_1+2)$$$. Notice that $$$\frac{x_1}{2}$$$ was not modified by other operations until now. Now, we can prove that we can generate the values of $$$x_1$$$, $$$x_1+1$$$, $$$x_1+2$$$ and $$$\frac{x_1}{2}$$$ with the new values generated, and $$$x_1$$$, that was not used.
Finally, we have to consider the cases where we didn't handle the last numbers. If only two numbers were left, then the solution is equal to the editorial. But if one number is left, then we will use $$$(n, 1, \text{a number that is coprime with n and was not used})$$$
You have to prove that there will always exist such number, for a big $$$n$$$ is reasonable, but for a small $$$n$$$ I just had faith.
Another approach of B: 2 * max >= 3 * 2nd max i.e. 2 * 15 >= 3 * 10, which means if n >= 30, always better to take 15s until n < 30, then for residual count (max 29), dp can be used.
submission: https://codeforces.net/contest/1934/submission/249116555
Can someone please explain how the formula was derived in problem A? Like how is |a−b|+|b−c|+|c−d|+|d−a| equal to 2∗d−2∗a? I'll be able to understand the next two if I only knew how to do this :(
When the value in the mod is negative, we negate its contents while removing the mod.
So as stated $$$a \leq b \leq c \leq d$$$, when we open the mods it becomes $$$(-(a-b))+(-(b-c))+(-(c-d))+(d-a)$$$ which on simplifying gives $$$b-a-b+c-c+d+d-a$$$ which equals $$$2*d-2*a$$$
Oh ok I get it. Thanks a lot!
include<bits/stdc++.h> include using namespace std;
void minCoins() { int n; cin>>n; int coins[] = {15, 10, 6, 3, 1}; int min1=INT_MAX, numCoins = 0; for(int i=0;i<5;i++){ if((n%coins[i])==0) min1=min(min1,n/coins[i]); } for (int i = 0; i < 5; i++) { numCoins += n / coins[i]; n %= coins[i]; }
cout<<min(min1,numCoins)<<endl; }
int main() { int t; cin >> t; while (t--) { minCoins(); }
return 0; }
This code is giving result 9 for 98(15*6+6*1+1*2) which is indeed correct right! but in problem the answer for 98 is 8.
15 * 5 + 10 * 2 + 3
You would never need more than 2 6s, 18=15+3 better than 6+6+6 and 24=15+3+6 better than 6+6+6+6
I was having the same doubt, that why they gave 4 sixes in the eidtorial
If you write C like this:
query a dot(x1,y1)
analyze which dot to query next (using many if-else)
and query (x2,y2)
and analyze all possible conditions...(using many if-else)
and query (x3,y3) ....
your code will easily become over 100 lines and have a very complex logic and also hard to write & debug. That's what I did in the contest.....qwq.
Maybe I should learn more. How to solve this kind of problem efficiently? Thanks for any ideas.
can someone please tell what was the rating of Problem B???
just wait for a few days, als why do you need the rating of a problem anyways?
just curious nothing else
For question B, there seems to be a better solution. 249111275 I like this solution by Sugar_fan, because it's linear time per test case after a tiny amount of precalculation. Could Sugar_fan himself or someone who gets it please explain a little bit on the solution? About why taking the remainder between m and 2m works and 0 and m does not?
As for the value of m in his/her solution (= 2700), I intuitively expanded on this idea and figured that in general m = lcm(numbers) will also work. As such here is an accepted solution (I simply changed m from 2700 to 30 in the code): 249254380, with 0ms, signifying O(1).
I also use this solution as I'm desperate. If anyone know how this solution works and prove it please tell me!!!
We need to prove this: for every $$$n>=30$$$ , the optimal choose must have a 15s coin.
prove: Consider an optimal choose that don't have 15s coin.
it have no more than 2*10s coin (3*10s coin->2*15s coin)
10s coin will not appear with 6s coin (10+6 = 15+1 , the optimal choose can have 15)
it have no more than 2*6s coin (3*6s coin -> 1*3s coin + 1*15s coin)
it have no more than 1*3s coin (2*3s coin -> 1*6s coin)
it have no more than 2*1s coin (3*1s coin -> 1*3s coin)
So , this optimal choose have sum of
2*1s + 1*3s + 2*10s = 25 < 30 (don't have 6s coin)
OR 2*1s + 1*3s + 2*6s = 17 < 30 (don't have 10s coin)
So exactly if n>25 we can greedy choose 15s coin.
In fact , 25=10+15 , 24=15+6+3 , 23 = 10+10+3 (if have 15s coin,it will be 15+6+1+1) , so the maximum n that we can't do greedy is 23.
Thanks, got what's going on. So you do greedy till your remaining number is <= 23, then you add the pre-calculation of the remainder. Or I can say, it can be shown that [can it?] the lcm is always >= (the number till which greedy can't be applied) [remains to be proven], and thus following what is done in the code, the remainder will always be , rem >= lcm >= lowest non greedy number, and thus works. Do you have anything to add?
An easier solution to D1. We just want to work on first 2 setbits of number n. I claim that if the first set bit of n is also set in b then the solution is always possible in 1 operation. Consider the example:
n = 5, m = 1. their binaries will be
Here, we can always have a value of s lesser than n such that its XOR with n will be also be lesser than n. For instance we can use
s = 100
and our value of n will be transformed into m. Case 2(m has a setbit between the first 2 setbits of n) Consider n = 5 and m = 3 The binaries will be ~~~~~ n = 101 m = 011 ~~~~~ Here we will always have to choose the second bit of s as 1 in order to reach m but it will be contra to the second condition of s < n. So the solution isn't possible in the test case. Case 3(We have first 2 setbits of n and m in same position): In this case we can always make n = m in just 2 operations. lets consider the example. n = 21, m = 5; The binaries will be:n = 10101; m = 00101
I know that this test case can be solved with case 1 but for the sake of explanation I'm using it. It's short and can help in understanding this case a lot.
First, we can choose s = 100001. How and why? If we set the first bit of s and leave the position of second set bit of n in s then we will have the independence to choose the leftover bits as we like as anyways, s is always going to be smaller than n. Thus, we have cut the problem into a position where we only check the positions between first 2 set bits of n. Once we are done with that, we can choose any s such that leftover bit gets toggled. Here, for instance, we could choose s = 00100 in the second step and hence, n will be transformed into m. Hope this helps someone. I have this code which does this job in time complexity closer to constant.
And thanks to Shayan for coming up with this approach.
I think I've got pretty interesting solution for B. It was knapsack problem, but with cunning trick.
Let's remove all 15 from number N. I mean like, lets cnt = n/15, then n -= cnt*15. Then just do DP with vector a = {1, 3, 6, 10}. And the answer will be cnt + dp[n] (because n is less than 15 it will pretty quick).
But there's a catch. Sometimes it is better to not take last 15 coin. For example, 98. With our method, answer will be 9 (15*6 + 6*1 + 1*2). But the correct answer is 8 (15*5 + 10*2 + 3*1).
So we should write two dps, one for n -= (n/15)*15, and the other is for (n/15-1)*15, and then output the minimal one min(dp1[n] + cnt1, dp2[N] + cnt2).
Cool problems! Thanks! I loved them.
For the problem C, I queried (1,1), (1,m),(n,1) and (n,m).
Let the manhattan distances be r1,r2,r3,r4.
So as in the solution I guessed 2 possible values of (x1,y1).Let them be (x11,y11){assuming r2 is distance from (x1,y1)} and x12,y12{assuming r3 is distance from (x2,y2) and r2 is distance from x1,y1}. Now using r4 ,we know for sure that r4=n-x2+m-y2 since we assumed x1+y1=r1.
We calculate the possible values of (x2,y2) using r4 and r3 (assuming (x1,y1) is calculated using r1 and r2 i.e (x1,y1)=(x11,y11)). Then to check out of (x11,y11) and (x12,y12) which one is the correct (x1,y1) we apply the condition that
I think my logic is correct ,but still my code is giving WA.What am i doing wrong here?
Here is my piece of code for reference.
perhaps your x11 y11 x21 y21 y12 x12 are floats...I don't understand why you're using float.The mines coordirates are all integers.
I'm using float so that i can identify when is r1+r2-m and similarly others are divisible by 2 or not. If you see carefully I have put a check in the if condition that
x11-(ll)x11!=0
which checks if x11 is integer or not. So if x11 or y11 is a decimal then the answer is (x12, y12)you don't need to check if the point is divisible by 2 or not. just query this point and you will know. and if you really want to get the answer in 3 queries just check if r1+r2-m is divisible by two and continue using integers. float won't fit anyways, try long double.
For a moment lets leave the discussion on floats or long doubles. I know i could query this point and if I get 0 ,then its the correct point otherwise the other is the correct point. But still I want to know what is wrong in my method or logic.It seems correct that I query (n,m) and then get a r4 to find the other point (x2,y2) and then apply the conditions for assuming that r2 is distance from (x1,y1) and r3 is distance from (x2,y2).
Bro sent the code as a spoiler or as a link
I'll take care the next time.
You can edit the comment
Very interesting problems, many thanks to the setters!. Just wanted to share my thoughts on problem A, as I thought it might help someone cuz it took a while for me to understand it well enough (or) intuitively.
The problem can be viewed as choosing any 4 elements from the initial array, and placing them along a circle in any order. Except that, we also want this ordering to give us the maximum possible sum of the absolute differences of the adjacent elements in the circle.
If we assume that we have chosen some 4 elements (we'll see how to choose them optimally a bit later), we just have to find a permutation along this circle that maximizes our goal.
Now, intuitively we can see that, placing the top 2 maximal elements (or equivalently the top 2 minimal elements) diametrically opposite to each other is the most optimal way, that gives 2*((max1+max2)-(min1+min2)) as the answer.
To prove this Intuitive claim of ours, let's consider elements a<=b<=c<=d. We can see that there are only 3 possible permutations with unique pairs of adjacents. This is because :
There are only (4-1)! = 6 circular permutations (Rotations are considered the same, cuz the adjacents are the same).
Among them as well, we must consider the mirror images (left-right inversion) to be the same (cuz the adjacents are the same). Therefore, we divide by 2, giving us 6/2 = 3 permutations.
To view these 3 permutations, consider the elements a,b,c,d to be permuted along the circle. Fix the position of a, now once we fix it's opposite element, the adjacents get fixed automatically.
Therefore, we only have 3 perms. ( a-b opposite, a-c opposite, a-d opposite ).
We can evaluate the answer for these 3 perms, as it has been done in the Editorial, and pick the maximum, which will turn out to be 2*((d+c)-(a+b)), thereby proving our intuitive claims. ( max1=d , max2=c , min1=a, min2=b )
Thus, for the overall problem, we can just pick the maximum two and the minimum two elements from the entire array, as this choice maximizes 2*((max1+max2)-(min1+min2)).
I'm sorry if this turned out to be lengthy. Also, feel free to correct me if there's something wrong in this.
Thanks for Reading :)
For the problem D2, if the number p has an even bit count, can I break it into p1=2^(lsb of p) and p2=p1⊕p , where lsb means the least significant bit?
In the Code Part of Solution to Problem D2, on Alice's turn, why does the code set curr into p1? Why cannot Bob choose p2 instead of p1?
.
For your first question:
For example take the number 1111000 in your turn. With your logic, you get 1110000 and 0001000 and you print it. Obviously the system will choose 1110000.
But here since only the XOR of two split numbers needs to be equal to 1110000; the system could print 1011111 and 0101111 (both are less than 1110000, constraints of the problem are met) so the lsb we removed can come back in cases like these. therefore, the interaction is not guaranteed to end in 63 operations.
but when we remove msb, it's impossible to get that same bit back again and we can finish the interaction in less than or equal to 63 operations.
Well, I forgot the constraint that up to 63 operations can be executed. I guess that is why I got TLE when I use lsb strategy. Thank you very much for pointing it out! :)
np :)
Could somebody please explain why is this solution giving TLE for problem D2??
The given below is the link to my submission :- https://codeforces.net/contest/1934/submission/249353586
Use long long in your print function
Use lsb will exceed operations limitation. See this
For D2, can you explain while in third test for (n == 13) in a particular step shown in the sample bob didn't choose to break 10 -> 2 & 8 to win the game
Why my submission doesn't work (Problem C)? 249380440
My solution for B is a little interesting. https://codeforces.net/contest/1934/submission/249410271
n = 420
, its15 x 28
and10 x 42
, and42-28 = 14
14
less coins than other combinations, so even if the remainder is non-zero, it will be less than14
and we can use 1-value coin to fill the remainder, and it will still be optimal.10^9
to a number between420 and 435
by using only 15-value coins, then I rely on dp to tell me the minimal coins needed to consume the current number.Bro sent the code as a spoiler or as a link
B was similar to this spoj problem TPC07. Seems like others are picked from somewhere with minor conditions change only or is this a coincidence ?
For problem E, how does jury check whether the answer output by the contestant is correct? That is, how to write the Special Judge for this problem?
problem C sucks
another approach for B
Bro sent the code as a spoiler or as a link
Why exactly can't problem B be solve greedily. I came up with obvious cases: 20 = 10 + 10, 35 = 15 + 10, and so on. But why not?
for ex n=20: greedy chooses the greatest that's why the output is 4(15*1+3*1+1*2) but it must be 2(10*2)
This is My B number Code I think this is more easy than this code
include <bits/stdc++.h>
using namespace std; typedef long long int ll; int main() {
}
include<bits/stdc++.h>
using namespace std;
void rec(int in,vector &v,long long c,long long &ans,long long n) {
if(in>=v.size()) return;
} int main() { int t; cin>>t; while(t--) { long long n; cin >> n; long long ans =INT_MAX; vector v = {15, 10, 6, 3, 1}; int in = 0; for (in = 0; in < v.size(); in++) { if (v[in] <= n) break; } long long c=0; rec(in, v, c, ans, n);
why is this code not running for this test case 402931328
and giving the output
Exit code is -1073741571
Another solution for D1. Since we are allowed up to 63 operations, we can just do $$$\le1$$$ operation per bit. First let's assume there are at least 2 set bits in $$$n$$$.
For each bit $$$b$$$, if $$$b$$$ is the same in $$$m$$$ and $$$n$$$, then obviously we don't have to change it. Then we can break into two cases:
$$$1$$$: If $$$b$$$ is set in $$$n$$$ but not $$$m$$$, then we can add another operation flipping just that bit to $$$0$$$. Obviously, $$$b < n$$$ and $$$b\oplus n < n$$$, so this is valid.
$$$2$$$: If $$$b$$$ is set in $$$m$$$ but not $$$n$$$, then we can't have a separate operation, since then $$$b \oplus n > n$$$. Instead, let's combine this operation with an operation from case $$$1$$$, specifically the largest one. Note that since $$$m < n$$$, we will always encounter case $$$1$$$ before case $$$2$$$.
Now all we have to do is check if the largest operation is valid (because it may have been invalidated by case $$$2$$$ operations). There is an answer iff the largest operation is valid.
Problem: C
Can anyone tell me why my answer is giving the Idleness limit exceeded? What's wrong with my code?
my code: https://codeforces.net/contest/1934/submission/258188798
try remove line #define endl '\n'
more info — https://codeforces.net/blog/entry/63071
interactive problem for Problem C? That's not interesting.