题目:

http://www.codeforces.com/contest/617/problem/C

自己感觉是挺有新意的一个题目, 乍一看挺难得(= =)。

其实比较容易想到的一个笨办法就是:分别计算出每个点到喷泉的距离,然后分别按照距离远近排序(要用到两个数组),然后选定一个喷泉,从近到远依次选点,然后标记该点,从另一个喷泉中找到距离他最远且还没用被标记的点,这个距离加前一个喷泉的距离就是要求的答案,选最小的即可,n方的时间复杂度。

需要注意的几点:

1、         对于每个点最好用结构体,保存距离的同时保存坐标。因为后面涉及到对两个数组进行排序,排序后还要从第二个喷泉对应的数组中找到与第一个喷泉所选的同一个点。这两个数组的排序是不同的,所以需要根据坐标寻找。

2、         用于标记的数组一定是先从第二个喷泉对应数组找到点,对这个的点的下标进行标记。

3、         有一种特殊情况是,第一个喷泉距离为0,第二个喷泉完全覆盖所有点。

 #include<stdio.h>
#include<algorithm>
#define maxn 2005
using namespace std; struct node{
int x;
int y;
long long d;
};
node d1[maxn];
node d2[maxn];
int book[maxn];
long long n,x1,y1,x2,y2;
//快排
void quicksort(int left,int right,node d[]){
if(left>right)
return;
int i = left,j = right;
node temp = d[left];
while(i!=j){
while(d[j].d>=temp.d && i<j)
j--;
while(d[i].d<=temp.d && i<j)
i++;
if(i<j){
node t = d[i];
d[i] = d[j];
d[j] = t;
}
}
d[left] = d[i];
d[i] = temp; quicksort(left,i-,d);
quicksort(i+,right,d);
}
//保存点与计算距离
void getd(long long x,long long y,int index){
d1[index].x = x; d1[index].y = y;
d2[index].x = x;
d2[index].y = y;
d1[index].d = (x-x1)*(x-x1) + (y-y1)*(y-y1);
d2[index].d = (x-x2)*(x-x2) + (y-y2)*(y-y2);
}
int main(){
scanf("%I64d%I64d%I64d%I64d%I64d",&n,&x1,&y1,&x2,&y2);
for(int i = ;i<n;i++){
long long x,y;
scanf("%I64d%I64d",&x,&y);
getd(x,y,i);
} quicksort(,n-,d1);
quicksort(,n-,d2); long long ans = d2[n-].d;//特殊情况,第二个喷泉全部覆盖
for(int i = ;i<n;i++){
for(int j = ;j<n;j++){//从第二个数组里寻找
if(d1[i].x == d2[j].x && d1[i].y == d2[j].y){
book[j] = ;
break;
}
}
//找到第二个数组里最大的未标记的点
int t = n-;
while(book[t] == && t>=){
t--;
}
ans = min(ans,d1[i].d+d2[t].d);
}
printf("%I64d\n",ans);
return ;
}

Codeforces Round #340 Watering Flowers的更多相关文章

  1. [Codeforces Round #340 (Div. 2)]

    [Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...

  2. Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力

    C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...

  3. 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)

    题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...

  4. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

  5. Codeforces Round #340 (Div. 2) B. Chocolate 水题

    B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...

  6. Codeforces Round #340 (Div. 2) A. Elephant 水题

    A. Elephant 题目连接: http://www.codeforces.com/contest/617/problem/A Descriptionww.co An elephant decid ...

  7. Codeforces Round #340 (Div. 2) D. Polyline 水题

    D. Polyline 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co There are three p ...

  8. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

    任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  9. Codeforces Round #340 (Div. 2) C

    Description A flowerbed has many flowers and two fountains. You can adjust the water pressure and se ...

随机推荐

  1. HDU5672String(尺标法)

    问题描述 有一个 10\leq10≤长度\leq 1,000,000≤1,000,000 的字符串,仅由小写字母构成.求有多少个子串,包含有至少k(1 \leq k \leq 26)k(1≤k≤26) ...

  2. JavaWeb---总结(八)HttpServletResponse对象(二)

    一.HttpServletResponse常见应用--生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类, 生成随机图片范例: 1 package gacl ...

  3. gnuplot使用1

    安装之后,迫切需要运行一个程序来看看,首先要找到软件默认的使用路径: 输入 show loadpath命令就会显示默认查找的几个路径, loadpath is loadpath from GNUPLO ...

  4. java编程思想-java 异常使用指南

    应该在以下情况下使用异常: 在恰当的级别处理问题(在知道该如何处理的情况下才捕获异常). 解决问题并且重新调用产生异常的方法. 进行少许修补,然后绕过异常发生的地方继续执行. 用别的数据进行计算,以代 ...

  5. JS字符串转换成json对象。。。。

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. windows7-SQLyog 安装图解

    双击: 双击已下载的SQLyog Enterprise 安装文件,点击“next”,选择“I accept...”,勾选安装组件,选择安装目录,等待安装完成. 协议:选择我接受 选择操作   选择路径 ...

  7. _mkdir

    [内容摘要]: C语言 在VS2013环境下使用_mkdir返回值是-,而且文件夹不存在,#include stdio.h#include direct.hmain(){)printf("无 ...

  8. cmake 编译 c++ dll 的一个例子(更新2:增加 python 调用方法)

    CMakeLists.txt project(xxx) add_library(xxx SHARED xxx.cpp) add_executable(yyy yyy.cpp) target_link_ ...

  9. VS插件开发,启用实验室环境

    启用外部程序: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe 命令行参数 /rootsuffix ...

  10. CKEditor的使用方法

    CKEditor的使用方法 2014-03-31 09:44 8649人阅读 评论(1) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. ckeditor 的官方网站是 http:/ ...