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∥=sqrt(x12+⋯+xn2) where X=(x1,x2,...,xn)).
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∥=sqrt(x12+⋯+xn2) 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).
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
Sample Input 3 4 1 2 3 4 4 2 2 2 2 5 5 6 2 3 4 Sample Output 5/1 0/1 10/1
题意:
已知一种计算方式||X||=sqrt(x12+x22+...+xn2),现给出W的值,求||W-aB||的最小值,其中B只能取-1和1,a为缩放因子,其中a>=0。
思路:
实际上求(W1-ab1)2+(W2-ab2)2+...+(Wn-abn)2的最小值,将这个公式展开,经过化简可以得到(Wi的平方和)+na2-2a(Wi绝对值的和)
求出a的值,然后将其带入即可,相当于求一元二次方程的最小值。即a=b/2a。
最后注意分子、分母分开计算,最后GCD求最大公因数。GCD用long long。
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<math.h> using namespace std; long long gcd(long long a,long long b)//一定记得用longlong { if(b==0) return a; return gcd(b,a%b); } int main() { int t; cin>>t; while(t--) { int n; cin>>n; int a[100010]; long long sum1,sum; sum1=sum=0;//sum1为平方和,sum为和 for(int i=0; i<n; i++) { cin>>a[i]; if(a[i]<0)//一定取正值,因为b为-1或者+1,为了使最后结果最小,sum1应该最大 a[i]=-a[i]; sum1+=a[i]; sum+=(a[i]*a[i]); } /*接下来注意不能用b/2a 因为他不一定为整数,最后求得是分数,所以要将最后的公式化作分子分母形式*/ long long nb=n*sum-sum1*sum1 ; long long x; x=gcd(nb,n); cout<<nb/x<<"/"<<n/x<<endl; } return 0; }