CF 38 D Vasya the Architect【递推+重心公式】

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
2
0 0 3 3
1 0 4 3
Output
2
Input
2
0 0 3 3
2 0 5 3
Output
1
Input
3
0 0 3 3
1 0 4 3
2 0 5 3
Output
3

题意:给你几个立方体,问你最多可以叠多高。这就是一个重心稳定的问题,每个立方体质地均匀,重心即几何中心。从下向上按照题目给定的顺序,一层一层的向上累加,如果某一层崩了就输出最高层数。
注意,新加进来的立方体不仅需要考虑最上面一层的稳定结构,还需要考虑它对于下面每一层的影响。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 105;
#define LL long long
const double eps=1e-8;
struct node{
    int x1,y1,x2,y2;
    double a,b;
    double m;
}p[maxn];
node q[maxn];
double x,y,M;
void js(double a1,double b1,double a2,double b2,double m1,double m2)
{
    double m=m2/(m1+m2);
    x=a1+(a2-a1)*m;
    y=b1+(b2-b1)*m;
    M=m1+m2;

}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
        if(p[i].x1>p[i].x2)
        {
            swap(p[i].x1,p[i].x2);
        }
        if(p[i].y1>p[i].y2) swap(p[i].y1,p[i].y2);
        p[i].m=1.0*abs(p[i].x2-p[i].x1)*abs(p[i].x2-p[i].x1)*abs(p[i].x2-p[i].x1);
        p[i].a=1.0*(p[i].x1+p[i].x2)/2;
        p[i].b=1.0*(p[i].y1+p[i].y2)/2;
    }
    bool flag=0;int cs=0;
    for(int i=2;i<=n;i++)
    {
        if(flag) break;
        M=p[i].m;x=p[i].a;y=p[i].b;
        if(x<p[i-1].x1||x>p[i-1].x2||y<p[i-1].y1||y>p[i-1].y2){

            flag = 1;
            cs = i-1;
            break;
        }
        for(int j=i-1;j>=2;j--)
        {
            js(x,y,p[j].a,p[j].b,M,p[j].m);
            if(x<p[j-1].x1||x>p[j-1].x2||y<p[j-1].y1||y>p[j-1].y2){
                flag = 1;
                cs = i-1;
                break;
            }
        }
    }
    if(flag){
        printf("%d\n",cs);
    }
    else{
        printf("%d\n",n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值