hihoCoder 1596 : Beautiful Sequence
阿新 • • 發佈:2017-10-02
std sin set spa problems only 不能 ability font
Description
Consider a positive integer sequence a[1], ..., a[n] (n ≥ 3). If for every 2 ≤ i ≤ n-1, a[i-1] + a[i+1] ≥ 2 × a[i] holds, then we say this sequence is beautiful.
Now you have a positive integer sequence b[1], ..., b[n]. Please calculate the probability P that the resulting sequence is beautiful after uniformly random shuffling sequence b.
You‘re only required to output (P × (n!)) mod 1000000007. (Obviously P × (n!) is an integer)
Input
First line contains an integer n. (3 ≤ n ≤ 60)
Following n lines contain integers b[1], b[2], ..., b[n]. (1 ≤ b[i] ≤ 1000000000)
Output
Output (P × (n!)) mod 1000000007.
Sample Input
4 1 2 1 3
Sample Output
8
https://hihocoder.com/problemset/problem/1596
dp鬼題題目條件可化為a[i+1]-a[i]>=a[i]-a[i-1]
考慮排序再做分配
根據分析我們發現最後的高度序列是一個勾函數,先減小後增大
我們討論b[i-1],b[i],b[i+1]的情況
顯然i-1和i+1不可能同時大於i
只可能一個大於i一個小於,或兩個都大於
但是兩個都大於的情況顯然只有一次,兩邊是不會有的
如5 3 5 1 3 5
那麽就出現了兩個都小於的情況
那麽我們就可以dp
令f[i][j][k][l]表示最左邊兩個為i,j 最右邊兩個為k,l
我們先將最小值放入f[1][0][1][0]=1
接下來要放的數為max(i,k)+1,為什麽?因為是排過序的,從小到大放就行了
判斷是否滿足:a[i+1]-a[i]>=a[i]-a[i-1]
還有一個細節:當有多個最小值時,顯然不能直接dp,以最小值數量為l=3舉例
因為直接dp只能得到4種,而實際有6種(此題鬼處)
所以把所有最小值縮為一個,最後乘l!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 int Mod=1000000007; 8 long long f[61][61][61][61],a[61],p[61],tmp,ans; 9 int n; 10 int main() 11 {int i,j,k,l=0; 12 cin>>n; 13 for (i=1;i<=n;i++) 14 { 15 scanf("%lld",&a[i]); 16 } 17 sort(a+1,a+n+1); 18 p[0]=1; 19 for (i=1;i<=n;i++) 20 p[i]=(p[i-1]*i)%Mod; 21 for (i=1;i<=n;i++) 22 if (a[i]==a[1]) l++; 23 tmp=p[l]; 24 for (i=2;i<=n-l+1;i++) 25 a[i]=a[i+l-1]; 26 n=n-l+1; 27 f[1][0][1][0]=1; 28 for (i=1;i<=n;i++) 29 { 30 for (j=0;j<=n-1;j++) 31 { 32 for (k=1;k<=n;k++) 33 { 34 for (l=0;l<=n-1;l++) 35 { 36 int z=max(i,k)+1; 37 if (z==n+1) 38 { 39 ans+=f[i][j][k][l]; 40 ans%=Mod; 41 continue; 42 } 43 if (a[z]-a[k]>=a[k]-a[l]||l==0) 44 f[i][j][z][k]+=f[i][j][k][l],f[i][j][z][k]%=Mod; 45 if (a[z]-a[i]>=a[i]-a[j]||j==0) 46 f[z][i][k][l]+=f[i][j][k][l],f[z][i][k][l]%=Mod; 47 } 48 } 49 } 50 } 51 cout<<(ans*tmp)%Mod; 52 }
hihoCoder 1596 : Beautiful Sequence