CodeForces 652F question report

title meaning

Yes n An ant is on a ring of m grids in length. The grids on the ring are numbered counterclockwise, and each ant moves one grid per second in the direction it faces. If two ants collide, switch directions and ask the position of each ant after t seconds.

Problem solution

First of all, we can find out by observation

  • The trajectory produced by the collision of ants can be seen as two ants passing through each other
  • Since they only switch directions after a collision, the relative number of each ant will not change, that is, the “neighbors” of each ant will not change

In other words:

  • I can calculate the unobstructed trajectory of each ant to know where there are ants after t seconds.
  • Because of each ant’s The relative number remains the same. As long as I know the position of a certain ant in t seconds, I can calculate the position of all the remaining ants

The current question is converted from the first question to the first question. Two questions:

  • Where is the position of the xth ant after t seconds?
  • What is the number of the ant at the xth position after t seconds
  • ul>

    For the second question, we only require the sequence of ants sorted by position number after t seconds to calculate the answer. Assuming that the ants are arranged in the order of the size of the initial position number at the beginning, position 0 (the middle of position 1 and position m) is a critical point, consider two cases to find the order of ants after t seconds:

    • All ants did not collide within t seconds: When an ant walks from 1 to 0 to m, then this ant will become the last ant, and the second An ant will become the first one, and so on. Similarly, when an ant goes from 0 to 1 from m, this ant will become the first ant. Therefore, we only need to calculate how many times the ants have walked past 0 o’clock from right to left or left to right in t seconds, and the difference can get the order of the ants after t seconds.

    • An ant collides within t seconds: If an ant collides, because the ant’s trajectory can be seen as passing through each other, we can still Ignoring the collision situation to calculate the number of times the ant passes through the 0 point from two directions, it is regarded as the above situation. Every time we pass 0 o’clock from right to left, the sequence of ants moves to the left once, and the same goes to the right.

    For example, if the sequence of ant numbers is [1,3,2] at the beginning, in a certain second 2 passes from 0 to 1 from point m Point, the sequence becomes [2,1,3]. If 2 and 1 collide, 2 passes through 0 again in the reverse direction, and the arrangement order changes back to [1,3,2]. It seems that we need to judge that Ant 2 has passed 0 o’clock from different directions, but we can completely ignore the collision as Ant 2 passes 0 o’clock from the left and Ant 1 passes 0 o’clock from the right. It is also fully adapted to complex situations. .

    At this time we know:

    • There are ants in those positions after t seconds
    • The ants are sorted by position after t seconds

    We can directly map the positions of the ants one by one

    #includeusing namespace std;#define rep(i, a, b) for(int i=(a); i<(b); i++)#define per(i, a, b) for(int i=(a-1); i>=(b); i --)typedef long long ll;const int maxn = 300005;const int inf = 0x3f3f3f3f;struct A {ll pos; char f; bool operator<(const A &x) const {return pos > n >> m >> t; int offset = 0; rep(i, 0, n) {id[i] = i; cin >> a[i].pos >> a[i].f; a[i ].pos--;} //Sort to get the initial arrangement of ants sort(id, id + n, [&](int x, int y) {return a[x].pos 
    

Leave a Comment

Your email address will not be published.