zoukankan      html  css  js  c++  java
  • 离散化解矩形覆盖

    神马是离散化:http://www.matrix67.com/blog/archives/108

    例题:POJ 1151 Atlantis

    #include <iostream>
    #include <vector>
    #include <set>
    #include <algorithm>
    #include <iomanip>
     
    using namespace std;
     
    struct rect
    {
        double x1;
        double x2;
        double y1;
        double y2;
    };
     
    int main(int argc, char **argv)
    {
        int t = 1;
        int n;
     
        while (cin >> n && n)
        {
            vector<double> vecX, vecY;
            vector<rect> vecRec;
            set<pair<double, double> > setCover;
     
            // Insert x, y to vecX, vecY.
            while (n--)
            {
                rect r;
     
                cin >> r.x1 >> r.y1 >> r.x2 >> r.y2;
     
                vecX.push_back(r.x1);
                vecX.push_back(r.x2);
                vecY.push_back(r.y1);
                vecY.push_back(r.y2);
                vecRec.push_back(r);
            }
     
            // Sort
            sort(vecX.begin(), vecX.end());
            sort(vecY.begin(), vecY.end());
     
            // Mark cover
            for (vector<rect>::const_iterator it = vecRec.begin();
                it != vecRec.end();
                ++it)
            {
                vector<double>::iterator x1 = lower_bound(vecX.begin(), vecX.end(), it->x1);
                vector<double>::iterator x2 = lower_bound(vecX.begin(), vecX.end(), it->x2);
                vector<double>::iterator y1 = lower_bound(vecY.begin(), vecY.end(), it->y1);
                vector<double>::iterator y2 = lower_bound(vecY.begin(), vecY.end(), it->y2);
     
                for (vector<double>::iterator x = x1; x != x2; ++x)
                {
                    for (vector<double>::iterator y = y1; y != y2; ++y)
                    {
                        setCover.insert(make_pair(*x, *y));
                    }
                }
            }
     
            double sum = 0.0;
     
            // Calculate result
            for (vector<double>::const_iterator x = vecX.begin(); x != vecX.end() - 1; ++x)
            {
                for (vector<double>::const_iterator y = vecY.begin(); y != vecY.end() - 1; ++y)
                {
                    if (setCover.find(make_pair(*x, *y)) != setCover.end())
                    {
                        sum += (*(x + 1) - *x) * (*(y + 1) - *y);
                    }
                }
            }
     
            // Make sure output format is correct.
            cout << "Test case #" << t++ << endl;
            cout << "Total explored area: " << setprecision(2) << fixed << sum << endl;
            cout << endl;
        }
     
        return 0;
    }
  • 相关阅读:
    httphelper
    MD5加密
    json操作
    将list转成tree
    GenerateId类:生成唯一id、订单号
    加密、解密(默认密钥向量)
    Hadoop HDFS批量处理
    OceanBase学习总结
    TiDB学习
    开机自启动rc.local文件修改权限
  • 原文地址:https://www.cnblogs.com/codingmylife/p/2698855.html
Copyright © 2011-2022 走看看