QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#650560#9475. Pangu and Stonesucup-team5071#TL 0ms3584kbC++202.5kb2024-10-18 15:36:122024-10-18 15:36:12

Judging History

This is the latest submission verdict.

  • [2024-10-18 15:36:12]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 3584kb
  • [2024-10-18 15:36:12]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long 
void gmin(int &x,int y){
    if(y==-1)return;
    if(x==-1)x=y;
    else if(y<x)x=y;
};
int solve(int n,int L,int R,vector<int> a){
    assert(n>=2&&n<=100);
    assert(L<=R&&L>=1&&R<=n);

    vector<vector<int>> dp(n+1,vector<int>(n+1,-2));
    sort(a.begin()+1,a.end());
    auto sum=a;
    for(int i=1;i<=n;i++)sum[i]+=sum[i-1];
    function<int(int,int)> dfs = [&](int x,int y){
        if(dp[x][y]!=-2)return dp[x][y];
        if(x==y){
            return 0ll;
        }
        dp[x][y]=-1;
        for(int i=0;i<x;i++){
            for(int j=(x-i)*L;j<=min((x-i)*R,y-i);j++){
                int v=dfs(j,y-i);
                if(v!=-1)gmin(dp[x][y],v+sum[y-i]);
            }
        }
        // cout<<"x="<<x<<" y="<<y<<" dp="<<dp[x][y]<<endl;
        return dp[x][y];
    };
    int v=dfs(1,n);
    if(v!=-1)assert(v>0);
    if(v==-1)return 0;
    else return v;
}

map<vector<int>,int> ma;
int dfs(int n,int L,int R,vector<int> a) {
    assert(n == a.size());
    if (n == 1) return 0ll;
    sort(a.begin(),a.end());
    if(ma.find(a)!=ma.end())return ma[a];
    if (L > a.size()) return -1;
    int ret = -1;
    for (int i = L; i <= R; ++i){
        if (i > a.size()) break;
        auto nxta = a;
        int add = accumulate(nxta.begin(),nxta.begin()+i,0ll);
        nxta.erase(nxta.begin(),nxta.begin()+i);
        nxta.push_back(add);
        int v = dfs(n-i+1,L,R,nxta);
        if (v != -1){
            gmin(ret,v+add);
        }
    }
    return ma[a]=ret;
}

int solve2(int n,int L,int R,vector<int> a) {
    a.erase(a.begin());
    sort(a.begin(),a.end());
    int x = dfs(n,L,R,a);
    if (x == -1) return 0;
    else return x;
}

signed main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    int n,L,R;
    while(cin>>n>>L>>R){
        vector<int> a(n+1);
        ma.clear();
        for(int i=1;i<=n;i++)cin>>a[i];
        cout<<solve2(n,L,R,a)<<endl;
    }
    // srand(time(NULL));
    // for(int tt=1;tt<=100000;tt++){
    //     int n=10,L=rand()%(n-2)+2,R=rand()%(n-2)+2;
    //     if(L>R)swap(L,R);
    //     vector<int>a(n+1);
    //     for(int i=1;i<=n;i++)a[i]=rand()%10+1;
    //     cout<<"n="<<n<<" L="<<L<<" R="<<R<<endl;
    //     for(int i=1;i<=n;i++)cout<<a[i]<<" ";;cout<<endl;
    //     int u=solve(n,L,R,a),v=solve2(n,L,R,a);
    //     cout<<"my="<<u<<" std="<<v<<endl;
    //     assert(u==v);
    // }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

input:

3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4

output:

9
6
0

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

input:

100 4 7
570 608 194 26 243 470 418 119 1000 936 440 302 797 155 676 283 869 60 959 793 158 397 808 656 379 316 485 854 753 280 543 435 756 822 106 561 402 347 99 739 8 682 834 549 812 32 338 765 699 575 575 785 171 504 335 113 284 612 276 518 835 677 865 900 687 48 859 179 343 318 626 812 523 11 400...

output:


result: