Codeforces Problem - 38D - Vasya the Architect(模拟)

D. Vasya the Architect
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart.

Let us consider the building process. Vasya takes a brick and puts it on top of the already built tower so that the sides of the brick are parallel to the sides of the bricks he has already used. Let's introduce a Cartesian coordinate system on the horizontal plane, where Vasya puts the first brick. Then the projection of brick number i on the plane is a square with sides parallel to the axes of coordinates with opposite corners in points (xi, 1, yi, 1) and (xi, 2, yi, 2). The bricks are cast from homogeneous plastic and the weight of a brick a × a × a is a3 grams.

It is guaranteed that Vasya puts any brick except the first one on the previous one, that is the area of intersection of the upper side of the previous brick and the lower side of the next brick is always positive.

We (Vasya included) live in a normal world where the laws of physical statics work. And that is why, perhaps, if we put yet another brick, the tower will collapse under its own weight. Vasya puts the cubes consecutively one on top of the other until at least one cube loses the balance and falls down. If it happens, Vasya gets upset and stops the construction. Print the number of bricks in the maximal stable tower, that is the maximal number m satisfying the condition that all the towers consisting of bricks 1, 2, ..., k for every integer k from 1 to m remain stable.

Input

The first input file contains an integer n (1 ≤ n ≤ 100) which is the number of bricks. Each of the next n lines contains four numbers xi, 1, yi, 1, xi, 2, yi, 2 (xi, 1 ≠ xi, 2, |xi, 1 - xi, 2| = |yi, 1 - yi, 2|) which are the coordinates of the opposite angles of the base of the brick number i. The coordinates are integers and their absolute value does not exceed 50

The cubes are given in the order Vasya puts them. It is guaranteed that the area of intersection of the upper side of the brick number i - 1 and the lower side of the brick number i is strictly strictly greater than zero for all i ≥ 2.

Output

Print the number of bricks in the maximal stable tower.

Examples
input
Copy
2
0 0 3 3
1 0 4 3
output
2
input
Copy
2
0 0 3 3
2 0 5 3
output
1
input
Copy
3
0 0 3 3
1 0 4 3
2 0 5 3
output
3

题意:

堆积木,问能堆几块

每块积木放置的位置都已经确定,每块积木都是长宽高相等的立方体,题目给出每个方块的俯视图斜对角坐标

积木的质量为 边长^3

堆积木的顺序就是输入的积木顺序

有一块堆不上去后面的就不再继续堆了



每次往上堆积木都要不断的向下判断重心是否在积木上

如:放置第i个积木,先判断第i个积木的重心是否在i-1个积木上,如果在,则将第i个积木和第i-1个积木看作一个整体,得到新的重心,判断该重心是否在i-2个积木上,不断判断,直到第一块积木,若中间有一次不再积木上则不能再向上堆积木了。


重心的合并可以利用公式

x = x1 + (x2 - x1) * m2 / (m1 + m2) = (x1 * m1 + x2 * m2) / (m1+m2);
y = y1 + (y2 - y1) * m2 / (m1 + m2) = (y1 * m1 + y2 * m2) / (m1+m2);

多个物体的重心公式

x = ∑(xi * mi) / ∑mi

y = ∑(yi * mi) / ∑mi


#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
const int N = 1e2 + 10;
struct node{
    double x1,x2,y1,y2; //落地坐标
    double x,y;         //重心坐标
    double m;           //质量
    node(int a,int b,int c,int d){
        x1 = a; y1 = b; x2 = c; y2 = d;
        if(x1>x2) swap(x1,x2);
        if(y1>y2) swap(y1,y2);//将对脚坐标方便变为左下和右上角坐标
        x = (x1+x2) / 2;
        y = (y1+y2) / 2;
        m = y2-y1; m = m * m * m;
    }
    node(){};
}b[N];
int n;
double x1,x2,y1,y2;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        b[i] = node(x1, y1, x2, y2);
    }
    for(int i=2;i<=n;i++){
        double x = b[i].x, y = b[i].y, m = b[i].m;
        for(int j=i-1;j>=1;j--){
            if(x<b[j].x1||x>b[j].x2||y<b[j].y1||y>b[j].y2){
                //判断重心是否在下面的积木上
                printf("%d\n", i-1);
                return 0;
            }
            x += (b[j].x - x) * b[j].m / (m + b[j].m);
            y += (b[j].y - y) * b[j].m / (m + b[j].m);
            m = m + b[j].m;
            //合并积木
        }
    }
    printf("%d\n", n);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/w326159487/article/details/79825497