1007(数组,时间超限)
2017-03-19 11:16
127 查看
Problem Description
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a
configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered
to be 0.
Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated
by N = 0.
Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
Sample Input
Sample Output
刚开始用数组的时候就已经感觉时间会超限了,没想到还真的超限的,刚开始用的数组用的double型,编译没有错误,一运行软件就提示程序停止工作,最后找了很久才发现时double数组长度的问题,这应该算是个低级问题了吧,果然还是菜鸟啊。既然数组做法时间超限,而且感觉也没法修改了吧,看来又得膜拜大神了
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a
configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered
to be 0.
Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated
by N = 0.
Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
Sample Input
2 0 0 1 1 2 1 1 1 1 3 -1.5 0 0 0 0 1.5 0
Sample Output
0.71 0.00 0.75
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int N; float coordinates[100000][3]; while(cin>>N&&N!=0&&N>=2&&N<=100000) { for(int i=0;i<N;i++) cin>>coordinates[i][0]>>coordinates[i][1]; float radius,minRadius; minRadius=(sqrt((coordinates[0][0]-coordinates[1][0])*(coordinates[0][0]-coordinates[1][0])+ (coordinates[0][1]-coordinates[1][1])*(coordinates[0][1]-coordinates[1][1])))/2; for(int i=1;i<N-1;i++) for(int j=i+1;j<N;j++) { if(coordinates[i][0]==coordinates[j][0]&&coordinates[i][1]==coordinates[j][1]) { cout<<0.00<<endl; break; } radius=(sqrt((coordinates[i][0]-coordinates[j][0])*(coordinates[i][0]-coordinates[j][0])+ (coordinates[i][1]-coordinates[j][1])*(coordinates[i][1]-coordinates[j][1])))/2; if(minRadius>radius) minRadius=radius; } cout<<fixed<<setprecision(2)<<minRadius<<endl; } return 0; }
刚开始用数组的时候就已经感觉时间会超限了,没想到还真的超限的,刚开始用的数组用的double型,编译没有错误,一运行软件就提示程序停止工作,最后找了很久才发现时double数组长度的问题,这应该算是个低级问题了吧,果然还是菜鸟啊。既然数组做法时间超限,而且感觉也没法修改了吧,看来又得膜拜大神了
相关文章推荐
- 1007(分治法,快速排序,但还是时间超限)
- C/C++面试之算法系列--1~n无序数组时间复杂度为O(n)排序
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- PHP 中巧用数组降低程序的时间复杂度
- 数据结构C语言>数组>一维数组的遍历 空间换取时间
- 查找数组最大值最小值:时间复杂度最多为:o(3n/2)
- 螺旋数组。。。。(今天输错一个东西,检查花了好多时间)
- PHP 巧用数组降低程序的时间复杂度
- 面试题精选(86):O(lgn)时间内找出有序数组中某个元素出现的次数
- 问题描述:一个长度为2n的(整型)数组元素为 a1 a2 ... an b1 b2 ... bn 要求: 用O(1)的空间代价, 在O(n)时间内把数组变成 a1 b1 a2 b2 a3 b3 ... an bn
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 动态规划求数组中和最大字串的值 以及 字符串的循环移位(要求空间复杂度O(1),时间复杂度O(n))
- PHP 中巧用数组降低程序的时间复杂度
- PHP 中巧用数组降低程序的时间复杂度
- [转]PHP 中巧用数组降低程序的时间复杂度
- php数组应用之比较两个时间的相减排序
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 面试题精选(79):取值为【1,n-1】含n个元素的整数数组至少存在一个重复数,O(n)时间内找出其中任意一个重复数
- PHP 巧用数组降低程序的时间复杂度
- 给定数组Arr[n],O(n)时间内找出每个元素左侧所有元素中位置最靠近该元素且大于该元素的元素