思维/数学(upc组队赛 Appalling Architecture)

Appalling Architecture

时间限制: 1 Sec 内存限制: 128 MB

题目描述

You have recently been hired as an architect for the BAPC (Bureau of Architecture and Promising Constructions), responsible for top-quality buildings such as the Tower of Pisa. However, in the past couple of weeks, some of the constructions that the BAPC has made have collapsed! It is up to you to figure out whether any other constructions are in danger.
After some research it seems like the x-coordinate of the center of gravity of some of the constructions is off: if this is too much to the left or to the right, the construction will fall over. Hence, you decide to check all the blueprints and see whether the constructions are stable or not.
Given is an up to 100 by 100 grid of characters in .#/_|-. The . characters denote empty space, while each other character represents a completely filled 1 × 1 box (any difference in symbols used is due to the artistic freedom of the other architects), whose center of mass is at the center of the box.
Every construction forms a single connected component that touches the ground, i.e. the bottom layer of the grid.
The construction falls to the left if the x-coordinate of the center of gravity is less than the x-coordinate of the leftmost point of the construction that touches the ground, and it falls to the right if the x-coordinate of the center of gravity is larger than the x-coordinate of the rightmost point of the construction that touches the ground. It is guaranteed that the center of gravity is never exactly above the leftmost or rightmost point where the building touches the ground.
Given a blueprint, is the construction balanced, does it fall to the left, or does it fall to the right?

输入

The first line has 1 ≤ h ≤ 100 and 1 ≤ w ≤ 100, the height and width of the grid.
Then follow h lines with l characters each. Each character is either ., indicating empty space, or one of #/_|-, indicating a filled 1 × 1 box .

输出

Print a single line containing left, balanced, or right.

样例输入

3 3
/-
|.|
#.#

样例输出

balanced

有公式
平面的重心的x坐标为

x = ∬ μ ( x , y ) x d σ ∬ μ ( x , y ) d σ x =\frac{\iint \mu (x,y)x d\sigma}{\iint \mu(x,y)d\sigma} x=μ(x,y)dσμ(x,y)xdσ

这里, μ ( x , y ) \mu(x,y) μ(x,y) 是密度函数,不过这里我们认为密度恒定为1。则:

x = ∬ x d σ ∬ d σ x =\frac{\iint x d\sigma}{\iint d\sigma} x=dσxdσ

对于这个题,我们可以看做:

x = ∑ i = 1 n x − 0.5 n x = \frac{\sum_{i=1}^n x-0.5}{n} x=ni=1nx0.5

为啥要减0.5?——因为一个横坐标为i的点代表的是区间[i-1,i]我们因为是线性的,取平均值(i+i-1)/2。
另外,最左边的点的坐标是i-1不是i。
下面是ac代码:

#include
#include
#include
#define eps 1e-8
using namespace std;
 
int h,w;
double ans,x,s,l,r;
char mp[105][105];
 
int main(){
    scanf("%d%d",&h,&w);
    for(int i=1;i<=h;i++)
        scanf("%s",mp[i]+1);
    double t=0.0;
    x=0.0;
    s=0.0;
    for(int i=1;i<=h;i++){
        if(i==h){
            for(int j=1;j<=w;j++)
                if(mp[i][j]!='.'){
                    l=1.0*j - 1.0;
                    break;
                }
            for(int j=w;j>=1;j--)
                if(mp[i][j]!='.'){
                    r=1.0*j;
                    break;
                }
        }
        for(int j=1;j<=w;j++){
            if(mp[i][j]=='.') continue;
            s+=1.0;
            x+=1.0*j - 0.5;
        }
    }
    t=x/s;//cout<
    if(t<l) printf("left");
    if(t>r) printf("right");
    if(t>l-eps&&t<r+eps) printf("balanced");
    return 0;
}
/*
5 7
///.//.
///..//
///.//.
....//.
/...//.
3 5
///..
////.
.././
*/

你可能感兴趣的:(做不出的思维)