2016 Multi-University Training Contest 2 Acperience(一直WA的小伙伴可以看看)

来源:互联网 发布:淘宝信用贷提前还款 编辑:程序博客网 时间:2024/04/27 20:20

Acperience

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 730 Accepted Submission(s): 188

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
题意就是给你一个向量w,让你找一个同样长度二元向量b(每个元素只能取1或-1),做w-b,然后把运算之后的向量中的每个元素平方求和,问如何使结果最小。
运算完展开一下就会发现下面这个东西:
这里写图片描述
这里有一点需要说明如果wi是正数那么bi一定取-1,反之bi就取1,因为我们的目标是要wi-alpha*bi和0最接近,很显然这样需要这么取。这样一来上面的那个公式中所有的量就都知道了,那么这就是一个关于阿尔法的一元二次方程,然后二次函数取个极值就搞定了。
再一个就是转换成分数。这里巨坑。。做分数运算的时候需要先约分在进行计算,不然会爆long long,在这里卡了很久。注意一下这个问题应该就OK了。

#include "cstring"#include "iostream"#include "string.h"#include "cmath"using namespace std;struct node{    long long a,b;};long long gcd(long long a,long long b){    if(a%b==0)return b;    else return gcd(b,a%b);}node gao(node a){    long long p=gcd(a.a,a.b);    a.a/=p;    a.b/=p;    if(a.b<0){        a.a=-a.a;        a.b=-a.b;    }    return a;}node add(node a,node b){    node c;    long long p1=gcd(a.b,b.b);    c.a=b.b/p1*a.a+a.b/p1*b.a;    c.b=a.b/p1*b.b;    c=gao(c);    return c;}node mul(node a,node b){    long long pp=gcd(a.a,b.b);    a.a/=pp;    b.b/=pp;    pp=gcd(b.a,a.b);    b.a/=pp;    a.b/=pp;    node c;    c.a=a.a*b.a;    c.b=a.b*b.b;    gao(c);    return c;}int main(){    int cas;    scanf("%d",&cas);    while(cas--)    {        int n;        scanf("%d",&n);        int w[100005];        long long c=0;        long long b=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&w[i]);            c+=w[i]*w[i];            if(w[i]>0)                b+=w[i]*(-1);            else                b+=w[i];        }        b*=2;        node alpha;        alpha.a=-b;        alpha.b=2*n;        node cnode;        cnode.a=c;        cnode.b=1;        node bnode;        bnode.a=b;        bnode.b=1;        node anode;        anode.a=n;        anode.b=1;        node ans=add(cnode,mul(bnode,alpha));        node temp=mul(anode,alpha);        temp=mul(temp,alpha);        ans=add(ans,temp);        //Frac ans=c+b*alpha+a*alpha*alpha;        ans=gao(ans);        printf("%lld/%lld\n",ans.a,ans.b);    }}
0 0
原创粉丝点击