Codeforces Gym 101190 (NEERC 2016) J. Jenga Boom

meaning of the title

The length, width and height are cuboids of w, n w and 1 respectively. Each layer is placed with n geometries forming a square bottom of n w * n w * 1.

Each layer alternately increases in the shape of a wellbore, with a total of h layers.

Facing an edge of a geometry, numbering n cuboids from near to far is 1, 2, 3, 3, and ___________

Now we give m l k in turn to denote the order of idx and remove the K cuboid of layer L.

How many operations will cause the geometry to collapse?

Solving problems

Because the operation sequence is given and the order is related, the simulation of the operation is considered. For each operation, the center of gravity of the top layer is judged whether it falls within the stable range of the h-top-1 layer.

The stability range can be considered as one-dimensional coordinates of the whole geometry of the layer [minimum number-0.5, maximum number+0.5] falling on [0.5, n+0.5].

The center of gravity can be simply regarded as the number of remaining blocks in the front top layer and the total number of remaining blocks in the front top layer.

Among them, because the layers are placed alternately in the shape of a well. Therefore, if a cube is removed from the odd layer, the even layer should be treated specially.

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n, w, h, m;
bool flr[2][2501][10001];
int lft[2][2501], rgt[2][2501];
int cet[2][2501], num[2][2501];
int cmp(double x) {
    if(abs(x) < 1e-8)    return 0;
    return x > 0 ? 1 : -1;
}
bool jug(double center, double left, double right) {
    if(cmp(center - left) <= 0 || cmp(center - right) >= 0)
        return false;
    return true;
}
int main()
{
    freopen("jenga.in", "r", stdin);
    freopen("jenga.out", "w", stdout);
    scanf("%d %d %d %d", &n, &w, &h, &m);
    memset(flr, true, sizeof(flr));
    long long lmt = (1+n) * n / 2;
    for(int i=1;i<=h;i++)
    {
        cet[i%2][(i+1)/2] = lmt;
        num[i%2][(i+1)/2] = n;
        lft[i%2][(i+1)/2] = 1;
        rgt[i%2][(i+1)/2] = n;
    }
    for(int idx=1, l, k;idx<=m;idx++)
    {
        scanf("%d %d", &l, &k);
        bool flg = l % 2;
        l = (l+1)/2;
        flr[flg][l][k] = 0;
        cet[flg][l] -= k;
        num[flg][l]--;
        for(int i=1;i<=n;i++)
            if(flr[flg][l][i] == true) {
                lft[flg][l] = i;
                break;
            }
        for(int i=n;i;i--)
            if(flr[flg][l][i] == true) {
                rgt[flg][l] = i;
                break;
            }
        double tCet = 0, tNum = 0;
        for(int i=h;i;i--)
        {
            if(cmp(tNum) == 1 && flg == i%2) {
                double center = tCet * 1.0 / tNum;
                if(jug(center, lft[flg][(i+1)/2] - 0.5, rgt[flg][(i+1)/2] + 0.5) == false) {
                    printf("yes\n%d\n", idx);
                    return 0;
                }
            }
            if(i%2 == flg)
                tCet += cet[flg][(i+1)/2],
                tNum += num[flg][(i+1)/2];
            else
                tCet += num[!flg][(i+1)/2] * (1.0+n)/2.0,
                tNum += num[!flg][(i+1)/2];
        }
    }
    printf("no\n");
}

Posted by clem_c_rock on Sat, 09 Feb 2019 17:21:17 -0800