Poj 1151 Atlanta Binary Search + discretization

Source: Internet
Author: User

Atlanta
Time limit:1000 ms   Memory limit:10000 K
Total submissions:17464   Accepted:6654

Description

There are several except ent Greek texts that contain descriptions of the fabled island Atlanta. some of these texts even include maps of parts of the island. but unfortunately, these maps describe different regions of Atlanta. 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 shocould 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 occupied ed area: a", where A is the total occupied ed 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

210 10 20 2015 15 25 25.50

Sample output

Test case #1Total explored area: 180.00 


I met several bugs:

1. printf ("%. 2f ", Res); for double type instead of the setence printf (" %. 2lf ", Res); because for G ++, % lf is not supported, so we must use % F for output and % lf for input.

2. Output a blank line after each test case.


#include <iostream>#include <map>#include <vector>#include <algorithm>#include <limits.h>#include <assert.h>#include <stdio.h>using namespace std;class Rec { public:  double ltx, lty, rdx, rdy;  Rec(double x1 = 0, double y1 = 0, double x2 = 0, double y2 = 0): ltx(x1), lty(y1), rdx(x2), rdy(y2){  }};class Inte {public:  double s, e;    Inte(double s1 = 0, double e1 = 0) : s(s1), e(e1) {  }};class cmp1 { public:  bool operator()(const Inte& inte1, const Inte& inte2) const{    return inte1.s == inte2.s ? inte1.e < inte2.e : inte1.s < inte2.s;  }};class Solution{ public:  vector<Inte> merge(vector<Inte>& inte) {    vector<Inte> res;    if (inte.empty())      return res;    sort(inte.begin(), inte.end(), cmp1());    Inte cur(inte[0].s, inte[0].e);    int size = inte.size();    for (int i = 1; i < size; ++i) {      if (inte[i].s > cur.e) {        res.push_back(cur);        cur.s = inte[i].s, cur.e = inte[i].e;      }      else {        cur.s = min(cur.s, inte[i].s);        cur.e = max(cur.e, inte[i].e);      }    }    res.push_back(cur);    return res;      }  double calArea(vector<vector<Inte> >& inters, const vector<double>& xcoor) {    double res = 0;    int size = inters.size();    for (int i = 0; i < size; ++i) {      vector<Inte> cur = merge(inters[i]);      int cursize = cur.size();      for (int j = 0; j < cursize; ++j)        res += (cur[j].e - cur[j].s)*(xcoor[i+1]-xcoor[i]);    }    return res;  }  double calAllArea(const vector<Rec>& recs) {    int i, size = recs.size(), j, intesize;    vector<double> xcoor;        for (i = 0; i < size; ++i) {      xcoor.push_back(recs[i].ltx);      xcoor.push_back(recs[i].rdx);    }    sort(xcoor.begin(), xcoor.end());    xcoor.resize(unique(xcoor.begin(), xcoor.end()) - xcoor.begin());    intesize = xcoor.size() - 1;    vector<vector<Inte> > inters(intesize, vector<Inte>(0));            for (i = 0; i < size; ++i) {      int j1 = lower_bound(xcoor.begin(), xcoor.end(), recs[i].ltx) - xcoor.begin();      int j2 = upper_bound(xcoor.begin(), xcoor.end(), recs[i].rdx) - xcoor.begin();      for (j = j1; j <= j2 -2; ++j) {        inters[j].push_back( Inte(recs[i].rdy, recs[i].lty));      }    }    double res = calArea(inters, xcoor);    return res;  }};int main(){  Solution s;  freopen("E://test.txt","r",stdin);  vector<Rec> recs;  int k, count = 0;  double x1, x2, y1, y2;  while (scanf("%d", &k) && k != 0) {    recs.resize(0);    printf("Test case #%d\n", ++count);    while (k--) {      scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);      recs.push_back(Rec(x1, y2, x2, y1));    }    double res = s.calAllArea(recs);    printf("Total explored area: %.2f\n\n", res);      }  return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.