CodeForces 47E. Cannon(离线暴力+数学)
3 seconds
256 megabytes
standard input
standard output
Bertown is under siege! The attackers have blocked all the ways out and their cannon is bombarding the city. Fortunately, Berland intelligence managed to intercept the enemies' shooting plan. Let's introduce the Cartesian system of coordinates, the origin of which coincides with the cannon's position, the Ox axis is directed rightwards in the city's direction, the Oy axis is directed upwards (to the sky). The cannon will make n more shots. The cannon balls' initial speeds are the same in all the shots and are equal to V, so that every shot is characterized by only one number alphai which represents the angle at which the cannon fires. Due to the cannon's technical peculiarities this angle does not exceed 45 angles (π / 4). We disregard the cannon sizes and consider the firing made from the point (0, 0).
The balls fly according to the known physical laws of a body thrown towards the horizon at an angle:
vx(t) = V·cos(alpha)vy(t) = V·sin(alpha) – g·tx(t) = V·cos(alpha)·ty(t) = V·sin(alpha)·t – g·t2 / 2
Think of the acceleration of gravity g as equal to 9.8.
Bertown defends m walls. The i-th wall is represented as a vertical segment (xi, 0) - (xi, yi). When a ball hits a wall, it gets stuck in it and doesn't fly on. If a ball doesn't hit any wall it falls on the ground (y = 0) and stops. If the ball exactly hits the point (xi, yi), it is considered stuck.
Your task is to find for each ball the coordinates of the point where it will be located in the end.
The first line contains integers n and V (1 ≤ n ≤ 104, 1 ≤ V ≤ 1000) which represent the number of shots and the initial speed of every ball. The second line contains n space-separated real numbers alphai (0 < alphai < π / 4) which represent the angles in radians at which the cannon will fire. The third line contains integer m (1 ≤ m ≤ 105) which represents the number of walls. Then follow m lines, each containing two real numbers xi and yi (1 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000) which represent the wall’s coordinates. All the real numbers have no more than 4 decimal digits. The walls may partially overlap or even coincide.
Print n lines containing two real numbers each — calculate for every ball the coordinates of its landing point. Your answer should have the relative or absolute error less than 10 - 4.
2 10
0.7853
0.3
3
5.0 5.0
4.0 2.4
6.0 1.9
5.000000000 2.549499369
4.000000000 0.378324889
2 10
0.7853
0.3
2
4.0 2.4
6.0 1.9
10.204081436 0.000000000
4.000000000 0.378324889 题意:
有一门大炮,坐标在(0,0)(0,0),和mm堵墙,现在大炮要射nn发炮弹,每发炮弹的初始速度v是一样的,射击角度为α(0<α<π/4),假设射击后经过时间t,重力加速度g=9.8,则有:
x(t)=v∗cos(α)
y(t)=v∗sin(α)−g∗t
x(t)=vx(t)∗t
y(t)=v∗sin(α)∗t−g∗t2/2
给定m堵墙墙顶坐标(xi,yi),墙垂直于xx坐标轴,炮弹如果打到墙上,就会卡住;如果掉到地上,也不会再滚动。
求这n发炮弹最终的位置
----translate by 守望、copy from 洛谷
题解:显然如果速度相同,角度在45度以内,那么角度越大的射的越高越远,所以如果矮的能越过的墙高的也能越过,把问题离线下来按照角度排序,模拟每个球会怎么走就可以了,显然每堵墙只会被访问一遍,均摊复杂度O(1),总复杂度O(n)
代码如下:
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct bomb
{
int id;
double a,ansx,ansy;
} b[]; struct wall
{
double x,y;
} w[]; int n,m;
double v; int cmp1(wall x,wall y)
{
if(x.x==y.x) return x.y<y.y;
return x.x<y.x;
} int cmp2(bomb x,bomb y)
{
return x.a<y.a;
} int cmp3(bomb x,bomb y)
{
return x.id<y.id;
} int main()
{
double g=9.8;
scanf("%d %lf",&n,&v);
for(int i=; i<=n; i++)
{
scanf("%lf",&b[i].a);
b[i].id=i;
}
scanf("%d",&m);
for(int i=; i<=m; i++)
{
scanf("%lf%lf",&w[i].x,&w[i].y);
}
sort(w+,w+m+,cmp1);
sort(b+,b+n+,cmp2);
int r=;
for(;; r++)
{
if(r>m)
{
r=m+;
break;
}
double t=w[r].x/(cos(b[].a)*v);
if(v*sin(b[].a)*t-g*t*t/<=w[r].y)
{
break;
}
}
for(int i=; i<=n; i++)
{
while()
{
if(r>m)
{
r=m+;
break;
}
double t=w[r].x/(cos(b[i].a)*v);
if(v*sin(b[i].a)*t-g*t*t/>=w[r].y) r++;
else break;
}
double t=w[r].x/(cos(b[i].a)*v);
if(r<=m)
{
b[i].ansy=(v*sin(b[i].a)*t)-(g*t*t/);
b[i].ansx=w[r].x;
}
else
{
b[i].ansy=;
b[i].ansx=(v*sin(b[i].a)/g)*v*cos(b[i].a)*;
}
}
sort(b+,b+n+,cmp3);
for(int i=; i<=n; i++)
{
if(b[i].ansy<)
{
b[i].ansy=;
b[i].ansx=(v*sin(b[i].a)/g)*v*cos(b[i].a)*;
}
printf("%.9lf %.9lf\n",b[i].ansx,b[i].ansy);
}
}
CodeForces 47E. Cannon(离线暴力+数学)的更多相关文章
- Codeforces Beta Round #4 (Div. 2 Only) A. Watermelon【暴力/数学/只有偶数才能分解为两个偶数】
time limit per test 1 second memory limit per test 64 megabytes input standard input output standard ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- Codeforces 1087B Div Times Mod(数学+暴力)
题意: 求(x div k) * (x mod k) = n的最小解x,保证有解 1<=n<=1e6, k<=1000,1s 思路: 注意到k的范围是1e3, 1<=x mod ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
- Codeforces 789A Anastasia and pebbles(数学,思维题)
A. Anastasia and pebbles time limit per test:1 second memory limit per test:256 megabytes input:stan ...
- B. Apple Tree 暴力 + 数学
http://codeforces.com/problemset/problem/348/B 注意到如果顶点的数值确定了,那么它分下去的个数也就确定了,那么可以暴力枚举顶点的数值. 顶点的数值是和LC ...
- Codeforces Little Dima and Equation 数学题解
B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces A. Playlist(暴力剪枝)
题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- 主从DNS服务器的搭建
一.DNS主从的理解 主从服务器,在一开始的理解中,以为是主的dns服务器挂掉后,(dns服务自动转向辅助dns服务器),客户端还能继续解析.事实貌似不是这样的.当我把主dns停掉的时候,客户端只设一 ...
- 使用java获取自己的机器网卡
package org.ibase4j.core.util; import java.io.BufferedReader;import java.io.IOException;import java. ...
- Cisco动态路由 OSPF协议
OSPF描述: 组播扩展OSPF 锁定 同义词 ospf一般指组播扩展OSPF 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . OSPF(Open Shortest Path Firs ...
- bat执行sqlplus语句,省去@xx.sql过程
bat文件中执行写sqlplus连接,再@调用自己,sql登录成功后,会忽略掉第一行sqlplus xxx,转而执行下方的sql语句 --------------------------------- ...
- Quest *nix Xwindows
国内一般网站搜到的linux系统添加Xwindows都是无法完成的,至少我在Ct6.3上不行,也许是yum源配置不同问题 我举个站点大家自己上去看,本人就不多说了,多说也无益,. http://yao ...
- 呕心沥血Android studio使用JNI实例
发现网上很多JNI的使用教程,也很详细,不过有的地方有些缺漏,导致很多小问题难以解决的,今天就来总结一下. 准备工作:下载NDK. 简单的说,要用到C/C++,就要用NDK.直接百度搜索然后去官网下载 ...
- python学习——练习题(12)
""" 题目:判断101-200之间有多少个素数,并输出所有素数. 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外 ...
- cmd使用代理
- sortingOrder,sortingLayer
sortingOrder 是sortingLayer内的分级 sortingLayer是抽象的分层,用于决定2D物体绘制的先后顺序. 2D物体分两类:sprite和UI. sprite虽是2D,却可以 ...
- 解决Tomcat 一闪而过的问题
启动tomcat时cmd窗口一闪而过解决方法. 问题现象: 在实际开发中一般都是eclipse+tomcat(也许还会用到tomcat的插件),我们只需要在eclipse中单击servers上的按钮就 ...