AGC 020 F Arcs on a Circle 题解
AGC 020 F Arcs on a Circle 题解
算法: 暴力枚举,dp
考虑端点只在整点的情况,可以破环为链,然后bitmask dp。
不在整点的时候只需要枚举大小关系的全排列即可。
code:
/*
{
######################
# Author #
# Gary #
# 2021 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int x=0;
// char ch=getchar();
// while(ch<'0'||ch>'9'){
// ch=getchar();
// }
// while(ch>='0'&&ch<='9'){
// x=(x<<1)+(x<<3)+(ch^48);
// ch=getchar();
// }
// return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
const int MAXN=6;
double rest=0;
int n,c,l[MAXN],p[MAXN],id[MAXN];
double dp[51][1<<MAXN][MAXN][51];
bool cmp(int A,int B){
return p[A]<p[B];
}
int main(){
scanf("%d%d",&n,&c);
rep(i,n) scanf("%d",&l[i]);
sort(l,l+n);
int tmpl=l[n-1];
--n;
rep(i,n) p[i]=i;
p[n]=-1;
do{
memset(dp,0,sizeof(dp));
dp[0][0][n][tmpl]=1;
rep(i,n) id[i]=i;
sort(id,id+n,cmp);
rb(i,0,c-1){
rep(pt_,n){
int pt=id[pt_];
rep(mask,1<<n){
rep(j,n+1){
rep(k,c+1){
if(i>k) continue;
if(dp[i][mask][j][k]>0){
if((mask>>pt)&1);
else{
if(i==k&&(p[j]<p[pt])) continue;
int to=min(c,i+l[pt]);
if(II(to,p[pt])>II(k,p[j])){
dp[i][mask|(1<<pt)][pt][to]+=dp[i][mask][j][k];
}
else{
dp[i][mask|(1<<pt)][j][k]+=dp[i][mask][j][k];
}
}
}
}
}
}
}
rep(mask,1<<n){
rep(j,n+1){
rep(k,c+1){
if(i>k) continue;
if(dp[i][mask][j][k]>0){
dp[i+1][mask][j][k]+=dp[i][mask][j][k];
}
}
}
}
}
rb(j,0,n){
rest+=dp[c][(1<<n)-1][j][c];
}
}while(next_permutation(p,p+n));
rb(i,1,n) rest/=i,rest/=c;
printf("%.16f\n",rest);
return 0;
}

浙公网安备 33010602011771号