题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4445
题目大意:
有n发炮弹,初速为v[i],所在高度和位置分别为h和0,敌人的朋友的所在高度为0,位置分别为[l1,r1]和[l2,r2].
第一次发炮前可以设定一个角度,之后都用这个角度打,判断最多有多少个炮弹会打到敌人,且没有一发炮弹会打到朋友.
题目思路:
暴力枚举[-pi/2,pi/2](居然每次+0.5都行= =,我去...)
也很容易想到三分(经验......),三分[-pi/2,0]和[0,-pi/2]即可.
代码(暴力 140ms):
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-8)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define _max(x,y) (((x)>(y))? (x):(y))
#define _min(x,y) (((x)<(y))? (x):(y))
#define _abs(x) ((x)<0? (-(x)):(x))
#define getmin(x,y) (x= (x<0 || (y)<x)? (y):x)
#define getmax(x,y) (x= ((y)>x)? (y):x)
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
int TS,cas=1;
const int M=200+5;
int n;
double h,l1,r1,l2,r2;
double v[M],g=9.8;
int getNum(double a){
int cnt=0;
for(int i=1;i<=n;i++){
double x=v[i]*cos(a)/g*(v[i]*sin(a)+sqrt(v[i]*v[i]*sin(a)*sin(a)+2*g*h));
if(l2<=x && x<=r2) return 0;
if(l1<=x && x<=r1 && (x<l2 || x>r2)) cnt++;
}
return cnt;
}
void run(){
int i,j;
scanf("%lf%lf%lf%lf%lf",&h,&l1,&r1,&l2,&r2);
for(i=1;i<=n;i++) scanf("%lf",&v[i]);
double l,r,mid,midmid;
int t1,t2,ans=0;
for(l=-90.0;l<=90;l+=0.5)
getmax(ans,getNum(l/180*pi));
printf("%d\n",ans);
}
void preSof(){
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
preSof();
//run();
while(~scanf("%d",&n) , n) run();
//for(scanf("%d",&TS);cas<=TS;cas++) run();
return 0;
}
代码(三分):
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-8)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define _max(x,y) (((x)>(y))? (x):(y))
#define _min(x,y) (((x)<(y))? (x):(y))
#define _abs(x) ((x)<0? (-(x)):(x))
#define getmin(x,y) (x= (x<0 || (y)<x)? (y):x)
#define getmax(x,y) (x= ((y)>x)? (y):x)
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
int TS,cas=1;
const int M=200+5;
int n;
double h,l1,r1,l2,r2;
double v[M],g=9.8;
int getNum(double a){
int cnt=0;
for(int i=1;i<=n;i++){
double x=v[i]*cos(a)/g*(v[i]*sin(a)+sqrt(v[i]*v[i]*sin(a)*sin(a)+2*g*h));
if(l2<=x && x<=r2) return 0;
if(l1<=x && x<=r1 && (x<l2 || x>r2)) cnt++;
}
return cnt;
}
void run(){
int i,j;
scanf("%lf%lf%lf%lf%lf",&h,&l1,&r1,&l2,&r2);
for(i=1;i<=n;i++) scanf("%lf",&v[i]);
double l,r,mid,midmid;
int t1,t2,ans=0;
for(l=0,r=pi/2;r-l>=eps;){
mid=(r+l)/2;
midmid=(r+mid)/2;
t1=getNum(mid),t2=getNum(midmid);
if(t1<t2) l=mid,getmax(ans,t2);
else r=midmid,getmax(ans,t1);
}
for(l=-pi/2,r=0;r-l>=eps;){
mid=(r+l)/2;
midmid=(r+mid)/2;
t1=getNum(mid),t2=getNum(midmid);
if(t1<t2) l=mid,getmax(ans,t2);
else r=midmid,getmax(ans,t1);
}
printf("%d\n",ans);
}
void preSof(){
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
preSof();
//run();
while(~scanf("%d",&n) , n) run();
//for(scanf("%d",&TS);cas<=TS;cas++) run();
return 0;
}