题目:

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. JZOJ P1830[9.30]送牛奶

    传送门 临近NOIp,写一些简单题. 二分+BFS,注意的是要把数组开小点,有效减少memset的时间. //OJ 1830 //by Cydiater //2016.9.22 #include &l ...

  2. Docker入门教程(六)另外的15个Docker命令

    Docker入门教程(六)另外的15个Docker命令 [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第六篇,继续介绍Docker命令.之前的第二篇文章 ...

  3. BZOJ3207: 花神的嘲讽计划Ⅰ

    显然hash,然后stl随便搞. #include<bits/stdc++.h> #define N 100005 using namespace std; typedef unsigne ...

  4. firefox怎么修改tls协议号

    如果目前正在运行火狐26,你可能已经注意到,浏览器仅支持SSL 3.0和TLS 1.0,默认不开启TLS 1.1或TLS 1.2.另外我们知道Firefox 27 已经实现了对TLS 1.2的支持.  ...

  5. JS定时器

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

  6. C# Pointer types

    https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx

  7. GPRS/3G

    像GPRS/3G模块之类的应用,需要连接,登陆,初始化等步骤完成后才能传输数据,而这些步骤又比较耗时. 所以用 状态机 + 超时 的机制来实现比较合理. 如下代码片段来描述数据透传 : 状态机 + 超 ...

  8. sqlserver检测死锁;杀死锁和进程;查看锁信息

    http://blog.sina.com.cn/s/blog_9dcdd2020101nf4v.html sqlserver检测死锁;杀死锁和进程;查看锁信息 ( ::)转载▼ 标签: sql 检测死 ...

  9. Python开发【第十八篇】:MySQL(二)

    视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...

  10. MYSQL双主故障解决实例。

    根据报错得知,获取到的主库文件格式错误. 一.锁住主从正常的库 Mysql> flush tables with read lock; 锁表 unlock tables; 解锁 SHOW MAS ...