MYF

HDU 5734 Acperience

题目链接

HDU 5734

解题方法:数学猜想

题目分析

题目大意

给定一些数字,选择一个数字$α$使∑(ai+bi*α)最小,其中bi为1或-1

解析

为了让这个值最小,从直觉上觉得很像求方差的感觉,所以当时做题的时候就直接猜想α的值为所有数字的绝对值的和的平均值,事实上也是这样的。证明(<-点我直达)

做题的时候一直卡在了数据范围,该提前gcd的时候一定要提前gcd呀。。。要么就把公式推导的优雅一点。

代码

ac代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <deque>
#include <algorithm>
#define Memset(a,val) memset(a,val,sizeof(a))
#define PI acos(-1)
#define rt(n) (i == n ? '\n' : ' ')
#define hi printf("Hi----------\n")
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<<"Debug : ---"<<x<<"---"<<endl;
#define mp make_pair
#define pb push_back
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int maxn=100000+5;
const int mod=1000000007;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
ll a[maxn];
int main(){
int t;
scanf("%d",&t);
while (t--) {
ll n;
scanf("%lld",&n);
ll one=0,two=0;
for (int i=0; i<n; i++) {
scanf("%lld",&a[i]);
one+=fabs(a[i]);
two+=a[i]*a[i];
}
ll up=0,down=n,fenzi=one,fenmu=n;
if (fenzi!=0) {
ll gcd = __gcd(fenzi,fenmu);
fenzi/=gcd;
fenmu/=gcd;
}

ll gcd = __gcd(fenmu,n);

up = fenmu*fenmu/gcd*two -2*fenmu/gcd*one*fenzi+n/gcd*fenzi*fenzi;
down = fenmu * fenmu/gcd;//0

if (up==0) {
cout<<"0/1"<<endl;
}
else{
ll gcd = __gcd(up,down);
cout<<up/gcd<<"/"<<down/gcd<<endl;
}
}
}

更优雅的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <deque>
#include <algorithm>
#define Memset(a,val) memset(a,val,sizeof(a))
#define PI acos(-1)
#define rt(n) (i == n ? '\n' : ' ')
#define hi printf("Hi----------\n")
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<<"Debug : ---"<<x<<"---"<<endl;
#define mp make_pair
#define pb push_back
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int maxn=100000+5;
const int mod=1000000007;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
ll a[maxn];
int main(){
int t;
scanf("%d",&t);
while (t--) {
ll n;
scanf("%lld",&n);
ll one=0,two=0;
for (int i=0; i<n; i++) {
scanf("%lld",&a[i]);
one+=abs(a[i]);
two+=a[i]*a[i];
}
ll up=0,down=n,fenzi=one,fenmu=n;


up = two *n-one*one;
down = n;//0


if (up==0) {
cout<<"0/1"<<endl;
}
else{
ll gcd = __gcd(up,down);
cout<<up/gcd<<"/"<<down/gcd<<endl;
}
}
}'