Codeforces(429D - Tricky Function)近期点对问题
2 seconds
256 megabytes
standard input
standard output
Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before
the contest. Because he wants to make sure Iahub will be the one qualified, he tells Iahub the following task.
You're given an (1-based) array a with n elements.
Let's define function f(i, j) (1 ≤ i, j ≤ n) as (i - j)2 + g(i, j)2.
Function g is calculated by the following pseudo-code:
int g(int i, int j) {
int sum = 0;
for (int k = min(i, j) + 1; k <= max(i, j); k = k + 1)
sum = sum + a[k];
return sum;
}
Find a value mini ≠ j f(i, j).
Probably by now Iahub already figured out the solution to this problem. Can you?
The first line of input contains a single integer n (2 ≤ n ≤ 100000).
Next line contains n integers a[1], a[2],
..., a[n] ( - 104 ≤ a[i] ≤ 104).
Output a single integer — the value of mini ≠ j f(i, j).
4
1 0 0 -1
1
2
1 -1
2
解法:能够将结果转化为求(i,sum(i))近期点对问题。sum(i)为前缀1-i之和;
代码:
/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
const double pi=acos(-1.0);
typedef long long LL;
const int Max=10100;
const int INF=1000000007; struct point
{
double x,y;
int lable;
} ;
point points[1001000]; bool operator<(const point& a,const point& b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
bool compareY(const point& a,const point& b)
{
return a.y<b.y;
}
double getDistance(const point& a,const point& b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double getMiniDistance(int left,int right)
{
if(left==right)
return 1000000000000;
if(right-left==1)
{
if(points[left].lable^points[right].lable)
return getDistance(points[left],points[right]);
else
return 1000000000000;
}
int mid=(left+right)/2;
double num=min(getMiniDistance(left,mid),getMiniDistance(mid+1,right));
double mLine=points[mid].x;
int L=mid;
while(L>left&&mLine-points[L].x<=num)
L--;
int R=mid+1;
while(R<right&&points[R].x-mLine<=num)
R++;
sort(points+L,points+R+1,compareY);
for(int i=L; i<=R; i++)
{
for(int j=i+1; j<=min(R,i+5); j++)
{
if(points[j].y-points[i].y>=num)
break;
if(points[j].lable^points[i].lable)
{
num=min(num,getDistance(points[i],points[j]));
}
}
}
return num;
} int main()
{
int T;
scanf("%d",&T);
for(int i=0; i<T; i++)
{
int N;
scanf("%d",&N);
for(int i=0; i<N; i++)
{
scanf("%lf%lf",&points[i].x,&points[i].y);
points[i].lable=0;
}
for(int i=N; i<N*2; i++)
{
scanf("%lf%lf",&points[i].x,&points[i].y);
points[i].lable=1;
}
sort(points,points+2*N);
printf("%.3f\n",getMiniDistance(0,2*N-1));
}
return 0;
}
Codeforces(429D - Tricky Function)近期点对问题的更多相关文章
- Codeforces 429D Tricky Function 近期点对
题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...
- Codeforces 429D Tricky Function(平面最近点对)
题目链接 Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ...
- Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对
D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...
- codeforce 429D. Tricky Function (思维暴力过)
题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...
- Codefoces 429 D. Tricky Function
裸的近期点对.... D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【codeforces 429D】Tricky Function
[题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...
- CodeForces - 598A Tricky Sum (数学,快速幂的运用)
传送门: http://codeforces.com/problemset/problem/598/A A. Tricky Sum time limit per test 1 second memor ...
- codeforces 598A Tricky Sum
题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...
随机推荐
- Perl语言入门:第六章习题:处理用户所指定的名字并汇报相应的姓。
37 print "\n----------------------------------_exercise_6_1--------------------------\n"; ...
- JAVA基础——对象流
对象的输入输出流的作用: 用于写入对象 的信息和读取对象的信息. 使得对象持久化. ObjectInputStream : 对象输入流 ObjectOutPutStream :对象输 ...
- CSS Paint API绘制透明格子背景实例页面
CSS代码: .box { width: 180px; height: 180px; background: paint(transparent-grid); } HTML代码: <div cl ...
- laydate组件选择时间段的判断
前言: 在使用laydate组件的时候,难免会遇到选择时间段,官网给的文档中有选择时间段的组件,但是并不好用,首先只能选择一个月的时间段,有局限性,其次精确到时间的话要先选日期范围再选时间范围,很变态 ...
- Zabbix微信告警
Zabbix微信告警 摘要 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信 ...
- 小DEMO之manifest初体验
前言 补漏洞系列~今天来动手体验一下HTML5中的离线应用之mainifest缓存清单.实际上H5还提供了一个JavaScript接口来用于更新缓存文件的方法以及对缓存文件的操作.在Chrome中,输 ...
- Python之FTP实现
Python之FTP实现 上传下载: import socket import struct import json import subprocess import os class MYTCPSe ...
- socket scoketserver
import socket sk = socket.socket() # 创建了一个socket对象 # sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSE ...
- 腾讯ISUX网站的一个小问题
腾讯isux网站的一个小问题. 它的网站:http://isux.tencent.com/?variant=zh-hans 优秀的网站和差的网站的距离往往就在于细节. 浏览环境:谷歌. ...
- xtu summer individual-4 D - Martian Strings
Martian Strings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...