[HihoCoder1596]Beautiful Sequence
题目大意:
\(n(n\le60)\)个数\(A_{1\sim n}\),将这些数随机打乱,问最后构成的数列满足对于所有的\(2\le i\le n-1\),都有\(2A_i\le A_{i-1}+A_{i+1}\)的概率。
思路:
显然题目要求的是构成下凸函数的概率。
将所有数排序,考虑最小值在中间,往两遍加数。
\(f[i][j][k][l]\)表示左边第一个数是\(i\),左边第二个数是\(j\),右边第一个数是\(k\),右边第二个数是\(l\)的方案数。对于每个状态,枚举新增的数放左边或右边即可。
注意特殊处理一开始的边界情况。
时间复杂度\(\mathcal O(n^4)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=61,mod=1e9+7;
int a[N],f[N][N][N][N];
int main() {
int n=getint();
for(register int i=1;i<=n;i++) a[i]=getint();
std::sort(&a[1],&a[n]+1);
int cnt=0;
for(register int i=1;i<=n&&a[i]==a[1];i++) cnt++;
for(register int i=cnt;i<=n;i++) a[i-cnt+1]=a[i];
a[0]=a[1];
n-=cnt-1;
for(register int i=f[1][0][1][0]=1;i<=cnt;i++) {
f[1][0][1][0]=(int64)f[1][0][1][0]*i%mod;
}
for(register int i=1;i<n;i++) {
for(register int j=0;j<i;j++) {
for(register int k=1;k<n;k++) {
for(register int l=0;l<k;l++) {
const int t=std::max(i,k)+1;
if(a[i]*2<=a[t]+a[j]) {
(f[t][i][k][l]+=f[i][j][k][l])%=mod;
}
if(a[k]*2<=a[t]+a[l]) {
(f[i][j][t][k]+=f[i][j][k][l])%=mod;
}
}
}
}
}
int ans=0;
for(register int i=0;i<=n;i++) {
for(register int j=0;j<=n;j++) {
for(register int k=0;k<=n;k++) {
for(register int l=0;l<=n;l++) {
if(i==n||j==n||k==n||l==n) {
(ans+=f[i][j][k][l])%=mod;
}
}
}
}
}
printf("%d\n",ans);
return 0;
}
[HihoCoder1596]Beautiful Sequence的更多相关文章
- Beautiful Sequence
Beautiful Sequence 给定一些数(可能相同),将它们随机打乱后构成凹函数,求概率 .N<=60 . 首先,这种题求概率事实上就是求方案.所以现在要求的是用这些数构成凹函数的方案数 ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- hihoCoder 1596 : Beautiful Sequence
Description Consider a positive integer sequence a[1], ..., a[n] (n ≥ 3). If for every 2 ≤ i ≤ n-1, ...
- Solution -「Gym 102956B」Beautiful Sequence Unraveling
\(\mathcal{Description}\) Link. 求长度为 \(n\),值域为 \([1,m]\) 的整数序列 \(\lang a_n\rang\) 的个数,满足 \(\not\ ...
- CodeForces 544A
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concat ...
- cf 403 D
D. Beautiful Pairs of Numbers time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- CF Set of Strings
Set of Strings time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- F - Set of Strings
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concat ...
- Codeforces Round #302 (Div. 2) A. Set of Strings 水题
A. Set of Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/pr ...
随机推荐
- JS简介——(一)
0.结构
- 编写pl/sql时,报错
/* 写一个简单的PL/SQL */ declare a ; b ; c number; begin c:=(a+b)/(a-b); dbms_output.put_line(c); exceptio ...
- aarch64_m3
mrpt-stereo-camera-calibration-1.4.0-1.fc26.aarch64.rpm 2017-03-17 10:02 143K fedora Mirroring Proje ...
- http请求中的中文乱码问题
通过浏览器访问服务器页面和资源时,不可避免地要传送中文字串,如果客户机与服务器不能用同一码表解析字串,肯定会出现各种各样的乱码问题.我总结了几个乱码场景及解决办法,如下 1.服务器上的中文字串被客户端 ...
- 20 Organizing Go code 组织go代码
Organizing Go code 16 August 2012 Introduction Go code is organized differently to that of other lan ...
- L1和L2特征的适用场景
How to decide which regularization (L1 or L2) to use? Is there collinearity among some features? L2 ...
- python随笔(二)
range(2,10):不包括10 range(2,10,3):步长为3 range(10,2,-1):从10到2,步长-1.
- 嵌入式 探讨父子线程、进程终止顺序不同产生的结果_skdkjxy_新浪博客
嵌入式 探讨父子线程.进程终止顺序不同产生的结果 Linux下编程,线程.进程退出顺序问题纷纷扰扰,如果父进程/线程先于子进程/线程终止,系统会做什么处理呢?反之,如果子进程/线程先于父进程/线 程终 ...
- 【直播预告】云栖直播:阿里热修复产品HotFix2.0升级详解
全面——你知道吗?1891年,卡尔森纳做出的第一把瑞士军刀,只有螺丝刀和开罐器.经过一代又一代能工巧匠的打磨,这把刀陆续增加了锯子.剪刀.镊子.放大镜.改锥,甚至内藏激光.LED手电筒.USB记忆碟等 ...
- How to tell your iPhone application that location services are required | The Agile Warrior
div{padding-bottom:10px}.b_vPanel>div:last-child{padding:0}.banner a{color:#1020d0} --> Below ...