zoukankan      html  css  js  c++  java
  • codeforce 429D. Tricky Function (思维暴力过)

    题目描述

    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?

     

    给你一个长为n的序列a

    定义f(i,j)=(i-j)2+g(i,j)2

    g是这样的一个函数

    求最小的f(i,j)的值,i!=j

    输入描述

    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).
    第一行一个数n
    之后一行n个数表示序列a

    输出描述

    Output a single integer — the value of mini ≠ j  f(i, j).
    输出一行一个数表示答案

    输入

    4
    1 0 0 -1

    输出

    1

    思路:

    简直是好题,就是一个暴力法,不过只需要暴力1100个数就行了。(不过最正确的解法是最近点对)

    因为题目给定 - 104 ≤ a[i] ≤ 104,当i和j的差值超过1100的话 (i-j)2就已经超过108,所以此时f(i,j)的值一定会比当i,j差值为1的大(i,j差值为1,最大结果为1+108)。所以直接暴力1100个数就可以了。

    顺便记得开long long

    代码:

    #include <bits/stdc++.h>
    
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    
    
    #define IO ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    
    typedef long long LL;
    const long long INF = 0x3f3f3f3f;
    const long long mod = 1e9+7;
    const double PI = acos(-1.0);
    const int maxn = 100000;
    const char week[7][10]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    const char month[12][10]= {"Janurary","February","March","April","May","June","July",
                               "August","September","October","November","December"
                              };
    const int daym[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    const int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    const int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
    
    using namespace std;
    LL a[1100000];
    LL sum[110000];
    int main()
    {
        IO;
        int n;
        cin>>n;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
            sum[i]=sum[i-1]+a[i];
        }
        LL minn=2*INF;
        for(int i=1; i<=n; i++)
            for(int j=i+1; j<=i+1100&&j<=n; j++)
            {
                LL ans=(i-j)*(i-j)+(sum[i]-sum[j])*(sum[i]-sum[j]);
                minn=min(ans,minn);
            }
        cout<<minn;
        return 0;
    }
  • 相关阅读:
    【转】Hibernate入门实例
    【J2EE】Java连接SQL Server 2000问题:“com.microsoft.sqlserver.jdbc.SQLServerException:用户'sa'登录失败。该用户与可信SQL Server连接无关联”
    【转】Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
    linux命令行下的ftp 多文件下载和目录下载(转)
    Ubuntu下部署java JDK和eclipse IDE
    构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)
    构建第一个Spring Boot2.0应用之集成mybatis(六)
    构建第一个Spring Boot2.0应用之Controller(三)
    linux修改系统时间为北京时间(CentOS)
    构建第一个Spring Boot2.0应用之application.properties和application.yml(八)
  • 原文地址:https://www.cnblogs.com/aiguona/p/8361904.html
Copyright © 2011-2022 走看看