TLE 81pts 求助
  • 板块学术版
  • 楼主Nephren_Sakura
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/5/19 09:56
  • 上次更新2023/10/28 01:08:51
查看原帖
TLE 81pts 求助
400783
Nephren_Sakura楼主2022/5/19 09:56
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int n=read(),a[1000005],sum,shu,len,cnt,nxt[1000005];
bool vis[1000005],f;
void dfs(int nw_id,int last_id,int nw_len){
    if(f==true)
        return;
    if(nw_len==0){
        if(nw_id==shu){
            f=true;
            return;
        }
        int tmp;
        for(int i=1; i<=n; i++)
            if(vis[i]==false){
                tmp=i;
                break;
            }
        vis[tmp]=true;
        dfs(nw_id+1,tmp,len-a[tmp]);
        vis[tmp]=false;
        if(f==true)
            return;
    }
    int lt=last_id-1,rt=n+1;
    while(lt+1<rt){
        int mid=lt+rt>>1;
        if(a[mid]<=nw_len)
            rt=mid;
        else
            lt=mid;
    }
    for(int i=lt; i<=n; i++){
        if(vis[i]==false){
            vis[i]=true;
            dfs(nw_id,i,nw_len-a[i]);
            vis[i]=false;
            if(f==true)
                return;
            if(a[i]==nw_len||len==nw_len)
                return;
            i=nxt[i];
        }
    }
    return;
}
bool cmp(int x,int y){
    return x>y;
}
signed main(){
    for(int i=1; i<=n; i++){
        int x=read();
        if(x<=50){
            a[++cnt]=x;
            sum+=a[cnt];
        }
    }
    n=cnt;
    sort(a+1,a+n+1,cmp);
    nxt[n]=n;
    for(int i=n-1; i>=1; i--){
        if(a[i]==a[i+1])
            nxt[i]=nxt[i+1];
        else
            nxt[i]=i;
    }
    vis[1]=true;
    for(len=1; len<=sum/2; len++){
        if(sum%len)
            continue;
        shu=sum/len;
        f=false;
        dfs(1,1,len-a[1]);
        if(f==true){
            cout<<len;
            return 0;
        }
    }
    cout<<sum;
    return 0;
}
2022/5/19 09:56
加载中...