首页 > 技术文章 > codeforces 485B Valuable Resources 解题报告

windysai 2014-11-06 11:26 原文

题目链接:http://codeforces.com/problemset/problem/485/B

题目意思:需要建造一个正方形的city,这座city的边需要满足平行于 x 轴和 y 轴,而且这座city需要包含所有的 mines。问这座city的最少面积是多少。

    我一开始的做法是分别求出每个象限的最大 x 和 最大 y 值,然后通过组合一、四象限求出较大的y,然后对二、三象限求出较大的y,取最大的那个;x 也 类似这样取。最后通过max(x,y) * max(x,y)就是答案。不过好多细节要注意,比如要考虑到如果在坐标轴上的点如何处理,还有一个致命的一点是,如果所有点在同一个象限里,我的代码是计算不出这种情况的,因为都是到原点(0,0)所围的面积= =

    看了别人的思路,一下子恍然大悟,简单,清晰,又不容易出错。

    每个坐标点都记录下来,然后sort完后分别求出 x[n-1]-x[0],y[n-1]-y[0],当然这个值有可能是负数!有可能不同象限嘛,尤其是一正一负的时候!所以要求绝对值。最后取绝对值较大的那个,再平方就是答案。

    真的是又想复杂了~~~~

   

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <cmath>
 7 using namespace std;
 8 
 9 const int maxn = 1000 + 10;
10 typedef __int64 LL;
11 LL x[maxn], y[maxn];
12 
13 int main()
14 {
15     #ifndef ONLINE_JUDGE
16         freopen("input.txt", "r", stdin);
17     #endif
18     int n;
19 
20     while (scanf("%d", &n) != EOF)
21     {
22         for (int i = 0; i < n; i++)
23             scanf("%I64d%I64d", &x[i], &y[i]);
24         sort(x, x+n);
25         sort(y, y+n);
26 
27         LL max_x = abs(x[n-1]-x[0]);
28         LL max_y = abs(y[n-1]-y[0]);
29         LL max_ans = max(max_x, max_y);
30         printf("%I64d\n", max_ans * max_ans);
31     }
32     return 0;
33 }

 

推荐阅读