QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#767741#8034. Ban or Pick, What's the Trickzwu2021016689TL 1928ms197392kbC++172.4kb2024-11-20 21:59:122024-11-20 21:59:12

Judging History

This is the latest submission verdict.

  • [2024-11-20 21:59:12]
  • Judged
  • Verdict: TL
  • Time: 1928ms
  • Memory: 197392kb
  • [2024-11-20 21:59:12]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

#define ull unsigned long long
const int P=131;

inline ull get(array<int,5>a){
	ull ans=0;
	ans+=a[0]*P*P*P*P+a[1]*P*P*P+a[2]*P*P+a[3]*P+a[4];
	return ans;
}

int main() {
    int N,K;
    cin >> N >> K;
    vector<vector<vector<vector<i64>>>> dp(2,vector<vector<vector<i64>>>(2,vector<vector<i64>>(N+1,vector<i64>(K+1))));
    vector a(2,vector<i64>(N+1));
    for ( int i = 0 ; i < 2 ; i++ ) {
        for ( int j = 1 ; j <= N ; j++ ) {
            cin >> a[i][j];
        }
    }
    sort(a[0].begin(),a[0].end());
    sort(a[1].begin(),a[1].end());
    for ( int i = 0 ; i < 2 ; i++ ) {
        for ( int j = 1 ; j <= N ; j++ ) {
            for ( int k = 1 ; k <= K ; k++ ) {
                dp[i][0][j][k] = ((j >= 2)?dp[i][0][j-2][k]:0)+a[i][j-1]-( (j-1-2*k >= 0) ? a[i][j-1-2*k]:0 ); 
                dp[i][1][j][k] = ((j >= 2)?dp[i][1][j-2][k]:0)+a[i][j]-( (j-2*k >= 0) ? a[i][j-2*k]:0 );
            }
        }
    }
    unordered_map<ull,i64>mp;
    unordered_map<ull,i64>vis;
    auto dfs = [&](auto&dfs,int A,int B,int kA,int kB,int cur)->i64{
        if ( B == 0 ) {
            int t = cur==0;
            return dp[0][t][A][kA];
        }
        if ( A == 0 ) {
            int t = cur==1;
            return -dp[1][t][B][kB];
        }
        if ( kB == 0 ) {
            int t = cur==0;
            return dp[0][t][A][kA];
        }
        if ( kA == 0 ) {
            int t = cur==1;
            return -dp[1][t][B][kB];
        }
        // return 0;
        array<int,5>U = {A,B,kA,kB,cur};
        if ( vis[get(U)] ) {
            return mp[get(U)];
        } 
        i64 res;
        if ( cur == 0 ) {
            res = -1e18;
            if ( A >= 1 )  res = max(dfs(dfs,A-1,B,kA-1,kB,cur^1)+a[0][A],res);
            if ( B >= 1 )  res = max(dfs(dfs,A,B-1,kA,kB,cur^1),res);
        } else {
            res = 1e18;
            if ( B >= 1 )  res = min(dfs(dfs,A,B-1,kA,kB-1,cur^1)-a[1][B],res);
            if ( A >= 1 )  res = min(dfs(dfs,A-1,B,kA,kB,cur^1),res);
        }
        mp[get({A,B,kA,kB,cur})] = res;
        vis[get({A,B,kA,kB,cur})] = 1;
        // cout << A << ' ' << B << ' ' << kA << ' ' << kB << ' ' << ' ' << cur << ' ' << res << '\n';
        return res;
    };
    // cout << 1 << '\n';
    cout << dfs(dfs,N,N,K,K,0) << '\n';
}

詳細信息

Test #1:

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

input:

2 1
3 6
2 4

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3808kb

input:

4 1
1 3 5 7
2 4 6 8

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3784kb

input:

4 2
4 6 7 9
2 5 8 10

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3592kb

input:

10 5
42 13 60 42 100 82 22 98 14 55
100 41 89 24 65 38 69 26 37 16

output:

41

result:

ok single line: '41'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3856kb

input:

9 10
4 32 566 885 439 479 332 95 432
409 140 704 26 558 781 457 356 404

output:

58

result:

ok single line: '58'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3764kb

input:

20 8
249 888 548 338 840 890 519 416 852 817 28 694 271 947 239 11 654 914 765 939
483 148 403 552 250 635 287 644 364 822 621 151 31 422 683 959 867 266 837 395

output:

1201

result:

ok single line: '1201'

Test #7:

score: 0
Accepted
time: 0ms
memory: 4700kb

input:

100 10
9813 7151 1299 6277 9002 7359 770 1582 3909 9113 1937 3436 846 9841 3378 5437 9443 9148 6862 6832 1910 1365 1328 26 7245 9468 6087 5828 8993 3378 6200 7242 6495 6783 7275 9837 5162 2140 3721 1586 7559 3721 9268 7186 1615 6648 5631 5752 3712 7856 7938 5414 9197 5259 3319 3616 3257 5880 2448 74...

output:

2174

result:

ok single line: '2174'

Test #8:

score: 0
Accepted
time: 1928ms
memory: 197392kb

input:

40000 5
19274618 29347041 40087005 4241769 41812959 94623872 30100813 76644033 64979494 96963240 48832843 15280489 52008818 47041532 58608992 73517436 15625815 32168369 68934604 89208811 79580765 42310673 69912065 59918072 29961600 96203768 8736038 83397821 69327027 17923674 3664021 44005027 7651388...

output:

8194

result:

ok single line: '8194'

Test #9:

score: -100
Time Limit Exceeded

input:

100000 10
85360465 81541239 77118024 87341741 99983425 10619868 1784917 20823381 74104953 35366414 80917288 72494315 44512062 20610367 84530471 73577105 48782904 9308066 74657628 58835630 19344370 85521394 91509750 31058798 76011614 78384876 52633286 93465152 62148686 46231760 69339331 74105686 4770...

output:


result: