http://acm.hdu.edu.cn/showproblem.php?pid=2540
题意:
给你一排木板,有一束阳光从一边斜射过来,问有多少块木板没有被完全遮住。
思路:
一种思路是这样的,我们先对木板的位置进行排序(按照x的坐标),可以证明
前i块木板的影子的最远距离是_max时,第i+1块木板的影子的距离是dis,如果
dis > _max则说明第i+1块木板没有被完全遮住,否则被完全遮住。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int N,T,A;
const int MAXN = 100010 ;
struct Node{
int x, h ;
bool operator< (const Node& n) const{
return x < n.x ;
}
}p[MAXN] ;
void solve(){
double _max = -1 ;
int ans = 0 ;
for(int i=0;i<N;i++){
double d = double(A)*p[i].h/T + p[i].x ;
if(d > _max) ans++ ;
else ;
if( _max < d ) _max = d ;
}
printf("%d\n",ans);
}
int main(){
while(scanf("%d",&N) && N){
for(int i=0;i<N;i++){
scanf("%d %d",&p[i].x ,&p[i].h);
}
scanf("%d/%d",&T,&A);
sort(p,p+N);
solve();
}
return 0 ;
}
本文介绍了如何通过排序和比较木板位置来解决HDU在线评测平台上的木板遮挡问题,包括算法思路、代码实现和实例应用。
1455

被折叠的 条评论
为什么被折叠?



