地址


看数据范围很明显的搜索题,暴力dfs是枚举按顺序每一场比赛的胜败情况到底,合法就累计。$O(3^{n*(n-1)/2})$。n到10的时候比较大,考虑剪枝。

本人比较菜所以关键性的剪枝没想出来,但由于数据较水,20个点就T了2个。依旧记下剪枝方案,没想到的用下划线和红笔标注:

  • 剪枝一:个人觉得应该将数组从小到大排序来搜,这样感觉起来状态少一些。实测有时和从大到小相差不大,但有时可能有效。
  • 剪枝二:如果当前选手就算赢了所有剩下的场也到不了分数,剪掉。弱剪枝。
  • 剪枝三:如果剩下所有场全是决出胜负的也没办法将分数累到总的要求得分,剪掉;就算全平局也超过了要求的总得分,剪掉。基于这种剪枝和上面一样效果不大,没写上去。
  • 剪枝四:当一个选手和后面选手的比赛枚举完毕时看他分数够不够,不够就不往下搜了。
  • 剪枝五:记忆化。由于每个队得分数字都不太大,可以将一个选手和后面选手全比完时剩下的没有互相比的几个队看成一个状态。即剩下的几个队顺序如何不重要,重要的是要看他们还差多少分满足条件。比如最后4个队分别还差3,2,4,3分,另一种情况剩下四个队还差2,3,3,4分,两种是等效的,可以将其排序后用hash记录下来,当以后遇到相同转态直接返回。
  • 剪枝六:通过方程求解相关量:设决出胜负的比赛x场,打平的比赛y场,总的分数为s,则有方程组$①3x+2y=s$   $②x+y=\frac{n(n-1)}{2}$ 然后可确定具体的胜负或打平比赛有多少场,然后带入剪枝。

然后是对于记忆化的几点补充,这个坑死我了。

  • 哈希可以用拖链表方法,这次不想写了,直接上裸的hash。注意初始时清-1,因为可能是会存0这样的值的
  • 记忆化看似每次很耗时间,到搜索终点就要线性算一下hash。但是发现本质相同的状态只有几千个左右,很多时候我发现状态相同了直接返回记忆化的值,会把原来可能的指数级转换为线性的,所以总体是飞快的。
  • 最坑的地方:关于hash值的计算。由于分数中存在0,所以算hash值的时候会有疏漏。比如1,0,1和1,1.而这两种答案是不一样的。所以要强制每个数都加上一点点。

WA记录:(非常重要)注意hash计算过程中出现数字0的情况!

BZOJ1306 [CQOI2009]match循环赛

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define ddbg(x,y) cerr<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
using namespace std;
typedef long long ll;
template<typename T>inline char MIN(T&A,T B){return A>B?A=B,:;}
template<typename T>inline char MAX(T&A,T B){return A<B?A=B,:;}
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const ll P=,base=;
int n,T,sx,sy;
int rest[],tmp[],H[P],sum;
inline int dfs(int i,int j){//ddbg(i,j),dbg(ans);
if(i==n)return !rest[i];
if(j>n){
if(rest[i])return ;
for(register int k=i+;k<=n;++k)tmp[k]=rest[k];
sort(tmp+i+,tmp+n+);int ha=;
for(register int k=i+;k<=n;++k)ha=(ha*base+tmp[k]+)%P;//¡ï¡ï¡ï¡ï¡ï
if(~H[ha])return H[ha];
return H[ha]=dfs(i+,i+);
}
if(*(n-j+)<rest[i])return ;
int ret=;
if(rest[i]>=)rest[i]-=,ret+=dfs(i,j+),rest[i]+=;
if(rest[i]&&rest[j])--rest[i],--rest[j],ret+=dfs(i,j+),++rest[i],++rest[j];
if(rest[j]>=)rest[j]-=,ret+=dfs(i,j+),rest[j]+=;
return ret;
} int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
memset(H,-,sizeof H);
read(n);for(register int i=;i<=n;++i)sum+=read(rest[i]);
sort(rest+,rest+n+);printf("%d\n",dfs(,));
return ;
}

BZOJ3139 [Hnoi2013]比赛

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define ddbg(x,y) cerr<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
using namespace std;
typedef long long ll;
template<typename T>inline char MIN(T&A,T B){return A>B?A=B,:;}
template<typename T>inline char MAX(T&A,T B){return A<B?A=B,:;}
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const ll P=,base=,MOD=1e9+;
int n,T,sx,sy;
int rest[],tmp[],H[P],sum,tot,flag;
inline ll dfs(int i,int j){//ddbg(i,j),dbg(ans);
if(i==n)return !rest[i];
if(j>n){
if(rest[i])return ;
for(register int k=i+;k<=n;++k)tmp[k]=rest[k];
sort(tmp+i+,tmp+n+);ll ha=;
for(register int k=i+;k<=n;++k)ha=(ha*base+tmp[k]+)%P;
if(~H[ha])return H[ha];
return H[ha]=dfs(i+,i+);
}
if(*(n-j+)<rest[i])return ;
ll ret=;
if(rest[i]>=&&sx)rest[i]-=,--sx,ret+=dfs(i,j+),rest[i]+=,++sx;
if(rest[i]&&rest[j]&&sy)--rest[i],--rest[j],--sy,ret+=dfs(i,j+),++rest[i],++rest[j],++sy;
if(rest[j]>=&&sx)rest[j]-=,--sx,ret+=dfs(i,j+),rest[j]+=,++sx;
return ret%MOD;
} int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
memset(H,-,sizeof H);
read(n);for(register int i=;i<=n;++i)sum+=read(rest[i]);
sx=sum-n*n+n,sy=*(n-)*n/-sum;
sort(rest+,rest+n+);
return printf("%lld\n",dfs(,)),;
}
05-08 15:08