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;
    }
    


  • 相关阅读:
    遥感影像数据产品级别概述
    数据传输中的成帧
    帧同步
    I、Q信号是如何产生的,I、Q信号复用的作用
    调制解调技术
    基带信号、载波信号和宽带信号
    浅谈MVP架构及开发模式
    剑指offer代码 vs2013执行
    进程的状态
    死锁的必要条件及避免
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7123496.html
Copyright © 2011-2022 走看看