GYM100078H Sol

· · 个人记录

题目大意

有 n 个球队参加了一场锦标赛。比赛的规则如下:

现在给定各个球队最终的分数,问比赛的得分情况有多少种可能。

此题需要文件输入输出。输入:history.in,输出:history.out。

数据范围:2 \le n \le 8。

时限:2s。

解法分析

这个数据范围很显然是搜索,枚举每一场比赛哪队赢了哪队输了就可以,但是算一下复杂度感觉非常的悬……

于是我们对代码使出浑身绝技:

然后有了这些操作我们就能卡到 1996ms 了……

代码

蒟蒻代码,1996ms

#include <iostream>
#include <algorithm>
using namespace std;
const int N=9;
int n,a[N],ans;
void dfs(int x,int y) {
    if (a[x]>(n-y+1)*3) return;
    if (y>n) {
        ++x,y=x+1;
        if (x==n) { ++ans; return; }
    }
    if (a[x]>=3) {
        a[x]-=3;
        if ((n-x-1)*3>=a[y]) dfs(x,y+1);
        a[x]+=3;
    }
    if (a[x]>=1&&a[y]>=1) {
        --a[x],--a[y];
        if ((n-x-1)*3>=a[y]) dfs(x,y+1);
        ++a[x],++a[y]; 
    }
    if (a[y]>=3) {
        a[y]-=3;
        if ((n-x-1)*3>=a[y]) dfs(x,y+1);
        a[y]+=3;
    }
}
int main() {
    freopen("history.in","r",stdin); freopen("history.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;++i) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    dfs(1,2);
    printf("%d\n",ans);
    return 0;
}

大佬代码 #001 by @lxy666(橙名),最短用时(之一),30ms

“之一”是因为有一个 15ms 的,看上去是用了奇奇怪怪的 STL。相对来说这份代码比较好理解,于是贴在了这里,但是绝对来说还是不好理解……

就是加上了一个哈希处理,然后在跟我的代码类似的 dfs 里面又套了一个带强力剪枝的 dfs 处理。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int n,p[9],sum;
int a,b;
int jc[11],bb[11];
map<long long,long long> mp[11];
int v[11];
int ans=0;
const int bas=28;
int dfss(int);
int dfs(int a,int b)
{
//  if (a==n+1) {++ans;return;}
    if (b==n+1)
    {
        if (p[a]!=0)return 0;
        else return dfss(a+1);
    }
    if (3*(n-b+1)<p[a]) return 0;
//  if (v[a]+3*(n-b+1)==p[a])
//  {
//      int t=v[a];
//      v[a]=p[a];
//      dfs(a+1,a+2);
//      v[a]=t;
//      return;
//  }
    int cnt=0;
    if (p[b]-3>=0)
    {
        p[b]-=3;
        cnt+=dfs(a,b+1);
        p[b]+=3;
    }
    if (p[a]-1>=0 && p[b]-1>=0)
    {
        p[b]-=1;p[a]-=1;
        cnt+=dfs(a,b+1);
        p[b]+=1;p[a]+=1;
    }
    if (p[a]-3>=0)
    {
        p[a]-=3;
        cnt+=dfs(a,b+1);
        p[a]+=3;
    }
    return cnt;
}
int dfss(int x)
{
    if (x==n) return (p[n]==0);
    int tot=0;
    for (int i=x;i<=n;i++)
        bb[++tot]=p[i];
    sort(bb+1,bb+1+tot);
    long long ha=0;
    for (int i=1;i<=tot;i++)
        ha=ha*bas+bb[i];
//  cout<<ha<<endl; 
    if (mp[x].find(ha)!=mp[x].end()) return mp[x][ha];
    else return mp[x][ha]=dfs(x,x+1);
}
int main()
{
    freopen("history.in","r",stdin);
    freopen("history.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        scanf("%d",&p[i]);
    sort(p+1,p+1+n);
    int ans=dfss(1);
    printf("%d",ans);
}

大佬代码 #002 by @colintian(灰名),最小码量,1902ms

这题代码比较短的基本都是在 1900ms 左右的。

#include <bits/stdc++.h>
using namespace std;
int a[10],s,n;
void dfs(int x,int y)
{
    if((n-y)*3<a[x])return;
    if(y>=n)
    {
        x++;
        y=x+1;
        if(x==n-1)
        {
            s++;
            return;
        }
    }
    if(a[x]>=3)
    {
        a[x]-=3;
        if((n-x-2)*3>=a[y])dfs(x,y+1);
        a[x]+=3;
    }
    if(a[x]>=1&&a[y]>=1)
    {
        a[x]--;
        a[y]--;
        if((n-x-2)*3>=a[y])dfs(x,y+1);
        a[x]++;
        a[y]++;
    }
    if(a[y]>=3)
    {
        a[y]-=3;
        if((n-x-2)*3>=a[y])dfs(x,y+1);
        a[y]+=3;
    }
}
int main()
{
    freopen("history.in","r",stdin);
    freopen("history.out","w",stdout);
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    dfs(0,1);
    cout<<s;
    return 0;
}