zoukankan      html  css  js  c++  java
  • hdu 5461 Largest Point 暴力

    Largest Point

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=5461

    Description

    Given the sequence A with n integers t1,t2,⋯,tn. Given the integral coefficients a and b. The fact that select two elements ti and tj of A and i≠j to maximize the value of at2i+btj, becomes the largest point.

    Input

    An positive integer T, indicating there are T test cases.
    For each test case, the first line contains three integers corresponding to n (2≤n≤5×106), a (0≤|a|≤106) and b (0≤|b|≤106). The second line contains n integers t1,t2,⋯,tn where 0≤|ti|≤106 for 1≤i≤n.

    The sum of n for all cases would not be larger than 5×106.

    Output

    The output contains exactly T lines.
    For each test case, you should output the maximum value of at2i+btj.

    Sample Input

    2

    3 2 1
    1 2 3

    5 -1 0
    -3 -3 0 3 3

    Sample Output

    Case #1: 20
    Case #2: 0

    HINT

    题意

    给你a,b,再给你n个数

    然后让你求ati*ti+btj最大值是多少

    题解:

    我们是暴力做的,找到最大的两个数,最小的两个数,绝对值最大的两个数,绝对值最小的两个数

    然后扔进一个vector里面,然后去重,然后暴力枚举的

    但是还是怕tle,就分治了一下解法,数据小的话就直接n^2暴力枚举= =

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <stack>
    #include <map>
    #include <set>
    #include <queue>
    #include <iomanip>
    #include <string>
    #include <ctime>
    #include <list>
    #include <bitset>
    typedef unsigned char byte;
    #define pb push_back
    #define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
    #define local freopen("in.txt","r",stdin)
    #define pi acos(-1)
    
    using namespace std;
    const int maxn = 5e6 + 500;
    long long  a , b  , ans , p[maxn];
    int vis[11] , used[maxn] , n , sz;
    vector<long long>s;
    
    struct data
    {
        long long val;
        int idx;
    };
    
    data A[maxn] , B[maxn];
    
    
    
    bool cmp1(const data & x,const data & y)
    {
        return x.val < y.val;
    }
    
    bool cmp2(const data & x,const data & y)
    {
        return abs(x.val) < abs(y.val);
    }
    
    
    void initiation()
    {
        scanf("%d%I64d%I64d",&n,&a,&b);
        memset(used,0,sizeof(int)*(n+2));
        for(int i = 0 ; i < n ; ++ i)
        {
            scanf("%I64d",&A[i].val);
            A[i].idx = i;
            B[i].val = A[i].val;
            B[i].idx = i;
            p[i] = A[i].val;
        }
        s.clear();
        ans = -(1LL<<58);
    }
    
    void dfs(int cur , long long check)
    {
        if(cur == 2) ans = max( ans , check);
        else
        {
            for(int i = 0 ; i < sz ; ++ i) 
            {
                if(!vis[i])
                {
                    vis[i] = 1;
                    if(cur == 0) dfs(cur + 1 , check + a * s[i]*s[i] );
                    else dfs(cur + 1 , check + b*s[i]);
                    vis[i] = 0;
                }
            }
        }
    }
    
    
    
    long long solve()
    {
        sort(A,A+n,cmp1);
        sort(B,B+n,cmp2);
        used[A[0].idx] = 1 , used[A[1].idx] = 1 , used[A[n-1].idx] = 1 , used[A[n-2].idx] = 1;
        used[B[0].idx] = 1 , used[B[1].idx] = 1 ; used[B[n-1].idx] = 1 , used[B[n-2].idx] = 1;
        for(int i = 0 ; i < n ; ++ i) if(used[i]) s.push_back(p[i]);
        sz = s.size();
        memset(vis,0,sizeof(vis));
        dfs(0,0);
        return ans;
    }
    
    long long solve2()
    {
        for(int i=0;i<n;i++)
         {
            for(int j=0;j<n;j++)
                {
                    if(i==j) continue;
                    ans = max(ans,A[i].val*A[i].val*a+b*A[j].val);
                }
            }
         return ans;
    }
    
    int main(int argc,char *argv[])
    {
        int Case;
        scanf("%d",&Case);
        for(int cas = 1 ; cas <= Case ; ++ cas)
        {
            initiation();
            printf("Case #%d: ",cas);
            if(n<=70) printf("%I64d
    ",solve2());
            else printf("%I64d
    ",solve());
        }
        return 0;
    }
  • 相关阅读:
    SQL Server解惑——查询条件IN中能否使用变量
    依赖注入高级玩法——注入接口服务的多个实现类
    最纯净的硬件检测工具箱
    批处理体会hosts文件
    针对m3u8视频的ts文件解密
    HLS协议之m3u8和ts流格式详解
    您的主机不满足在启用Hyper-V或Device/Credential Guard的情况下运行VMwareWorkstation的最低要求
    FFmpeg的安装和使用
    如何下载 blob 地址的视频资源
    Win10系统创建WiFi热点的两种方法
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4822519.html
Copyright © 2011-2022 走看看