Блог пользователя vovuh

Автор vovuh, история, 6 лет назад, перевод, По-русски

1311A - Add Odd or Subtract Even

Идея: vovuh

Разбор
Решение

1311B - WeirdSort

Идея: MikeMirzayanov

Разбор
Решение (n^2)
Решение (n log n)

1311C - Perform the Combo

Идея: vovuh

Разбор
Решение

1311D - Three Integers

Идея: MikeMirzayanov

Разбор
Решение

1311E - Construct the Binary Tree

Идея: MikeMirzayanov

Разбор
Решение

1311F - Moving Points

Идея: vovuh

Разбор
Решение (Fenwick tree)
Решение (pbds)
Разбор задач Codeforces Round 624 (Div. 3)
  • Проголосовать: нравится
  • +31
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +42 Проголосовать: не нравится

B works in O(n), no sort needed. Code

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +24 Проголосовать: не нравится

If we use 2 pointers in E, complexity will be O(n). https://mirror.codeforces.com/contest/1311/submission/71823186

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Can you explain your approach ? Thank advance ^_^!!

    • »
      »
      »
      6 лет назад, скрыть # ^ |
      Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      Statement 1: There is no exist the tree with sum of depths of all vertices more than unary tree.

      Statement 2: There is no exist the tree with sum of depths of all vertices less than balanced tree.

      Let's create unary tree for default. Sum of depth for this tree — is the sum of ariphmetic progression. We must decrease it to the d. Fix the deepest vertex. Fix the top vertex of the tree. If we can't attacth the lower vertex to the higher because the sum of depth will be smaller than d, it means that we must deepen the upper vertex. And as we go from top to bottom the difference on every step decreases exactly 1. It means that there isn't exist the better solution.

      Add to these the condition that every layer must be not greater than 2 size of previous layer and this will be a solution.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

F can be solved without pbds and fenwick tree, look at this submission.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +8 Проголосовать: не нравится

    I explained some reasoning behind this solution but I should have posted it here: this is it:

    First there is a trick to sum all $$$x_j-x_i$$$ for $$$i \lt j$$$ in a sorted array $$$x[]$$$ in linear time: Errichto wrote some blogs calling it the contribution technique.

    Now in our problem say $$$x=[100,50,75,25]$$$, and the corresponding sorted $$$v=[1,2,3,4]$$$. First we sort $$$x=[25,50,75,100]$$$. Then we can sum $$$x_j-x_i$$$ using the above technique. Look at the ways 75 gets added:

    $$$+75, +75, -75$$$

    (from 75-50, 75-25, and 100-75)

    Now let's iterate through $x$ in sorted $$$v$$$ order: when we get to the third element 75, we see that for our answer we should only have a single

    $$$+75$$$

    term due to the

    $$$75 \gt 50$$$

    ; we only want $x_j-x_i$ where $$$v_j \gt v_i$$$. Coincidentally this equals the net sum of the three terms above. The reason is that if $$$75$$$ has the same rank in both the sorted $$$x$$$ and the sorted $$$v$$$ versions, then there is a symmetry in how $$$[25,50,75,100]$$$ mutates into $$$[100,50,75,25]$$$: we can let other elements "jump over" 75 to anywhere on the other side, and the symmetry is that the number of jumps going right over 75 equals the number of jumps going left: 100 jumped left, but then 25 jumped right. So the rank of 75 is equal in both cases, and we don't need to correct the $$$+75,+75,-75$$$ in our final answer.

    Another case is $$$25$$$, which is rank 1 with respect to $$$x$$$, but rank 4 with respect to $$$v$$$: From the contribution method, we had

    $$$ -25, -25, -25 $$$

    due to 50-25, 75-25, and 100-25. But because the rank changed from 1 (w.r.t $$$x$$$) to 4 (w.r.t $$$v$$$), we must have had 3 jumping overs to the left: and every such jumping over means we shouldn't have included a $$$-25$$$, so we correct with $$$+25$$$ 3 times, and end up with $$$0\cdot 25$$$ in the final answer.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    I managed to solve F using a modified version of Merge Sort and prefix sums. Code.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    why after changing vector<pair<int,int> >vec(n); into vector<pair<int,int> >vec; then it will get a Runtime Error? Is the data type "pair" responsible for this? Thanks.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

How to solve F if there is integer t or positive integer t restriction.could we reduce any one restriction to nlogn time or some optimal time.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

On problem D, I understood why do we iterate A to maximum 2a but what is the reason behind checking all multiples of A to maximum 2b? Also if we don't check for C = (c / B) * B + B, can we get the true answer?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Another solution for D: Let's precompute divisors and multiples (up to $$$2 \times 10^4$$$) for all numbers up to the $$$10^4$$$. Let's then fix a value of $$$B$$$, then $$$A$$$ has to be a divisor and $$$C$$$ a multiple. We find such $$$A$$$ closest to $$$a$$$ and $$$C$$$ closest to $$$c$$$ in $$$O(log n)$$$, compute the number of operations for that particular $$$B$$$, and repeat this for all possible $$$B$$$'s. Total complexity $$$O(n log n)$$$

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

WoW, thanks for the tutorial.

In problem B, thought if I approach find a -> find b -> find c. I have to iterator for (all possible a), for (all possible b), (find c), which run in O(q * a * b) then get TLE. But now I learn more that I just need to go for (all possible a), for (mutiples a), (find c). O(q * a log a)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -7 Проголосовать: не нравится

IN D why editorial code O((n^2)*10^2) is working even after a<=10^4 and b <=10^4 ?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Does anyone uses brute force algorithm like me in problem D?XD

#define n (20000)
int a,b,c;cin>>a>>b>>c;
int ans=9999999,_a=0,_b=0,_c=0;
for(int i=1;i<=n;++i) for(int j=1;j<=n/i;++j) for(int k=1;k<=n/i/j;++k) 
{
	int now=abs(i-a)+abs(i*j-b)+abs(i*j*k-c);
	if(now<ans) _a=i,_b=i*j,_c=i*j*k,ans=now;
}
cout<<ans<<endl<<_a<<' '<<_b<<' '<<_c<<endl;

I heard that a lot of people were hacked because they set the limit only 10000 XD

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

On problem D, I understand why we iterate A to maximum 2a but what is the reason behind checking all multiples of A to maximum 2b?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone please tell me what is wrong in my solution for problem B :

#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << " : " << x << endl;
 
bool cmp(pair<int, int> a, pair<int, int> b){
			return a.first < b.first;
}
 
int main() {
	const int N = 105;
	int t;
	cin >> t;
	while (t-->0) {
		int n, m;
		cin >> n >> m;
		vector <int> b(N);
		vector <pair<int, int> > a(n);
		for (int i=0; i<n; i++) {
			cin >> a[i].first;
			a[i].second = i;
		} 
		sort(a.begin(), a.end(), cmp);
		for (int i=0; i<m; i++) {
			int val;
			cin >> val;
			b[val]++;
		} 
		bool flag = true;
		for (int i=0; i<n; i++) {
			int pos = a[i].second;
			// deb(i);
			// deb(pos);
			// deb(a[i].first);
			for (int j=min(pos, i); j < max(pos, i); j++) {
				if (b[j + 1] == 0) {
					flag = false;
				}
			}
		}
		if (flag) cout << "YES\n";
		else cout << "NO\n";
	}
}
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

" A cannot be bigger than 2a, " Proof please. (Problem D)

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Let's say A is bigger than 2a, then in order to get A from a, we need A — a > a add operations. No matter what B we pick, we can always use a — 1 subtract operations to get A = 1 from a, which always meet the requirement B % A = 0. So going over 2a always yields a worse answer than simply decrease a all the way down to 1.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For D I have different approach. I brute force C up to 2c. We know that number of divisors for numbers up to 20000 maximum is 60. So, if you prebuild all divisors for all numbers up to 20000, then for each C you can try each B (divisor of C) and for each B try each A (divisor of B). So each test is done roughly in 2c * 60 * 60 tries.

I didn't prove that C is not exceeding 2c. Is there proof? Or, perhaps hack? 71792331

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone tell me why this submission got WA 71787302. and this AC 71788262. why could defining arrays in main is wrong?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -7 Проголосовать: не нравится

In Problem B you can try the logic of Bubble sort.. 71787966

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

Any good source to learn pbds?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

I made B, by using dfs

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -23 Проголосовать: не нравится

For D -> The Upper Bound of B and C should be mentioned in the problem statement.

»
6 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится -8 Проголосовать: не нравится

Why the complexity of the solution of Problem E in this tutorial is $$$O(nd)$$$. The while loop from n*(n-1)/2 to the lower bound that approximately equal nlogn. So I suspect it's complex is $$$O(n^3)$$$. (n<700) And I copy the solution written at the tutorial, and add a variant to count the time complex as below:

code

It is my input :

1
650 5000

It is my output:

4837
268511100
YES
1 2 3 4 5 6 7 8 9 ...

It seems that the complex is bigger than $$$O(nd)$$$. 650*5000=3250000 and it iters 268511100 times.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +2 Проголосовать: не нравится

Another solution for B is use DSU. We will define x and x + 1 is an edge, then we merge them . Then we sort the array A with storing the original position. We will consider all elements of A ( after sorting ), Call POS[] is a array to store the original pos, If Pos[i] < Pos[i + 1] we continue the loop, else we check if they have the same root , if not the answer is NO and we return, else the answer is YES. My solution : https://mirror.codeforces.com/contest/1311/submission/71846993

»
6 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится -17 Проголосовать: не нравится

I can't figure out which is the editorial, the blog or the comments? :D

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

vovuh For problem F, I am not familiar with the fenwick tree coordinates compression technique. Can you elaborate a bit more on this?

The following statement in your editorial probably has something to do with this technique? add our current point to the Fenwick trees (add 1 to the position Vi in the first tree and Xi to the position Vi in the second tree).

I have some knowledge on fenwick tree but not so much with the coordinates compression technique. I assume it is needed because the input can be in range[1, 10^8] which would consume too much memory, right?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

More clear explanation for sentence in D editorial "Then let's iterate over all possible multiples of A from 1 to 2b. Let this number be B." While we want to choose B if we choose it bigger than 2b this mean we need to have at least (b+1) operations. Except this if we just equalize the B to A at most we do b operations. Because highest value of |b-A| can be (2a-b) or (b-1) and these are smaller or equal to b because a<=b. So that we can't choose B upper than 2b.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

is there a proof for solution to E?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I am getting the error for testcase4 in Problem B. Please help me my code

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится -9 Проголосовать: не нравится

It seems like for D, any reasonably tight bounds will work: here is the one I came up with (after the contest...) and a proof:

  • $$$a$$$ ranges from $$$i = 1$$$ to $$$2c$$$;
  • $$$b$$$ equals $$$ji$$$ where $$$j$$$ ranges from $$$1$$$ to $$$ji\leq 2c$$$

Complexity: $$$O(c\log c)$$$ by counting all pairs $$$(i,j)$$$ with $$$ij\leq 2c$$$ (use harmonic series).

Proof: $$$a$$$ will never be above $$$2c$$$: because instead of taking $$$a$$$ to $$$ \gt 2c$$$, $$$b$$$ to $$$ \gt 2c$$$, and $$$c$$$ to $$$ \gt 2c$$$, we could simply take $$$a$$$ and $$$b$$$ to $$$c$$$.

And for the same reason, $$$b$$$ will never be above $$$2c$$$; because the number of moves in raising $$$b$$$ to $$$ \gt 2c$$$, and in raising $$$c$$$ to $$$ \gt 2c$$$, already exceeds the number of moves in making both $$$a$$$ and $$$b$$$ equal to $$$c$$$: $$$(c-a)+ (c-b) \leq 2c$$$.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem E, what is the logic behind calculating lower bound ld?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For E, Can anyone explain me why the time complexity is O(nd)?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why is this incorrect for fourth question (D).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

In F , what if the points start moving simultaneously at time t=0. how to solve then?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain why we need to consider only upto 2*a in problem D?

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

Could anyone share the simplest way you've figured out to build that tree(in E)? Thanks in advance.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why in problem D the harmonic series is estimated as log n? (I would like to see the proof)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem B solution using disjoint sets

include<bits/stdc++.h>

using namespace std;

int giveParent(int x,int *parent) { if(parent[x] == -1)return x; else{ return giveParent(parent[x],parent); } }

void solve() { int n,m; cin>>n>>m;

int a[n+1],b[n+1];
for(int i=1;i<=n;i++){
       cin>>a[i];
       b[i] = a[i];
}


int parent[n+1];
memset(parent,-1,sizeof(parent));
while(m--)
{
    int x;
    cin>>x;
    parent[x+1] = x;
}

sort(b+1,b+n+1);
bool visited[n+1];
memset(visited,false,sizeof(visited));
for(int i=1;i<=n;i++)
{
    if(b[i] ==  a[i])
    {
           visited[i]=true;
           continue;
    }
    else{
        int j;
        for( j=1;j<=n;j++)
        {
            if(b[j] ==  a[i] && visited[j] == false)
            {
                if(giveParent(i,parent) == giveParent(j,parent)){
                    visited[j] == true;
                    break;
                }

            }
        }

        if(j==n+1)
                {
                    cout<<"NO"<<endl;
                    return;
                }
    }
}

cout<<"YES"<<endl;

}

int main() { int T; cin>>T; while(T--) solve(); }

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem F,while updating and querying Fenwick tree we generally isolate the last bit and the update our loop control variable as x+=(x&-x) and x-=(x&-x).However,here we are using different updating statements in the functions(pos=(pos & (pos + 1)) — 1) and (pos |= pos + 1).Can someone please explain me this.I am new to Fenwick trees. Thank you!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in problem A how we are geting only 1 & 2 as output .can any one please explain in easy way

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    if a == b then obviously answer is 0,
    if a < b then we have to add a number to get from a to b, but we can add only odd numbers which is good if a is odd and we want to go to even number (o+o=e), or if a is even and we need to go to odd numbers (e+o=o), other wise we can subtract 1 from a to get to one of the above mentioned stage,
    do the same reasoning for a > b..

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In the fourth question, we can enumerate a, B and C. the upper bound is 2 * C, which can also be crossed.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone tell me why I am seeing O(n^2) and O(n^3) answers getting accepted in probD, here n = 10^4, how is that even possible, isn`t 10^8 too much to pass through 2 seconds limit.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

https://mirror.codeforces.com/contest/1311/submission/76479163

Firstly, I supposed the tree as complete binary tree and used the depth sum as initial sum. I checked that if I can move any vertex down from it's original depth and if I could then did it and incremented the sum till it becomes d. I thought it would be a tle as we are running two loops. Why it isn't.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

Can someone explain me how the time complexity of the problem D is O(nlogn) ?

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    For each value of $$$A$$$ from $$$1$$$ to $$$2a$$$, you iterate over all possible multiples of $$$A$$$ from $$$1$$$ to $$$2b$$$. So the number of iterations is:

    $$$\sum_{A=1}^{2a} \lfloor \frac{2b}{A} \rfloor \leq {2b} \sum_{A=1}^{2a} \frac{1}{A} \leq {2b} \cdot (\log_2 2a + 1) = O(n \log n) $$$

    according to harmonic series

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

2

1 4

2 0

How is it coincide??

Anyone help please

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Im practicing DSU and just found out that the problem B can be solved by DSU, here's my solution: 196218629, but I wonder about the time complexity of this code

»
3 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

F was so incredible