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 ...
随机推荐
- C#开发和调用Web Service
http://blog.csdn.net/h0322/article/details/4776819 1.1.Web Service基本概念 Web Service也叫XML Web Service ...
- yum提示another app is currently holding the yum lock;waiting for it to exit
Another app 解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了
- JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证
本工程的功能是实现Javaweb的servlet身份验证 一下是login.html文件中的代码 <!DOCTYPE html> <html> <head> < ...
- Java VM for IOS
http://oss.readytalk.com/avian/ http://robovm.com/ http://www.xmlvm.org/overview/
- js正则,电话,邮箱
1. <script type="text/javascript"> var str="Is this all th05777-89856825ere is5 ...
- PCH 配置
$(SRCROOT)/$(PROJECT)/PrefixHeader.pch
- iOS后台播放
### 音乐后台播放 * .当程序进入后台的时候,开启后台任务 ``` - (void)applicationDidEnterBackground:(UIApplication *) { // 开启后 ...
- 创建menu文件
一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...
- 【转】Delphi 关键字详解
absolute //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同. var Str: string[32]; StrLen: Byte absolute Str; //这个声明 ...
- thinkphp 3.2响应头 x-powered-by 修改
起初是看到千图网的登录链接 查看到的 自己做的网站也看了下 修改的办法就是TP3.2.2 的框架里 具体路径是D:\www\ThinkPHP\Library\Think\View.class.php ...