| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 17464 | Accepted: 6654 |
Description
Input
Output
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;
}poj 1151 Atlantis 二分查找+离散化,布布扣,bubuko.com
原文:http://blog.csdn.net/taoqick/article/details/38639795