HDU 5734 A - Acperience

http://acm.hdu.edu.cn/showproblem.php?pid=5734 
Problem Description 
Deep neural networks (DNN) have shown significant improvements in several application domains including computer vision and speech recognition. In computer vision, a particular type of DNN, known as Convolutional Neural Networks (CNN), have demonstrated state-of-the-art results in object recognition and detection.

Convolutional neural networks show reliable results on object recognition and detection that are useful in real world applications. Concurrent to the recent progress in recognition, interesting advancements have been happening in virtual reality (VR by Oculus), augmented reality (AR by HoloLens), and smart wearable devices. Putting these two pieces together, we argue that it is the right time to equip smart portable devices with the power of state-of-the-art recognition systems. However, CNN-based recognition systems need large amounts of memory and computational power. While they perform well on expensive, GPU-based machines, they are often unsuitable for smaller devices like cell phones and embedded electronics.

In order to simplify the networks, Professor Zhang tries to introduce simple, efficient, and accurate approximations to CNNs by binarizing the weights. Professor Zhang needs your help.

More specifically, you are given a weighted vector W=(w1,w2,…,wn). Professor Zhang would like to find a binary vector B=(b1,b2,…,bn) (bi∈{+1,−1}) and a scaling factor α≥0 in such a manner that ∥W−αB∥2 is minimum.

Note that ∥⋅∥ denotes the Euclidean norm (i.e. ∥X∥=x21+?+x2n−−−−−−−−−−√, where X=(x1,x2,…,xn)).

Input 
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integers n (1≤n≤100000) – the length of the vector. The next line contains n integers: w1,w2,…,wn (−10000≤wi≤10000).

Output 
For each test case, output the minimum value of ∥W−αB∥2 as an irreducible fraction “p/q” where p, q are integers, q>0.

Sample Input 


1 2 3 4 

2 2 2 2 

5 6 2 3 4

Sample Output 
5/1 
0/1 
10/1

Author 
zimpha

题目,给你一堆数字,要求你选定一个数,然后要使得这堆数字中每个数减去/加上这个数字后,剩下的数的平方和最小。

首先把公式展开,得到的是w^2 + a^2*B^2 - 2a*B*w

然后要使这个式子值最小,就要减号那部分的东西最大,那么因为w中有负数而且B的值只能是-1和+1,那么B就用来修正w的符号,使得w全是正数。然后根据一个关于a的二次函数,在对称轴上取得最小 。sum2是w的abs相加

最小值是 sum1(数字的平方和) - (sum2)*(sum2)/n

因为有可能sum2*sum2是不能整除n的,那么把整个式子乘上一个n,最后输出的时候和n的gcd约去即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
LL gcd (LL n,LL m)
{
    if (n%m==0) return m;
    else return gcd(m,n%m);
}
void work ()
{
    int n;
    scanf("%d",&n);
    LL sum1 = 0,sum2 = 0;
    for (int i=1;i<=n;++i)
    {
        LL x;
        scanf("%I64d",&x);
        sum1 += x*x;
        sum2 += abs(x);
    }
    LL ansn = n*sum1 - (sum2*sum2);
    LL GCD = gcd(ansn,1LL*n);
    printf ("%I64d/%I64d\n",ansn/GCD,n/GCD);
    return ;
}
int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    int t;
    scanf("%d",&t);
    while (t--) work();
    return 0;
}

时间: 2024-11-08 17:43:43

HDU 5734 A - Acperience的相关文章

hdu 5734 Acperience(2016多校第二场)

Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 484    Accepted Submission(s): 258 Problem Description Deep neural networks (DNN) have shown significant improvements in several applic

HDU 5734 Acperience

化简之后发现会是一个一元二次方程,对称轴大于0,所以把对称轴代入计算即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<ios

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

题目:传送门. #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int gcd(long long a,long long b) { if(!b) return a; return gcd(b,a%b); } int a[100005]; int main() { int T,n; scanf("%d

HDU 5734 Acperience(数学推导)

Problem Description Deep neural networks (DNN) have shown significant improvements in several application domains including computer vision and speech recognition. In computer vision, a particular type of DNN, known as Convolutional Neural Networks (

2016 Multi-University Training Contest 2题解报告

A - Acperience HDU - 5734 题意: 给你一个加权向量,需要我们找到一个二进制向量和一个比例因子α,使得|W-αB|的平方最小,而B的取值为+1,-1,我们首先可以想到α为输入数据的平均值,考虑到是平方和,然后化简表达式,可以得到一个化简的式子,用n通分,可以做到没有除法,然后分子分母化简到互质. #define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstring> #include<iom

多校2

A HDU 5734 t 组样例 n  n个数字 w[i]    b 是1或者 -1 求 min   1~n  sum(w[i]-a*b)^2 展开  1~n sum(w[i]^2) + a*a*n- 2*a*(w[1]*b1+w[2]*b2 ...); a>0  二次函数 a变量     最小  a= (w[1]*b1...)/n; 然后 求和  求一下gcd 就行  long long #include<stdio.h> #include<algorithm> #incl

[HDOJ5734]Acperience(数学,公式推导)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5734 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorith

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include