zoukankan      html  css  js  c++  java
  • Codeforces(429D

    D. Tricky Function
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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?

    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 test(s)
    input
    4
    1 0 0 -1
    
    output
    1
    
    input
    2
    1 -1
    
    output
    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
    ",getMiniDistance(0,2*N-1));
        }
        return 0;
    }
    


  • 相关阅读:
    约合¥1720 LG法国称G Watch将于6月开卖
    c语言中的位移位操作
    兼容的动态加载JS【原】
    Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g
    面向对象思想----不看懊悔!
    学习设计模式--观察者模式(C++)
    使用POI 导入excel
    机器学习 F1-Score, recall, precision
    阿里无线11.11:手机淘宝移动端接入网关基础架构演进之路
    ICE 迁移64位安装问题
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7123496.html
Copyright © 2011-2022 走看看