POJ 1151 Atlantis

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16244 Accepted: 6183

Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area. 
The input file is terminated by a line containing a single 0. Don't process it.

Output

For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point. 
Output a blank line after each test case.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

Source


 原理是线段的切割
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define N 100000
using namespace std;
struct num
{
    double x1,y1,x2,y2;
}a[N],b[N];
int Top,pos;
bool uv;
bool cmp(const struct num &p1,const struct num &p2)
{
    return p1.x1<p2.x1;
}
int main()
{
    //freopen("data.txt","r",stdin);
    void ch();
    void add(double x1,double y1,double x2,double y2,int p);
    int n;
    int T=1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        {
            break;
        }
        for(int i=0;i<=n-1;i++)
        {
            scanf("%lf %lf %lf %lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
        }
        sort(a,a+n,cmp);
        Top = 0;
        for(int i=0;i<=n-1;i++)
        {
            pos = Top;
            double x3 = a[i].x1,y3 = a[i].y1;
            double x4 = a[i].x2,y4 = a[i].y2;
            bool in = false;
            for(int j=0;j<=Top-1;j++)
            {
               double x1 = b[j].x1,y1 = b[j].y1;
               double x2 = b[j].x2,y2 = b[j].y2;
               if(x3>x2||x1>x4||y3>y2||y1>y4)
               {
                   continue;
               }
               in = true;
               pos = j;
               uv = false;
               double k1 = max(x1,x3);
               double k2 = min(x4,x2);
               if(k1>x1)
               {
                   add(x1,y1,k1,y2,pos);
                   ch();
               }
               if(k2<x2)
               {
                   add(k2,y1,x2,y2,pos);
                   ch();
               }
               double k3 = max(y3,y1);
               double k4 = min(y4,y2);
               if(k3>y1)
               {
                   add(k1,y1,k2,y3,pos);
                   ch();
               }
               if(k4<y2)
               {
                   add(k1,y4,k2,y2,pos);
                   ch();
               }
            }
            add(x3,y3,x4,y4,pos);
            if(!in)
            {
                Top++;
                continue;
            }
            ch();
        }
        double sum = 0;
        for(int i=0;i<=Top-1;i++)
        {
            sum+=(b[i].x2-b[i].x1)*(b[i].y2-b[i].y1);
        }
        printf("Test case #%d\n",T++);
        printf("Total explored area: %.2lf\n",sum);
        printf("\n");
    }
    return 0;
}
void ch()
{
    if(uv)
    {
        Top++;
    }
    pos = Top;
    uv = true;
}
void add(double x1,double y1,double x2,double y2,int p)
{
    b[p].x1 = x1;
    b[p].y1 = y1;
    b[p].x2 = x2;
    b[p].y2 = y2;
}

以上是用矩形切割的想法做的,由于数据量小,没问题,如果矩形较多的话,会tle,今天学了一下扫描线,面积并

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define N 500
using namespace std;
struct num
{
    double x,y1,y2;
    int deg;
}infor[N];
struct tree
{
    int l,r,c;
    double ly,ry,dis;
}a[4*N];
double b[N];
bool cmp(num p1,num p2)
{
    return p1.x<p2.x;
}
int main()
{
    void build(int k,int l,int r);
    void update(int k,num p);
    int n;
    int T = 1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        {
            break;
        }
        int t = 1;
        for(int i=1;i<=n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            infor[t].x = x1;
            infor[t].y1 = y1;
            infor[t].y2 = y2;
            infor[t].deg = 1;
            b[t++] = y1;
            infor[t].x = x2;
            infor[t].y1 = y1;
            infor[t].y2 = y2;
            infor[t].deg = -1;
            b[t++] = y2;
        }
        sort(infor+1,infor+t,cmp);
        sort(b+1,b+t);
        build(1,1,t-1);
        update(1,infor[1]);
        double res = 0;
        for(int i=2;i<=t-1;i++)
        {
            res+=(infor[i].x-infor[i-1].x)*a[1].dis;
            update(1,infor[i]);
        }
        printf("Test case #%d\n",T++);
        printf("Total explored area: %.2lf\n",res);
        printf("\n");
    }
    return 0;
}
void build(int k,int l,int r)
{
    a[k].l = l;
    a[k].r = r;
    a[k].dis = a[k].c = 0;
    a[k].ly = b[l];
    a[k].ry = b[r];
    if(l+1==r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid,r);
}
void cal(int k)
{
    if(a[k].c>0)
    {
        a[k].dis = a[k].ry - a[k].ly;
        return ;
    }
    if(a[k].l+1==a[k].r)
    {
        a[k].dis = 0;
    }else
    {
        a[k].dis = a[k<<1].dis+a[k<<1|1].dis;
    }
}
void update(int k,num p)
{
    if(a[k].ly==p.y1&&a[k].ry==p.y2)
    {
        a[k].c+=p.deg;
        cal(k);
        return ;
    }
    if(p.y2<=a[k<<1].ry)
    {
        update(k<<1,p);
    }else if(p.y1>=a[k<<1|1].ly)
    {
        update(k<<1|1,p);
    }else
    {
        num e = p;
        e.y2 = a[k<<1].ry;
        update(k<<1,e);
        e = p;
        e.y1 = a[k<<1|1].ly;
        update(k<<1|1,e);
    }
    cal(k);
}



内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值