zoukankan      html  css  js  c++  java
  • UVa 511

    这题不难,但细节比较多,排序的依据有很多,比较容易出错。考差C++和STL也比较全面,比如结构体、构造函数、map、pair、vector、sort、unique、copy等等,使用C++11的lambda配合泛型算法是一个很好的选择。

    #include <bits/stdc++.h>
    #define EPS 1e-7
    using namespace std;
    
    struct MAP
    {
    	string name;
    	double x1, y1, x2, y2, area, Ratio;
    	pair<double, double> center;
    	int ilevel = -1;
    	MAP(double _x1, double _y1, double _x2, double _y2, string _name) :
    		x1(_x1), y1(_y1), x2(_x2), y2(_y2), name(_name) {
    		if (x1 > x2) swap(x1, x2);
    		if (y1 > y2) swap(y1, y2);
    		area = (x2 - x1) * (y2 - y1);
    		center.first = (x1 + x2) / 2.0;
    		center.second = (y1 + y2) / 2.0;
    		Ratio = (y2 - y1) / (x2 - x1);
    	}
    	bool contain(pair<double, double> x) const {
    		return x.first >= x1 && x.first <= x2 && x.second >= y1 && x.second <= y2;
    	}
    };
    vector<MAP> maps;
    map<string, pair<double, double>> site;
    pair<double, double> Qpos;
    
    double dist(pair<double, double> a, pair<double, double> b) {
    	return hypot(a.first - b.first, a.second - b.second);
    }
    bool cmp(const MAP & a, const MAP & b){
    	if (a.ilevel != b.ilevel) return a.ilevel > b.ilevel;
    	double d1 = dist(a.center, Qpos), d2 = dist(b.center, Qpos);
    	if (fabs(d1 - d2) > EPS) return d1 < d2;
    	d1 = fabs(a.Ratio - 0.75), d2 = fabs(b.Ratio - 0.75);
    	if (fabs(d1 - d2) > EPS) return d1 < d2;
    	d1 = dist(Qpos, make_pair(a.x2, a.y1)), d2 = dist(Qpos, make_pair(a.x2, a.y1));
    	if (fabs(d1 - d2) > EPS) return d1 > d2;
    	return a.x1 < b.x1;
    }
    int main()
    {
    	ios::sync_with_stdio(false);
    	string name; cin >> name;
    	while (cin >> name, name != "LOCATIONS") {
    		double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
    		maps.push_back(MAP(x1, y1, x2, y2, name));
    	}
    	while (cin >> name, name != "REQUESTS") {
    		double x, y; cin >> x >> y;
    		site[name] = make_pair(x, y);
    	}
    	while (cin >> name, name != "END") {
    		int level; cin >> level;
    		if (!site.count(name)) printf("%s at detail level %d unknown location
    ", name.c_str(), level);
    		else {
    			printf("%s at detail level %d ", name.c_str(), level);
    			Qpos = site[name];
    			vector<double> all_area;
    			vector<MAP> C1, C2;
    			for (auto & i : maps) if (i.contain(Qpos)) {
    				C1.push_back(i);
    				all_area.push_back(i.area);
    			}
    			sort(all_area.begin(), all_area.end(), greater<double>());
    			unique(all_area.begin(), all_area.end(), greater<double>());
    			for (auto & i : C1)
    				i.ilevel = find_if(all_area.begin(), all_area.end(), [i](const double area){return fabs(area - i.area) <= EPS; }) - all_area.begin() + 1;
    			sort(C1.begin(), C1.end(), cmp);
    			copy_if(C1.begin(), C1.end(), back_inserter(C2), [level](const MAP & x) {return x.ilevel == level; });
    			if (C1.size() == 0) puts("no map contains that location");
    			else if (C2.size() == 0) printf("no map at that detail level; using %s
    ", C1[0].name.c_str());
    			else printf("using %s
    ", C2[0].name.c_str());
    		}
    	}
    	return 0;
    }
    


  • 相关阅读:
    封装
    面向对象的思想
    Arrays工具类
    二分查找
    选择排序
    冒泡排序
    对象数组
    二维数组
    一维数组
    循环语句注意事项
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312704.html
Copyright © 2011-2022 走看看