hdu 4368 Water World I
来源:互联网 发布:淘宝兼职 编辑:程序博客网 时间:2024/05/15 19:09
多校联合第七场的题,开始后50分钟第二个ac。简单的计算几何,思路比较明确,求出倾斜后的坐标,扫描一遍所有点,去掉那些水下的点,求一次面积,减去原来面积就行了。复杂度O(n)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const double Pi=acos(-1.0);const double EP=1e-8;int n, h[10005], vis[20015];double angle;struct Point{ double x, y;}p[20015], pt, tt;double x_mult(Point sp, Point ep, Point op){ return (sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x);}Point intersect(Point u1,Point u2,Point v1,Point v2){ Point res=u1; double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/ ((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x)); res.x+=(u2.x-u1.x)*t; res.y+=(u2.y-u1.y)*t; return res;}double area(Point pp[], int sum){ double s=0; if(sum<3) return s; int j; for(int i=1; i<sum-1;){ while(i<sum-1&&vis[i])i++; if(i>=sum-1)break; j=i+1; while(j<sum&&vis[j])j++; if(j>=sum)break; s+=x_mult(pp[i],pp[j],pp[0]); i=j; } s/=2; return s;}int main(){ //freopen("1.txt", "r", stdin); int i, j; while(scanf("%d%lf", &n, &angle)!=EOF){ memset(vis, 0, sizeof(vis)); angle=angle/180.0*Pi; double s=0, ans=0; p[0].x=0;p[0].y=0; for(i=1; i<=n; i++){ scanf("%d", &h[i]); s+=h[i]; p[2*i-1].x=i-1;p[2*i-1].y=h[i]; p[2*i].x=i;p[2*i].y=h[i]; } p[2*n+1].x=n;p[2*n+1].y=0; for(i=1; i<=2*n+1; i++){ pt.x=p[i].x*cos(angle)-p[i].y*sin(angle); pt.y=p[i].x*sin(angle)+p[i].y*cos(angle); p[i]=pt; } pt=p[0]; int tmp=0; for(i=1; i<=2*n+1; i++){ if(tmp==i-1&&p[i].y>=pt.y){ tmp=i; pt=p[i]; } else if(i<2*n+1&&p[i].y<=pt.y&&p[i+1].y>pt.y+EP){ for(j=tmp+1; j<i; j++)vis[j]=1; tt.x=pt.x+1;tt.y=pt.y; p[i]=intersect(pt, tt, p[i], p[i+1]); pt=p[i]; tmp=i; } } pt=p[2*n+1];tmp=2*n+1; for(i=2*n; i>=0; i--){ if(tmp==i+1&&p[i].y>=pt.y){ tmp=i; pt=p[i]; } else if(i>=1&&p[i].y<=pt.y&&p[i-1].y>pt.y+EP){ for(j=tmp-1; j>i; j--)vis[j]=1; tt.x=pt.x+1;tt.y=pt.y; p[i]=intersect(pt, tt, p[i], p[i-1]); pt=p[i]; tmp=i; } } ans=fabs(area(p, 2*n+2)); printf("%.2f\n", ans-s); } return 0;}
- hdu 4368 Water World I
- hdu_4368 Water World I (计算几何)
- hdu 2120 Ice_cream's world I
- HDU 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu-2120-Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- HDU-2120-Ice_cream's world I
- HDU 2120 Ice_cream's world I
- hdu-2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- HDU 2120 Ice_cream's world I
- HDU 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu 2120 (Ice_cream's world I)
- 【HDU 2120 Ice_cream's world I】
- 下拉菜单效果的实现方法----css+javascript
- 七个高效的编辑文本的习惯
- excle练习
- 装饰者模式(Decorator Pattern) 追MM
- Qt4.8.2 Qt调用dll
- hdu 4368 Water World I
- ubuntu中txt中文文本乱码最简单解决方法
- The Sound of Music 音乐之声英语美文English
- ubuntu中更改root用户密码
- myeclipse出现异常
- watir识别控件
- struts获取request等对象
- HDU1053--哈夫曼树
- hdu1875(MST)(prim)