题目来源于牛客竞赛:https://ac.nowcoder.com/acm/contest/discuss
题目描述:
输入描述:
输出描述:
示例1:
题解:
代码:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
vector<int> fact;
vector<int> ifact;
vector<int> inv;
vector<int> pow2;
const int MOD = (1e9 + 7);
int add(int a, int b)
{
a+=b;
while(a>=MOD) a-=MOD;
return a;
}
int mult(int a, int b)
{
return (a*1LL*b)%MOD;
}
int modpow(int a, int b)
{
int r=1;
while(b)
{
if(b&1) r=mult(r,a);
a=mult(a,a);
b>>=1;
}
return r;
}
int choose(int a, int b)
{
if(a<b) return 0;
if(b==0) return 1;
if(a==b) return 1;
return mult(fact[a],mult(ifact[b],ifact[a-b]));
}
int inverse(int a)
{
return modpow(a,MOD-2);
}
void init(int _n)
{
fact.clear(); ifact.clear(); inv.clear(); pow2.clear();
fact.resize(_n+1);
ifact.resize(_n+1);
inv.resize(_n+1);
pow2.resize(_n+1);
pow2[0]=1;
ifact[0]=1;
fact[0]=1;
for(int i=1;i<=_n;i++)
{
pow2[i]=add(pow2[i-1],pow2[i-1]);
fact[i]=mult(fact[i-1],i);
//ifact[i]=mult(ifact[i-1],inv[i]);
}
ifact[_n] = inverse(fact[_n]);
for(int i=_n-1;i>=1;i--)
{
ifact[i] = mult(ifact[i + 1], i + 1);
}
for(int i=1;i<=_n;i++)
{
inv[i] = mult(fact[i-1],ifact[i]);
}
}
int cnt[13];
vi v;
vi perm;
void dfs(int u)
{
if(cnt[u]>=3) return ;
cnt[u]++;
v[perm[u]]=min(v[perm[u]],v[u]+1);
dfs(perm[u]);
}
int solve_naive(vi t)
{
int n=t.size();
for(int i=0;i<n;i++)
{
if(t[i]==-1) t[i]=int(1e9);
}
perm.resize(n);
set<vi> S;
for(int i=0;i<n;i++) perm[i]=i;
do
{
v=t;
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++)
{
if(cnt[i]==0) dfs(i);
}
sort(v.begin(),v.end());
for(int i=0;i<n;i++)
{
if(v[i]>=int(1e9)) v[i]=-1;
assert(v[i]<10000);
}
//for(int x:v) cerr<<x<<' ';
//cerr<<'\n';
S.insert(v);
}while(next_permutation(perm.begin(),perm.end()));
return S.size();
}
const int C = 200;
int dp[C+2][C+2][C+2]; //cur node, sum, last, # of bad
void radd(int &a, int b)
{
a=add(a,b);
}
int solve_fast(vi t)
{
memset(dp,0,sizeof(dp));
vi cnt(C,0);
vi pref(C,0);
int n=t.size();
for(int i=0;i<t.size();i++)
{
if(t[i]==-1) t[i]=C-1;
assert(t[i]>=0&&t[i]<C);
cnt[t[i]]++;
}
for(int i=1;i<C;i++) pref[i]=cnt[i]+pref[i-1];
dp[0][0][0]=1;
for(int i=0;i<C;i++)
{
for(int j=0;j<=n;j++)
{
for(int k=0;k<=n;k++)
{
if(dp[i][j][k]==0) continue;
int v=dp[i][j][k];
for(int c=0;j+c<=n;c++)
{
int realc = cnt[i+1];
int newbad=j-pref[i];
if(c>=realc)
{
//need to borrow from front
//we need c-realc more to hook with the front
int needed=c-realc;
if(k<needed) continue;
//exist >= needed to hook with
//# of bad person increases by needed
newbad+=needed;
}
else
{
//need to finally "use up the needed ones"
int extra = realc-c;
if(extra>newbad) continue;
//extra<=l
newbad-=extra;
}
radd(dp[i+1][j+c][c],v);
}
}
}
}
int ans=0;
for(int k=0;k<=n;k++)
{
radd(ans,dp[C-1][n][k]);
}
return ans;
}
void read_solve()
{
int n; cin>>n;
vi t(n);
for(int i=0;i<n;i++) cin>>t[i];
//int naive = solve_naive(t);
int fast = solve_fast(t);
cout<<fast<<'\n';
}
void gen_solve()
{
int n = rand()%8+1;
vi t(n);
int p=rand()%100+1;
int C=rand()%6+1;
for(int i=0;i<n;i++)
{
t[i]=rand()%C+1;
if(rand()%100<=p) t[i]=-1;
}
int naive = solve_naive(t);
cerr<<"NAIVE : "<<naive<<'\n';
int fast = solve_fast(t);
cerr<<"FAST : "<<fast<<'\n';
if(naive!=fast)
{
freopen("birthday-wishes.in","w",stdout);
cout<<n<<'\n';
for(int i=0;i<n;i++)
{
cout<<t[i]<<' ';
}
cout<<'\n';
exit(0);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
init(123333);
read_solve();
}
更多问题,更详细题解可关注牛客竞赛区,一个刷题、比赛、分享的社区。
传送门:https://ac.nowcoder.com/acm/contest/discuss