Acperience
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 120 Accepted Submission(s): 56
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
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
Author
zimpha
题目大意:
给你一个向量W=(W1,W2,W3.......)【wi的值取决于输入】,然后再给你一个向量B=(B1,B2,B3...........)【Bi的值要么是+1,要么是-1】,令W-αB得到的向量摸方值最小,问这个最小值,并且用分数表示(当然是要不可约分的数)。
思路:
1、这个题挺坑的,一开始如果想错了,那么每一步都想错了,而且这个题真的很容易把我们带入求平均值从而得到值α的路上去,很可惜,这个思路是错的。
2、观察式子以及推导过程:
①(W1-αB1)^2+(W2-αB2)^2+(W2-αB2)^2+..............
②将上述式子展开:n*α^2+Σ(i=1,i=n)【Wi*2】α+Σ(i=1,i=n)【Wi^2】;
③我们不妨将上式子看成一元二次方程组:aα^2+bα+c,其中a=n,b=Σ(i=1,i=n)【Wi*2】,c=Σ(i=1,i=n)【Wi^2】
④因为n是一定大于0的,所以这个抛物线在直角坐标系中一定是开口向上的,那么其最小值也一定在对称轴-b/2*a上, 那么我们将对称轴作为α的值带回原式。
⑤其解ans=4*a*c-b^2/4a
3、输出ans。注意负Wi
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll aa[101010];
ll gcd(ll x,ll y)
{
if(x%y==0)return y;
else return gcd(y,x%y);
}
ll abs(ll tmp)
{
if(tmp<0)return -tmp;
else return tmp;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
ll a,b,c;
a=n;
b=0;
c=0;
for(int i=0;i<n;i++)
{
scanf("%I64d",&aa[i]);
aa[i]=abs(aa[i]);
b+=2*aa[i];
c+=aa[i]*aa[i];
}
ll fenzi=4*a*c-b*b;
ll fenmu=4*a;
if(fenzi==0)printf("0/1\n");
else
printf("%I64d/%I64d\n",fenzi/gcd(fenzi,fenmu),fenmu/gcd(fenzi,fenmu));
}
}