题目链接

题意 : 给出 n 维向量 W、要你构造一个 n 维向量 B = ( b1、b2、b3 ..... ) ( bi ∈ { +1, -1 } ) 、然后求出对于一个常数 α > 0 使得 || W - αB ||^2 尽量小

分析 :

将 || W - αB || ^ 2 进行化简、如下

未知数是 α

不难看出这是一个一元二次方程 Ax^2 + Bx + C

而根据实际的贪心选择

当 wi > 0 时、有 bi < 0

当 wi < 0 时、有 bi > 0

那么上述方程的 A、B、C 都可以确定并求出且 A > 0

那么根据公式法、此方程有最小值 (4AC - B^2) / (4A)

直接求就行了

#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long

#define scl(i) scanf("%lld", &i)
#define scll(i, j) scanf("%lld %lld", &i, &j)
#define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
#define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)

#define scs(i) scanf("%s", i)
#define sci(i) scanf("%d", &i)
#define scd(i) scanf("%lf", &i)
#define scIl(i) scanf("%I64d", &i)
#define scii(i, j) scanf("%d %d", &i, &j)
#define scdd(i, j) scanf("%lf %lf", &i, &j)
#define scIll(i, j) scanf("%I64d %I64d", &i, &j)
#define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
#define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
#define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
#define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
#define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
#define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(i) (i & (-i))
#define mem(i, j) memset(i, j, sizeof(i))

#define fir first
#define sec second
#define VI vector<int>
#define ins(i) insert(i)
#define pb(i) push_back(i)
#define pii pair<int, int>
#define VL vector<long long>
#define mk(i, j) make_pair(i, j)
#define all(i) i.begin(), i.end()
#define pll pair<long long, long long>

#define _TIME 0
#define _INPUT 0
#define _OUTPUT 0
clock_t START, END;
void __stTIME();
void __enTIME();
void __IOPUT();
using namespace std;

;

LL w[maxn], b[maxn];
int n;

int main(void){__stTIME();__IOPUT();

    printf("%d", b);

    int nCase;
    sci(nCase);
    while(nCase--){
        sci(n);

        ; i<=n; i++){
            scl(w[i]);
            ) b[i] = 1LL;
            else b[i] = - 1LL;
        }

        LL C = ;
        ; i<=n; i++)
            C += (w[i] * w[i]);

        LL B = ;
        ; i<=n; i++)
            B += (w[i] * b[i]);
        B *= (- 2LL);

        LL A = ;
        ; i<=n; i++)
            A += (b[i] * b[i]);

        LL p = ( * A * C - B * B);
        LL q =  * A;

        LL GCD = __gcd(p, q);

        p /= GCD;
        q /= GCD;

        printf("%lld/%lld\n", p, q);
    }

__enTIME();;}

void __stTIME()
{
    #if _TIME
        START = clock();
    #endif
}

void __enTIME()
{
    #if _TIME
        END = clock();
        cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
    #endif
}

void __IOPUT()
{
    #if _INPUT
        freopen("in.txt", "r", stdin);
    #endif
    #if _OUTPUT
        freopen("out.txt", "w", stdout);
    #endif
}

HDU 5734 Acperience ( 数学公式推导、一元二次方程 )的更多相关文章

  1. HDU 5734 Acperience(数学推导)

    Problem Description Deep neural networks (DNN) have shown significant improvements in several applic ...

  2. HDU 5734 Acperience(返虚入浑)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. HDU 5734 Acperience (公式推导) 2016杭电多校联合第二场

    题目:传送门. #include <iostream> #include <algorithm> #include <cstdio> #include <cs ...

  4. HDU 5734 Acperience (推导)

    Acperience 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5734 Description Deep neural networks (DN ...

  5. hdu 5734 Acperience 水题

    Acperience 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5734 Description Deep neural networks (DN ...

  6. HDU 5734 Acperience

    Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. hdu 5734 Acperience(2016多校第二场)

    Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. HDU 2092 (将表达式变成一元二次方程形式)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2092 整数解 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  9. Python数学运算的一个小算法(求一元二次方程的实根)

    请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax² + bx + c = 0的两个解. #!/usr/bin/env python # -*- coding: ...

随机推荐

  1. docker-compose搭建elasticsearch+kibana环境,以及php使用elasticsearch

    一.elasticsearch的Dockerfile 增加中文搜索插件analysis-ik FROM docker.elastic.co/elasticsearch/elasticsearch:7. ...

  2. Python多线程VS多进程

  3. spring-data-redis 2.0 的使用

    在使用Spring Boot2.x运行Redis时,发现百度不到顺手的文档,搞通后发现其实这个过程非常简单和简洁,觉得有必要拿出来分享一下. Spring Boot2.x 不再使用Jedis,换成了L ...

  4. 第二章、http协议及嗅探抓包--http协议详解

    初识http协议    hypertext trandfer protocol 超文本传输协议,是一种分布式,合作式,多媒体信息系统服务,面向应用层的协议.使用最广泛的应用层协议,基于传输层的TCP协 ...

  5. Codeforces 1097E. Egor and an RPG game

    传送门 首先考虑怎么算 $f(n)$ (就是题目里面那个 $f(n)$) 发现可以构造一组序列大概长这样: ${1,3,2,6,5,4,10,9,8,7,15,14,13,12,11,...,n(n+ ...

  6. 面试官:Kafka 如何优化内存缓冲机制造成的频繁 GC 问题?

    Jusfr 原创,转载请注明来自博客园 Request 与 Response 的响应格式 Request 与 Response 都是以 长度+内容 形式描述, 见于 A Guide To The Ka ...

  7. CNN 笔记

    1. 卷积后的图像的大小为    (w+2p-f)*3 / s    W为图像的宽,p为padding的大小, f为卷积核大小, 3 为图像的通道数, s为步长 2. 卷积层和池化层的区别? 卷积层是 ...

  8. vue学习(7)-路由抽离

    cnpm i vue-router -S

  9. CAFFE(三):Ubuntu下Caffe框架安装(仅仅Caffe框架安装)

    步骤一. 从github上下载(克隆)安装包 1.1 在你要安装的路径下 clone 此处我直接安装到home目录,执行: ~$ cd ~ 2 :~$ git clone https://github ...

  10. python-----操作路径

    分离路径.文件名 org_path = r"F:\temp\test" fpath, fname = os.path.split(org_path) print(fpath,fna ...