zoukankan      html  css  js  c++  java
  • <Codeforces Beta Round #38> D. Vasya the Architect

    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 nlines 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.

    Sample test(s)
    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
    

    这是一个数学题。判断一个brick能否保持平衡的方法是看它的重心在xoy平面上的投影是否落在它下方的那一个brick上(包括边界)。故此这题涉及计算重心坐标的问题,对于单个brick,计算重心坐标是一个显而易见的问题,假设对角顶点在xoy上的投影的坐标分别是(x1,y1),(x2,y2),则重心在xoy上的投影的坐标就是两者的中点((x1 + x2)/2,(y1 + y2)/2)。那么,多个brick组成的复合体的重心怎么求?运用以下公式:

    Pw = (P[1] * W[1] + P[2]*W[2] + ... + P[n] * W[n]) / (W[1] + W[2] + ... + W[n])
    比如要求y,则Pw是复合体的重心的纵坐标,p[i]是复合体中brick[i]的重心纵坐标,w[i]是brick[i]的重量。

    AC Code:

    //Memory: 1400 KB		Time: 30 MS
    //Language: GNU C++ 4.6		Result: Accepted
    
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    
    struct BRICK
    {
        int x1, x2, y1, y2; //对角坐标
        double wei; //重量
        double cenx, ceny; //重心坐标
    }b[101];
    
    int main()
    {
        int n, x1, x2, y1, y2;
        int i, j, k;
        double w, cx, cy; //分别是总的重量,重心横坐标、纵坐标
        scanf("%d", &n);
        for(i = 1; i <= n; i++)
        {
            scanf("%d %d %d %d", &b[i].x1, &b[i].y1, &b[i].x2, &b[i].y2);
            b[i].wei = pow(((b[i].x1 - b[i].x2)*(b[i].x1 - b[i].x2) + (b[i].y1 - b[i].y2)*(b[i].y1 - b[i].y2))/2.0, 1.5);
            b[i].cenx = 0.5*(b[i].x1 + b[i].x2);
            b[i].ceny = 0.5*(b[i].y1 + b[i].y2);
        }
        for(i = 2; i <= n; i++)
        {
            cx = b[i].cenx, cy = b[i].ceny, w = b[i].wei;
            //往下搜看加上b[i]后会不会使得下面的brick不稳
            for(j = i - 1; j > 0; j--)
            {
                if((cx - b[j].x1)*(cx - b[j].x2) > 0 || (cy - b[j].y1)*(cy - b[j].y2) > 0)
                {
                    break;
                }
                //计算新的重心和重量
                cx = (w*cx + b[j].wei*b[j].cenx)/(w + b[j].wei);
                cy = (w*cy + b[j].wei*b[j].ceny)/(w + b[j].wei);
                w += b[j].wei;
            }
            if(j) break;
        }
        printf("%d\n", i - 1);
        return 0;
    }


  • 相关阅读:
    关于callback
    vue项目前端限制页面长时间未操作超时退出到登录页
    vue 项目文件流数据格式转blob图片预览展示
    You are using the runtime-only build of Vue where the template compiler is not available.
    element-ui el-cascader级联选择器设置指定层级不能选中
    vue项目中图片预览旋转功能
    nhandled rejection Error: EPERM: operation not permitted, open 'C:Program Files odejs ode_cache npm ERR! cb() never called!
    vue+element-ui upload图片上传前大小超过4m,自动压缩到指定大小,长宽
    vue+element-ui 项目中实现复制文字链接功能
    vue项目js实现图片放大镜功能
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910570.html
Copyright © 2011-2022 走看看