CODE THANKS FESTIVAL 2018(Parallel)

Submission #11200749

Source codeソースコード

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

int n,k,a[5010],b[5010];
vector<int> G[5010];

vector<int> get_dp(vector<int> &v){
    int s=v.size();
    vector<vector<vector<vector<int>>>> dp(s+1,vector<vector<vector<int>>>(s+3,vector<vector<int>>(2,vector<int>(2,-mod))));
    dp[0][0][0][0]=0;
    rep(i,s-1){
        rep(j,s+1){
            rep(flagX,2){
                //cout << flagX << " " << (!flagX) << endl;
                rep(flagY,2){
                    dp[i+1][j+1][1][flagY]=max(dp[i+1][j+1][1][flagY],dp[i][j][flagX][flagY]+v[i+1]);
                    dp[i+1][j+(!flagX)][0][flagY || (i==0)]=max(dp[i+1][j+(!flagX)][0][flagY || (i==0)],dp[i][j][flagX][flagY]+v[i]);
                }
            }
        }
    }
    rep(j,s+1){
        rep(flagX,2){
            rep(flagY,2){
                dp[s][j+(!flagY)][1][flagY]=max(dp[s][j+(!flagY)][1][flagY],dp[s-1][j][flagX][flagY]+v[0]);
                dp[s][j+(!flagX)][0][flagY]=max(dp[s][j+(!flagX)][0][flagY],dp[s-1][j][flagX][flagY]+v[s-1]);
            }
        }
    }
    vector<int> res(s+1);
    rep(j,s+1){
        res[j]=max(max(dp[s][j][0][0],dp[s][j][0][1]),max(dp[s][j][1][0],dp[s][j][1][1]));
        //cout << j << " " << res[j] << endl;
    }
    return res;
}

bool visited[5010];

void solve(){
    cin >> n >> k;
    rep(i,n){
        cin >> a[i];
    }
    rep(i,n){
        cin >> b[i];
    }
    rep(i,n){
        G[a[i]].push_back(b[i]);
        G[b[i]].push_back(a[i]);
    }
    vector<vector<int>> DP(2,vector<int>(n+1,-mod));
    DP[0][0]=0;
    int m=0;
    Rep(i,1,n+1){
        if(visited[i]) continue;
        visited[i]=true;
        int par=i,nex=G[i][0];
        vector<int> v={i};
        while(!visited[nex]){
            //cout << par << " " << nex << endl;
            v.push_back(nex);
            visited[nex]=true;
            for(int t:G[nex]){
                if(t==par) continue;
                par=nex;
                nex=t;
                break;
            }
        }
        // for(int v_:v){
        //     cout << v_ << " ";
        // }
        //cout << "" << endl;
        vector<int> dp=get_dp(v);
        int l=dp.size();
        rep(j,n+1){
            DP[(m+1)%2][j]=-mod;
        }
        rep(j,n+1){
            rep(p,l){
                if(j+p>n) continue;
                DP[(m+1)%2][j+p]=max(DP[(m+1)%2][j+p],DP[m%2][j]+dp[p]);
            }
        }
        m+=1;
    }
    
    // rep(i,m+1){
    //     rep(j,n+1){
    //         cout << i << " " << j << " " << DP[i][j] << endl;
    //     }
    // }
    int ans=0;
    Rep(j,k,n+1){
        ans=max(ans,DP[m%2][j]);
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}

Submission

Task問題 G - Sum of Cards
User nameユーザ名 Chanyuh
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 TLE
Score得点 0
Source lengthソースコード長 3776 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Test case

Set

Set name Score得点 / Max score Cases
Sample - s1.txt,s2.txt,s3.txt
All 0 / 500 01.txt,02.txt,03.txt,04.txt,05.txt,06.txt,07.txt,08.txt,09.txt,10.txt,11.txt,12.txt,13.txt,14.txt,15.txt,16.txt,17.txt,18.txt,19.txt,20.txt,21.txt,22.txt,23.txt,24.txt,25.txt,26.txt,27.txt,28.txt,29.txt,30.txt,31.txt,32.txt,33.txt,34.txt,35.txt,36.txt,37.txt,38.txt,39.txt,40.txt,s1.txt,s2.txt,s3.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01.txt AC 1 ms 384 KB
02.txt AC 1 ms 384 KB
03.txt AC 1 ms 384 KB
04.txt AC 1 ms 384 KB
05.txt AC 1 ms 384 KB
06.txt AC 4 ms 1280 KB
07.txt AC 12 ms 7168 KB
08.txt AC 9 ms 4744 KB
09.txt AC 11 ms 7424 KB
10.txt AC 6 ms 3584 KB
11.txt AC 6 ms 2560 KB
12.txt AC 5 ms 2176 KB
13.txt AC 10 ms 7168 KB
14.txt AC 7 ms 4192 KB
15.txt AC 6 ms 3200 KB
16.txt TLE
17.txt AC 1444 ms 480280 KB
18.txt TLE
19.txt TLE
20.txt AC 1802 ms 576512 KB
21.txt TLE
22.txt TLE
23.txt TLE
24.txt AC 1802 ms 881920 KB
25.txt TLE
26.txt TLE
27.txt AC 1573 ms 533376 KB
28.txt AC 1096 ms 482924 KB
29.txt TLE
30.txt TLE
31.txt AC 43 ms 640 KB
32.txt AC 49 ms 640 KB
33.txt AC 55 ms 640 KB
34.txt AC 39 ms 640 KB
35.txt AC 54 ms 640 KB
36.txt TLE
37.txt TLE
38.txt AC 1397 ms 427904 KB
39.txt MLE
40.txt TLE
s1.txt AC 1 ms 384 KB
s2.txt AC 1 ms 384 KB
s3.txt AC 1 ms 384 KB