Codeforces Round #245(Div. 1) 429D - Tricky Function 최근 포인트 쌍

11249 단어 codeforces
D. Tricky Function
Time Limit: 20 Sec
Memory Limit: 256 MB
제목 연결
codeforces.com/problemset/problem/429/D
Description
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?
Input
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
Output a single integer — the value of mini ≠ j  f(i, j).
Sample Input
41 0 0 -1
Sample Output
1
HINT
 
제목의 뜻
n개 드릴게요. 가장 작은 f(i, j)를 구하라고요.
f(i,j)=(j-i)^2+(sum[j]-sum[i])^2
그 중에서sum는 접두사와
문제 풀이:
간단하게 분석해 보면 두 제곱이면 거리잖아
모든 점을 (i,sum[i])로 바꾸고 가장 가까운 점을 찾으면 맞다. 그리고 우리는 몇 가지 방법이 있다.
1. 과학적 폭력에 가지치기
2. 최근 질문
3. 각 점에 대해 2분 nlogn을 진행하는 것은 매우 과학적이라고 느낀다
코드
 
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

struct Point
{
    ll x;
    ll y;
}point[maxn];
int n;
int tmpt[maxn];

bool cmpxy(const Point& a, const Point& b)
{
    if (a.x != b.x)
        return a.x < b.x;
    return a.y < b.y;
}

bool cmpy(const int& a, const int& b)
{
    return point[a].y < point[b].y;
}

ll dis2(int i, int j)
{
    return (point[i].x - point[j].x) * (point[i].x - point[j].x)
            + (point[i].y - point[j].y) * (point[i].y - point[j].y);
}

ll sqr(ll x)
{
    return x * x;
}

ll Closest_Pair(int left, int right)
{
    ll d = infll;
    if (left == right)
        return d;
    if (left + 1 == right)
        return dis2(left, right);
    int mid = (left + right) >> 1;
    ll d1 = Closest_Pair(left, mid);
    ll d2 = Closest_Pair(mid + 1, right);
    d = min(d1, d2);
    int i, j, k = 0;
    //      d   
    for (i = left; i <= right; i++) {
        if (sqr(point[mid].x - point[i].x) <= d)
            tmpt[k++] = i;
    }
    sort(tmpt, tmpt + k, cmpy);
    //    
    for (i = 0; i < k; i++) {
        for (j = i + 1; j < k && sqr(point[tmpt[j]].y - point[tmpt[i]].y) < d;
                j++) {
            ll d3 = dis2(tmpt[i], tmpt[j]);
            if (d > d3)
                d = d3;
        }
    }
    return d;
}

int main()
{
    scanf("%d", &n);
    ll sum = 0;
    for (int i = 0; i < n; ++i)
    {
        int x;
        scanf("%d", &x);
        point[i].x = i;
        sum += x;
        point[i].y = sum;
    }
    cout << Closest_Pair(0, n - 1) << endl;
    return 0;
}

좋은 웹페이지 즐겨찾기