【JZOJ3478】【NOIP2013模拟联考9】旅行者问题(tourist)
Limits:
Time Limits: 1000 ms Memory Limits: 256MB
Description
lahub是一个旅行者的粉丝,他想成为一个真正的旅行者,所以他计划开始一段旅行。lahub想去参观n个目的地(都在一条直道上)。lahub在起点开始他的旅行。第i个目的地和起点的距离为ai千米(ai为非负整数)。不存在两个目的地和起点的距离相同。
从第i个目的地走到第j个目的地所走的路程为 |ai-aj|千米。我们把参观n个目的地的顺序称作一次“旅行”。lahub可以参观他想要参观的任意顺序,但是每个目的地有且只能被参观一次(参观顺序为n的排列)。
lahub把所有可能的“旅行”都写在一张纸上,并且记下每个“旅行”所要走的路程。他对所有“旅行”的路程之和的平均值感兴趣。但是他觉得计算太枯燥了,所以就向你寻求帮助。
Input
第一行一个正整数n。
第二行n个非负整数a1,a2,....,an(1≤ai≤10^7)。
Output
两个整数,答案用最简分数形式输出,第一个为分子,第二个为分母。
Sample Input
3
2 3 5
Sample Output
22 3
【样例提示】
样例有6种可能的旅行:
[2, 3, 5]: 该“旅行”的路程:|2 – 0| + |3 – 2| + |5 – 3| = 5;
[2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
[3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
[3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
[5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
[5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
答案为 1/6 * (5+7+7+8+9+8)=44/6=22/3
Data Constraint
30% n<=10
50% n<=1000
100% n<=100000
考场上的思考
n<=10,枚举全排列即可过。
先打个暴力中的暴力:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <iomanip>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double db;
ll n,g,p[100005],to[100005],ans,C;
bool vis[100005];
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
void over(){
for(int i=1;i<=n;i++)
ans+=abs(p[i]-p[i-1]);
C++;
}
void dfs(int k){
if(k>n){
over();
return;
}
for(int i=1;i<=n;i++)
if(!vis[i]){
p[k]=to[i];
vis[i]=1;
dfs(k+1);
vis[i]=0;
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>to[i];
dfs(1);
g=gcd(ans,C);
ans/=g;
C/=g;
cout<<ans<<" "<<C;
return 0;
}
30pts到手。
既然是个数论,那就打表找规律吧!
明显每个数字本身只是一个因数,不妨设为自然数序列,之后把中间的路程输出来,利用瞪眼法。
打表程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <iomanip>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double db;
int n,p[100005],ans,C,way[100][100];
bool vis[100005];
void over(){
for(int i=1;i<=n;i++){
ans+=abs(p[i]-p[i-1]);
way[p[i]][p[i-1]]++;
way[p[i-1]][p[i]]++;
}
C++;
}
void dfs(int k){
if(k>n){
over();
return;
}
for(int i=1;i<=n;i++)
if(!vis[i]){
p[k]=i;
vis[i]=1;
dfs(k+1);
vis[i]=0;
}
}
int main(){
cin>>n;
dfs(1);
cout<<ans<<" "<<C<<endl;
for(int i=0;i<n;i++){
for(int j=i+1;j<=n;j++)
cout<<way[i][j]<<" ";
cout<<endl;
}
return 0;
}
Input:
3
Output:
28 6
2 2 2
4 4
4
Input:
4
Output:
180 24
6 6 6 6
12 12 12
12 12
12
Input:
5
Output:
1320 120
24 24 24 24 24
48 48 48 48
48 48 48
48 48
48
规律很明显了。
从0出发,到各个点的距离的倍数都是(n-1)!。
从一个非0点出发,到另一个非0点的倍数也是(n-1)!。
因为是一来一回,所以×2
时间从O(n!)降至O(n^2)。
50pts到手。
代码没写。
如何优化至O(n)?
从0出发的直接加起来。
另外的:
在纸上写:
∵(pos[2]-pos[1])+(pos[3]-pos[1])+……+(pos[n]-pos[1])
=Σ(pos[i])(2≤i≤n)-(n-1)pos[1]
依次类推
∴ans[x]=(pos[x+1]-pos[x])+(pos[x+2]-pos[x])+……+(pos[n]-pos[x])
=Σ(pos[i])(x+1≤i≤n)-(n-x)pos[x]
∴最终答案为
Σ(pos[x])(1≤x≤n)+2×Σ(ans[x])(1≤x<n)
=Σ(pos[x])(1≤x≤n)+2×(Σ(pos[i])(x+1≤i≤n)-(n-x)pos[x])
Σ(pos[i])可以直接预处理出来。
最后(n-1)!和n!约分只剩下n,和最终答案约分即可。
O(n)。100pts到手!
真的这么容易吗?
我们这么推,是因为默认序列是自然数序列。
也就是说这么做有一个前提:pos[i]必须是升序的!
加个sort,解决!
(我才不会说我忘了加sort让100pts变成0pts了)
AC代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <iomanip>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double db;
ll n,ans,p,a[100005],add[100005];
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+1,a+1+n);
for(int i=n;i>=1;i--)
add[i]=add[i+1]+a[i];
ans=add[1];
for(int i=2;i<=n;i++)
ans+=2*(add[i]-(n-i+1)*a[i-1]);
p=gcd(ans,n);
ans/=p;
n/=p;
printf("%lld %lld",ans,n);
return 0;
}
End.