zoukankan      html  css  js  c++  java
  • POJ 2991 Crane

    Crane
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 8238   Accepted: 2217   Special Judge

    Description

    ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of various lengths, connected by flexible joints. The end of the i-th segment is joined to the beginning of the i + 1-th one, for 1 ≤ i < n. The beginning of the first segment is fixed at point with coordinates (0, 0) and its end at point with coordinates (0, w), where w is the length of the first segment. All of the segments lie always in one plane, and the joints allow arbitrary rotation in that plane. After series of unpleasant accidents, it was decided that software that controls the crane must contain a piece of code that constantly checks the position of the end of crane, and stops the crane if a collision should happen.

    Your task is to write a part of this software that determines the position of the end of the n-th segment after each command. The state of the crane is determined by the angles between consecutive segments. Initially, all of the angles are straight, i.e., 180o. The operator issues commands that change the angle in exactly one joint.

    Input

    The input consists of several instances, separated by single empty lines.

    The first line of each instance consists of two integers 1 ≤ n ≤10 000 and c 0 separated by a single space -- the number of segments of the crane and the number of commands. The second line consists of n integers l1,..., ln (1 li 100) separated by single spaces. The length of the i-th segment of the crane is li. The following c lines specify the commands of the operator. Each line describing the command consists of two integers s and a (1 ≤ s < n, 0 ≤ a ≤ 359) separated by a single space -- the order to change the angle between the s-th and the s + 1-th segment to a degrees (the angle is measured counterclockwise from the s-th to the s + 1-th segment).

    Output

    The output for each instance consists of c lines. The i-th of the lines consists of two rational numbers x and y separated by a single space -- the coordinates of the end of the n-th segment after the i-th command, rounded to two digits after the decimal point.

    The outputs for each two consecutive instances must be separated by a single empty line.

    Sample Input

    2 1
    10 5
    1 90
    
    3 2
    5 5 5
    1 270
    2 90

    Sample Output

    5.00 10.00
    
    -10.00 5.00
    -5.00 10.00

    题意:给你n段线段,初始时是竖直的,有C次修改,每次修改使得线段Si和Si+1之间的角度变成A[i]度,每次修改后输出最后那条线段的坐标,初始时为(0,0)
    题解:用线段树进行维护    1.第一条线段的起点指向最后一条线段的终点的向量
                   2.每个节点的对应部分链接后,右儿子需要转动的角度
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn = 1e5+5;
    #define M_PI acos(-1.0)
    int N,C;
    int L[maxn];
    int S[maxn],A[maxn];
    double vx[maxn],vy[maxn];
    double ang[maxn];
    double pre[maxn];
    void init(int k,int l,int r){
        ang[k]=vx[k]=0.0;
        if(r-l==1) vy[k]=L[l];
        else{
            int chl=k*2+1;
            int chr=k*2+2;
            init(chl,l,(l+r)/2);
            init(chr,(l+r)/2,r);
            vy[k]=vy[chl]+vy[chr];
        }
    }
    void update(int s,double a,int v,int l,int r){
        if(s<=l) return;
        else if(s<r){
            int chl=v*2+1;
            int chr=v*2+2;
            int m=(l+r)/2;
            update(s,a,chl,l,m);
            update(s,a,chr,m,r);
            if(s<=m){
                ang[v]+=a;
            }
            double s=sin(ang[v]),c=cos(ang[v]);
            vx[v]=vx[chl]+(c*vx[chr]-s*vy[chr]);
            vy[v]=vy[chl]+(s*vx[chr]+c*vy[chr]);
        }
    }
    void solve(){
        init(0,0,N);
        for(int i=1;i<N;i++){
            pre[i]=M_PI;
        }
        for(int i=0;i<C;i++){
            int s=S[i];
            double a=A[i]/360.0*2*M_PI;
            update(s,a-pre[s],0,0,N);
            pre[s]=a;
            printf("%.2f %.2f
    ",vx[0],vy[0]);
        }
    }
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        while(cin>>N>>C){
        memset(L,0,sizeof(L));
        memset(S,0,sizeof(S));
        memset(A,0,sizeof(A));
        for(int i=0;i<N;i++){
            cin>>L[i];
        }
        for(int i=0;i<C;i++){
            cin>>S[i]>>A[i];
        }
            solve();
        }
        return 0;
    }
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    chrome书签插件
    Js箭头函数和lambda
    CSS水平或垂直居中技巧
    前端需要注意的SEO优化
    OpenCV图像识别初探-50行代码教机器玩2D游戏
    机器学习笔记(十一)----降维
    基于Docker搭建分布式消息队列Kafka
    一个经典面试题:如何保证缓存与数据库的双写一致性?
    Flask 蓝图机制及应用
    软件开发团队如何管理琐碎、突发性任务
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9363633.html
Copyright © 2011-2022 走看看