hdu4445(三分法)

Crazy Tank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2672    Accepted Submission(s): 470


Problem Description
Crazy Tank was a famous game about ten years ago. Every child liked it. Time flies, children grow up, but the memory of happy childhood will never go.

Now you’re controlling the tank Laotu on a platform which is H meters above the ground. Laotu is so old that you can only choose a shoot angle(all the angle is available) before game start and then any adjusting is not allowed. You need to launch N cannonballs and you know that the i-th cannonball’s initial speed is Vi.
On the right side of Laotu There is an enemy tank on the ground with coordination(L1, R1) and a friendly tank with coordination(L2, R2). A cannonball is considered hitting enemy tank if it lands on the ground between [L1,R1] (two ends are included). As the same reason, it will be considered hitting friendly tank if it lands between [L2, R2]. Laotu's horizontal coordination is 0.
The goal of the game is to maximize the number of cannonballs which hit the enemy tank under the condition that no cannonball hits friendly tank.
The g equals to 9.8.
 

Input
There are multiple test case.
Each test case contains 3 lines.
The first line contains an integer N(0≤N≤200), indicating the number of cannonballs to be launched.
The second line contains 5 float number H(1≤H≤100000), L1, R1(0<L1<R1<100000) and L2, R2(0<L2<R2<100000). Indicating the height of the platform, the enemy tank coordinate and the friendly tank coordinate. Two tanks may overlap.
The third line contains N float number. The i-th number indicates the initial speed of i-th cannonball.
The input ends with N=0.
 

Output
For each test case, you should output an integer in a single line which indicates the max number of cannonballs hit the enemy tank under the condition that no cannonball hits friendly tank.
 

Sample Input
  
  
2 10 10 15 30 35 10.0 20.0 2 10 35 40 2 30 10.0 20.0 0
 

Sample Output
  
  
1 0
Hint
In the first case one of the best choices is that shoot the cannonballs parallelly to the horizontal line, then the first cannonball lands on 14.3 and the second lands on 28.6. In the second there is no shoot angle to make any cannonball land between [35,40] on the condition that no cannonball lands between [2,30].
 

Source
 


本题要求选择适当角度,是初速度不同的炮弹尽可能多的落在敌人阵地,但又不能伤到友军阵地。

本题是个典型的物理问题。我认为解物理问题有两种思路:

        1.自己列出方程,求得解的表达式。这个是所有人都会想到的,但是计算机处理的物理问题往往很难求得解析式,故想靠此法一步走到底几乎是不可能的。

        2.枚举思想。理解题意,列出中间变量的方程(中间变量是可以求解的),通过枚举得到最终答案。有时可以考虑二分来优化枚举,当所求函数具有单调性时可以三分法求极值。

 

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

const double PI=acos(-1),g=9.8,eps=1e-6;
double s[200+10];
int n;
double l1,r1,l2,r2,h;

int Get_num(double angl)
{
	int i,tmp=0;
	for(i=1;i<=n;i++)
	{
	      double vx=cos(angl)*s[i];
		  double vy1=sin(angl)*s[i];
		  double vy2=sqrt(2*g*h+vy1*vy1);
		  double t=(vy2+vy1)/g;

		  double dx=vx*t;
		  if(dx>=l2+eps&&dx<=r2+eps)
			  return 0;
		  if((dx>=l1+eps&&dx<=r1+eps))
			  tmp++;
	}
	return tmp;
}

int main()
{
	int i;
	while(~scanf("%d",&n),n)
	{
		scanf("%lf%lf%lf%lf%lf",&h,&l1,&r1,&l2,&r2);
		for(i=1;i<=n;i++)
			scanf("%lf",&s[i]);

		double inc=PI/1000;
		double angl;
		int num,ans=-1;
		for(angl=-PI/2;angl<=PI/2+eps;angl+=inc)
		{
			num=Get_num(angl);
			if(num>ans)
				ans=num;
		}
		printf("%d\n",ans);
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值