首页 > 代码库 > poj 1151 Atlantis 二分查找+离散化

poj 1151 Atlantis 二分查找+离散化

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17464 Accepted: 6654

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 


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;
}