【27.22%】【poj2991】Crane
| Time Limit: 2000MS | Memory Limit: 65536K | |||
| Total Submissions: 5772 | Accepted: 1571 | Special Judge | ||
Description
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 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 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
Source
【题解】
题意就是说把n条线段从下往上竖直堆积。然后有时候会给你几个操作。要求把第s个的上面那个棍子(s+1)转到一定的角度。(以第s个棍子作为衡量角度的参照物);
这题我们不要管它转到多少度。而只需要知道它转的这个角度和前一个角度的差是多少就可以了。
每次遇到一个角度就减去180度。
然后用减去180度的角和之前它的角相减。就能得到角度差了。为什么减去180度等下会讲到。
然后我们把这n条线段看成一个个向量。那么从头加到尾。就能够得到最上面那条线段的顶点坐标了。(向量加法);
这样看来。我们只要维护这n条线段的向量和就可以了。
然后其中一个向量m它的角度转了x度,则m+1,m+2..n它们的向量也都会转x度。
而一个向量砖了x度之后。我们是能够求出它新的向量坐标形式的。
x' = cosa*x-sina*y;
y' = sina*x+cosa*y;
(a是旋转的角度);
推导过程看下面.
推导的基础就是一条线段它转了之后,长度是不会变的!
然后注意这是逆时针转的情况。
所以我们一开始减去180度。
比如样例90,减去180就变成-90了
然后套用这个公式就变成逆时针旋转(-90)也就是顺时针转90度了。
而这正是我们所需的。(可以带进样例试试。)
注意减去180度之后,要重新记录k+1的角度,方便下次对k+1操作。(输入的是k,但是我们实际在对k+1进行操作!);
然后我们在做线段树的时候对k+1,k+2...n进行相同的旋转操作即可。
然后维护一下区间的向量和。
//然后一开始的区间向量和的x坐标都是0,y坐标的和就用前缀和的方法获得。
具体的过程看代码;
ps:突然发现如果define里面比如define MAXN 10000+10 ,交上去后会runtime error。。。就是define里面貌似不能写表达式。。以后都用const吧。const 即不会有事。。
acos(-1.0)==pi
里面的点号不能漏,不然它会以为你输入的是整形然后会Compile error
【代码】
#include <cstdio>
#include <cmath>
#define lson begin,m,rt<<1
#define rson m+1,end,rt<<1|1 const int MAXN = 10000 + 100;
const double pi = acos(-1.0); int n, c;
int a[MAXN];
double sum[MAXN * 4][2];
int lazy_tag[MAXN * 4];
int pre[MAXN]; void build(int begin, int end, int rt)//建树
{
lazy_tag[rt] = 0;
sum[rt][1] = a[end] - a[begin - 1];//一开始建树的时候就能处理区间和了
sum[rt][0] = 0;
if (begin == end)
return;
int m = (begin + end) >> 1;
build(lson);
build(rson);
} void input_data()
{
a[0] = 0;
for (int i = 1; i <= n; i++)
{
int len;
scanf("%d", &len);
a[i] = a[i - 1] + len;//获取前缀和,用来处理一开始的初始区间和
pre[i] = 0;//这是第i个棍子的初始角度置0就可以。
}
build(1, n, 1);
} double get_rad(int k) //获取角度k的弧度制。
{
double temp = (k * pi)/180;
return temp;
} void rotation(int rt, double rad) //根据坐标和选择角度更改这个向量
{
//y = sina*x+cosa*y
//x = cosa*x-sina*y
double dx = sum[rt][0], dy = sum[rt][1];
sum[rt][0] = cos(rad)*dx - sin(rad)*dy;
sum[rt][1] = sin(rad)*dx + cos(rad)*dy;
} void push_up(int rt)
{
sum[rt][0] = sum[rt << 1][0] + sum[rt << 1 | 1][0];
sum[rt][1] = sum[rt << 1][1] + sum[rt << 1 | 1][1];
} void push_down(int rt)//传递懒惰标记
{
if (lazy_tag[rt] != 0)
{
double temp = get_rad(lazy_tag[rt]);
rotation(rt << 1, temp);
rotation(rt << 1 | 1, temp); //把儿子们旋转
lazy_tag[rt << 1] += lazy_tag[rt];
lazy_tag[rt << 1 | 1] += lazy_tag[rt];
lazy_tag[rt] = 0;
}
} void updata(int k, int change, int begin, int end, int rt)
{
if (begin == end)
{
double hudu = get_rad(change);
rotation(begin,hudu);//只有一个点 那就是要旋转的了。
return;
}
push_down(rt);
int m = (begin + end) >> 1;
if (k <= m + 1) //这个意思是说右儿子全是大于等于k的节点。那么就要全部旋转
{
double hudu = get_rad(change);
if (k <= m)//左区间可能还有部分需要旋转
updata(k, change, lson);
rotation(rt << 1 | 1, hudu);//因为要全部旋转所以就用懒惰标记。
lazy_tag[rt << 1 | 1] += change;
}
else
if (k > m + 1)//k>m+1的话左边就全是小于k的节点了(不用管)。只要处理右边就好
updata(k, change, rson);
push_up(rt);
} void get_ans()
{
for (int i = 1; i <= c; i++)
{
int number, degree;
scanf("%d%d", &number, °ree);
number++;//要处理的是number+1
int d = degree - 180;
int change = d - pre[number];
pre[number] = d;
updata(number, change,1, n, 1);
printf("%.2lf %.2lf\n", sum[1][0], sum[1][1]);
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
//freopen("F:\\rush_out.txt", "w", stdout);
bool flag = false;
while (scanf("%d%d", &n, &c) == 2)
{
if (flag)
printf("\n");
input_data();
get_ans();
flag = true;
}
return 0;
}
【27.22%】【poj2991】Crane的更多相关文章
- JAVA 基础编程练习题22 【程序 22 递归求阶乘】
22 [程序 22 递归求阶乘] 题目:利用递归方法求 5!. 程序分析:递归公式:fn=fn_1*4! package cskaoyan; public class cskaoyan22 { @or ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 1333:【例2-2】Blah数集
1333:[例2-2]Blah数集 注意是数组,答案数组中不能有重复数字 q数组是存储答案的 代码: #include<iostream> #include<cstdio> # ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 【UOJ#22】【UR #1】外星人(动态规划)
[UOJ#22][UR #1]外星人(动态规划) 题面 UOJ 题解 一道简单题? 不难发现只有按照从大往小排序的顺序选择的才有意义,否则先选择一个小数再去模一个大数是没有意义的. 设\(f[i][j ...
- Selenium3自动化测试【27】Frame的操作
本篇文章内容摘要 " 讲解Python3+Selenium3如何处理Frame窗体" 同步视频知识与系列知识内容,可关注:[公众号]:柒哥测试:[WX]:Lee-890;[视频号] ...
- 【ASP.NET程序员福利】打造一款人见人爱的ORM(二)
上一篇我已经给大家介绍AntORM的框架[ASP.NET程序员福利]打造一款人见人爱的ORM(一),今天就来着重介绍一下如何使用这套框架 1>AntORM 所有成员 如果你只想操作一种数据库,可 ...
- 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound
[什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...
- 【iScroll源码学习04】分离IScroll核心
前言 最近几天我们前前后后基本将iScroll源码学的七七八八了,文章中未涉及的各位就要自己去看了 1. [iScroll源码学习03]iScroll事件机制与滚动条的实现 2. [iScroll源码 ...
随机推荐
- 洛谷 P2347 砝码称重
P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...
- Cocos2d-x学习笔记(三)CCNode分析
原创文章.转载请注明出处:http://blog.csdn.net/sfh366958228/article/details/38706483 通过前两份学习笔记,我们不难发现CCScene.CCLa ...
- Java 学习(17): Java 泛型
Java 泛型 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说将 ...
- uva 1463 - Largest Empty Circle on a Segment(二分+三分+几何)
题目链接:uva 1463 - Largest Empty Circle on a Segment 二分半径,对于每一个半径,用三分求出线段到线段的最短距离,依据最短距离能够确定当前R下每条线段在[0 ...
- amazeui-datatables(登录注册界面用到)
amazeui-datatables(登录注册界面用到) 一.总结 amazeui-datatables:DataTables 插件 Amaze UI 集成,只修改了样式和默认显示语言,其他参数同官方 ...
- Day1:注释
一.注释方法 1.单行注释用#,本行#号后的内容为注释内容,不执行 2.多行用三个单引号或三个双引号标注,中间内容为注释,不执行 二.其他相关内容 三个引号中的内容还可以当作字符串赋值给变量,可以同时 ...
- GO语言学习(九)Go 语言运算符
运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 接下来让我们来详细看看各个运算符的介绍. 算术运算符 下表 ...
- Maven学习总结(17)——深入理解maven灵活的构建
一个优秀的构建系统必须足够灵活,应该能够让项目在不同的环境下都能成功构建.maven为了支持构建的灵活性,内置了三大特性,即:属性.profile和资源过滤. 1.maven属性 maven属性分6类 ...
- [React] Style the body element with styled-components and "injectGlobal"
In this lesson, we see how we can apply styles globally with the "injectGlobal" helper met ...
- OC学习篇之---第一个程序HelloWorld
从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这里最好还是Java).在干活之前,得先找到 ...