Codeforces Round #492 (Div. 2) C. Tesla


Link

http://codeforces.com/contest/996/problem/C

answer

My English is not good, I misunderstood the meaning of the question at first, so I wrote 3 K The code results in non-stop W A , I later learned that a kind of garage can only allow the corresponding cars to enter.
The correct solution is this, you let the car keep spinning, and if anyone arrives at their door, they go in.
The conditions for no solution are that there are no vacancies at the beginning and no car is in front of the house.
One of the things that is hard to debug here is when rotating, if you always use it mechanically it's the same every time f O r loop, then it is possible to make every time 0 all come back to the same place, I think I could write a piece of code that looks for each time 0 position, and then let it keep swapping positions with adjacent cars (along a fixed direction), and each time 0 The stop position is not the same as last time.
Think about it if 0 returns to the same position every time, then this 0 It is equivalent to occupying this position. If a car must enter the garage from this position, it will fall into an infinite loop.
I ended up using randomization and every move has 50 % The probability of cancellation is guaranteed, so as to ensure that it will not be stuck by extreme data.
The above is an algorithm based on making all the cars turn around and then checking whether they can enter the garage. If you use every time you move a car to check whether it has come to your door, you don't need to consider the above problem

code

#include <cstdio>
#include <algorithm>
#include <cstdio>
#define maxn 100
#define rep(x) for(int i=1;i<=x;i++)
#define rd (rand()&1)
using namespace std;
int now[10][maxn], dx[maxn], dy[maxn], n, k, q[200010][3], head, tail, res;
void push(int a, int b, int c)
{
    q[tail][0]=a, q[tail][1]=b, q[tail][2]=c;
    tail++;
}
void up(int x, int y)
{
    push(now[x][y],x-1,y);
    swap(now[x][y],now[x-1][y]);
}
void down(int x, int y)
{
    push(now[x][y],x+1,y);
    swap(now[x][y],now[x+1][y]);
}
void left(int x, int y)
{
    push(now[x][y],x,y-1);
    swap(now[x][y],now[x][y-1]);
}
void right(int x, int y)
{
    push(now[x][y],x,y+1);
    swap(now[x][y],now[x][y+1]);
}
void park()
{
    int i, t;
    for(i=1;i<=n;i++)
    {
        t=now[2][i];
        if(i==dy[t] and dx[t]==1)up(2,i), res--;
    }
    for(i=1;i<=n;i++)
    {
        t=now[3][i];
        if(i==dy[t] and dx[t]==4)down(3,i), res--;
    }
}
void init()
{
    int i, j;
    scanf("%d%d",&n,&k);res=k;
    for(i=1;i<=4;i++)for(j=1;j<=n;j++)scanf("%d",now[i]+j);
    for(i=1;i<=4;i+=3)for(j=1;j<=n;j++)if(now[i][j])dx[now[i][j]]=i, dy[now[i][j]]=j, now[i][j]=0;
    park();
}
void spin()
{
    int i;
    for(i=n-1;i;i--)if(now[2][i] and now[2][i+1]==0 and rd)right(2,i);
    if(now[3][1] and now[2][1]==0 and rd)up(3,1);
    for(i=2;i<=n;i++)if(now[3][i] and now[3][i-1]==0 and rd)left(3,i);
    if(now[2][n] and now[3][n]==0 and rd)down(2,n);
}
void show()
{
    for(printf("%d\n",tail-head);head^tail;head++)printf("%d %d %d\n",q[head][0],q[head][1],q[head][2]);
}
int main()
{
    int i;
    init();
    for(i=1;i<=n;i++)if(now[2][i]==0 or now[3][i]==0)break;
    if(i>n){printf("-1");return 0;}
    while(res)spin(), park();
    show();
    return 0;
}

Related


Codeforces#236 Div2

Anurag Sharma We call n vertices an undirected graph of interest to p if: The graph contains exactly 2n + p edges; The graph does not contain self-loops and multiple edges; For any integer k (1≤k≤n), any subgraph consisting of k vertices contains at most 2k+p

Codeforces#236 Div2

Anurag Sharma We call an undirected graph of interest to n vertices p if: The graph contains exactly 2n + p edges; The graph does not contain self-loops and multiple edges; For any integer k (1≤k≤n), any subgraph consisting of k vertices contains at most 2k+p

Codeforces#236 Div2

Anurag Sharma We call an undirected graph of interest to n vertices p if: The graph contains exactly 2n + p edges; The graph does not contain self-loops and multiple edges; For any integer k (1≤k≤n), any subgraph consisting of k vertices contains at most 2k+p

Select range in contenteditable div (round 2)

username as a track to the selection range in the content editable div Stackoverflow doesn't allow me to comment on Tim's "answer", but I just tried Tims code into a jsfiddle, but can't see it working on Chrome or Edge right now, maybe it works on older browse

Select range in contenteditable div (round 2)

username as a track to the selection range in the content editable div Stackoverflow doesn't allow me to comment on Tim's "answer", but I just tried Tims code into a jsfiddle, but can't see it working on Chrome or Edge right now, maybe it works on older browse

Select range in contenteditable div (round 2)

username as a track to the selection range in the content editable div Stackoverflow doesn't allow me to comment on Tim's "answer", but I just tried Tims code into a jsfiddle, but can't see it working on Chrome or Edge right now, maybe it works on older browse

Select range in contenteditable div (round 2)

username as a track to the selection range in the content editable div Stackoverflow doesn't allow me to comment on Tim's "answer", but I just tried Tims code into a jsfiddle, but can't see it working on Chrome or Edge right now, maybe it works on older browse

Select range in contenteditable div (round 2)

username as a track to the selection range in the content editable div Stackoverflow doesn't allow me to comment on Tim's "answer", but I just tried Tims code into a jsfiddle, but can't see it working on Chrome or Edge right now, maybe it works on older browse

Select range in contenteditable div (round 2)

username as a track to the selection range in the content editable div Stackoverflow doesn't allow me to comment on Tim's "answer", but I just tried Tims code into a jsfiddle, but can't see it working on Chrome or Edge right now, maybe it works on older browse

Select range in contenteditable div (round 2)

username as a track to the selection range in the content editable div Stackoverflow doesn't allow me to comment on Tim's "answer", but I just tried Tims code into a jsfiddle, but can't see it working on Chrome or Edge right now, maybe it works on older browse

Read numeric value from div element and round to 2 decimal places

spontaneous I want to retrieve a numeric value from a div and then use jQuery to round it to two decimal places. So far I have element names and values: <div class="value">Price = £133.3223443</div> I am using the toFixed() method to convert a number to a str

Read numeric value from div element and round to 2 decimal places

spontaneous I want to retrieve a numeric value from a div and then use jQuery to round it to two decimal places. So far I have element names and values: <div class="value">Price = £133.3223443</div> I am using the toFixed() method to convert a number to a str

round with c

Roberto Rocco I'm trying to convert hours and minutes from a time since midnight (recorded in an array). But I see there is a rounding error. For example, departure time 583 (minutes to midnight), calculate the time and minutes as follows: 583/60 = 9.716666 I

round with c

Roberto Rocco I'm trying to convert hours and minutes from a time since midnight (recorded in an array). But I see there is a rounding error. For example, departure time 583 (minutes to midnight), calculate the time and minutes as follows: 583/60 = 9.716666 I

round with c

Roberto Rocco I'm trying to convert hours and minutes from a time since midnight (recorded in an array). But I see there is a rounding error. For example, departure time 583 (minutes to midnight), calculate the time and minutes as follows: 583/60 = 9.716666 I

round with c

Roberto Rocco I'm trying to convert hours and minutes from a time since midnight (recorded in an array). But I see there is a rounding error. For example, departure time 583 (minutes to midnight), calculate the time and minutes as follows: 583/60 = 9.716666 I

round with c

Roberto Rocco I'm trying to convert hours and minutes from a time since midnight (recorded in an array). But I see there is a rounding error. For example, departure time 583 (minutes to midnight), calculate the time and minutes as follows: 583/60 = 9.716666 I

round div balloon

Down This is the CSS for my current image round balloon .circle-image{ width: 64px; height: 64px; border-radius: 50%; background-size: contain; background-position: center; background-image: url("/assets/img/dashboard/img-stdn.png");

Round hole in <div>

username I'm looking for a way to create a relatively sized square div, e.g. set the background color to 70% x 70% and have a circular hole in it (so that the background can be seen behind it) so that the div's Tangent on both sides. Weafs.py You can radial-gr

round div balloon

Down This is the CSS for my current image round balloon .circle-image{ width: 64px; height: 64px; border-radius: 50%; background-size: contain; background-position: center; background-image: url("/assets/img/dashboard/img-stdn.png");

round div balloon

Down This is the CSS for my current image round balloon .circle-image{ width: 64px; height: 64px; border-radius: 50%; background-size: contain; background-position: center; background-image: url("/assets/img/dashboard/img-stdn.png");

Round hole in <div>

username I'm looking for a way to create a relatively sized square div, e.g. set the background color to 70% x 70% and have a circular hole in it (so that the background can be seen behind it) so that the div's Tangent on both sides. Weafs.py You can radial-gr