Codeforces Round #340 Watering Flowers
题目:
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的更多相关文章
- [Codeforces Round #340 (Div. 2)]
[Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
- 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)
题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...
- 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 ...
- Codeforces Round #340 (Div. 2) B. Chocolate 水题
B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...
- Codeforces Round #340 (Div. 2) A. Elephant 水题
A. Elephant 题目连接: http://www.codeforces.com/contest/617/problem/A Descriptionww.co An elephant decid ...
- Codeforces Round #340 (Div. 2) D. Polyline 水题
D. Polyline 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co There are three p ...
- 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 ...
- Codeforces Round #340 (Div. 2) C
Description A flowerbed has many flowers and two fountains. You can adjust the water pressure and se ...
随机推荐
- Manacher算法
Manacher算法是求回文串最高效的算法,能在线性时间内求出以每一个字符为中心的最长回文串. 首先,我们都能想出$O(N^2)$求出每一个字符为中心的最长回文串的算法.那么我们考虑这样一种情况. ...
- POJ 3678 Katu Puzzle(强连通 法)
题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...
- vimium Keyboard Bindings
Modifier keys are specified as `<c-x>`, `<m-x>`, and `<a-x>` for ctrl+x, meta+x, a ...
- js切换实现背景颜色
<script type="text/javascript"> obj=document.getElementsByTagName('h1'); ;i<obj.l ...
- fcc的高级算法题
核心提示:本部分一个9道题,给定时间50小时.属于fcc前端学习的"高级编程脚本"题,对于初学者来说,确实算是"高级"了.如果只想着闭门造车,50小时确实也不过 ...
- ecshop去掉“云服务中心”或者是“模板堂知识库”
ECSHOP开发中心(www.68ecshop.com)教程介绍一下如何去除后台云服务中心菜单: 打开admin/templates/menu.htm,把415行的 document.getEleme ...
- LaTeX Software & Manuals
LaTeX Software & Manuals How to Typeset Equations in LaTeX LaTeX is a very powerful tool for typ ...
- localdb下载地址
https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=29062 只能使用IE下载 http://www.microsoft.co ...
- Python之路【第五篇续】:面向对象编程二
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABgQAAALaCAIAAABxja8cAAAgAElEQVR4nOzd6X9Tdd74/+uv+f5uzF
- OC-ARC
一. 基本简介 ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autorelease语句.你不再需要担心内存管理, ...