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 ...
随机推荐
- NOIp 0924 水题记
这场貌似是gcd专场? 第一题很有意思,模拟gcd的过程即可. //0924 candy //by Cydiater //2016.9.24 #include <iostream> #in ...
- js日期加减
先补充下基础知识: var myDate = new Date(); //myDate默认返回当前时间 myDate.getYear(); //获取当前年份(2位) myDate.getFullYea ...
- JavaScript的闭包原理
什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 个人的理解是 ...
- php 生成随机字符串 abcdeft....789
) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = &quo ...
- jquery特效收藏
js网址收藏: 懒人图库:www.lanrentuku.com 懒人之家:http://www.lanrenzhijia.com/jquery/list_5_2.html 1.UI下载:http:// ...
- Tween + 缓动函数
Unity-Tween http://www.cnblogs.com/MrZivChu/p/UnityTween.html iTween: iTween大解构(一)之抛物线移动 http://blog ...
- Multiple actions were found that match the request Web API
在WebAPI工程入口不对外公开的接口不能使用public. [HttpPost] public string PostRequest([FromBody] Model model) { /// } ...
- java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别
java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别 以前一直没有注意过这个问题,前两天机缘巧合上网查了一下,然后自 ...
- composer错误收集
1. Problem 1 - The requested package ** is satisfiable by ** but these conflict with your requiremen ...
- 缓存算法(页面置换算法)-FIFO、LFU、LRU
在前一篇文章中通过leetcode的一道题目了解了LRU算法的具体设计思路,下面继续来探讨一下另外两种常见的Cache算法:FIFO.LFU 1.FIFO算法 FIFO(First in First ...