CODE THANKS FESTIVAL 2018(Parallel)

Submission #11201314

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],dp[5010][5010][2][2];
vector<int> G[5010];

vector<int> get_dp(vector<int> &v){
    int s=v.size();
    rep(i,s+1){
        rep(j,s+3){
            rep(flagX,2){
                rep(flagY,2){
                    dp[i][j][flagX][flagY]=-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;
        //cout << i << endl;
        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();
        //cout << l << endl;
        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状態 AC
Score得点 500
Source lengthソースコード長 3929 Byte
File nameファイル名
Exec time実行時間 429 ms
Memory usageメモリ使用量 375296 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - s1.txt,s2.txt,s3.txt
All 500 / 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 512 KB
04.txt AC 1 ms 384 KB
05.txt AC 1 ms 384 KB
06.txt AC 2 ms 4608 KB
07.txt AC 5 ms 17024 KB
08.txt AC 5 ms 12928 KB
09.txt AC 5 ms 17024 KB
10.txt AC 4 ms 10752 KB
11.txt AC 3 ms 8704 KB
12.txt AC 3 ms 8704 KB
13.txt AC 5 ms 17024 KB
14.txt AC 4 ms 12800 KB
15.txt AC 4 ms 10752 KB
16.txt AC 301 ms 307328 KB
17.txt AC 139 ms 140672 KB
18.txt AC 216 ms 231296 KB
19.txt AC 230 ms 251776 KB
20.txt AC 173 ms 155136 KB
21.txt AC 257 ms 270336 KB
22.txt AC 341 ms 329984 KB
23.txt AC 429 ms 375296 KB
24.txt AC 169 ms 192128 KB
25.txt AC 319 ms 315648 KB
26.txt AC 364 ms 338304 KB
27.txt AC 155 ms 148864 KB
28.txt AC 119 ms 140672 KB
29.txt AC 365 ms 340352 KB
30.txt AC 364 ms 338304 KB
31.txt AC 37 ms 640 KB
32.txt AC 42 ms 640 KB
33.txt AC 48 ms 640 KB
34.txt AC 33 ms 640 KB
35.txt AC 47 ms 640 KB
36.txt AC 223 ms 204544 KB
37.txt AC 236 ms 258048 KB
38.txt AC 140 ms 132480 KB
39.txt AC 181 ms 208640 KB
40.txt AC 229 ms 231296 KB
s1.txt AC 1 ms 384 KB
s2.txt AC 1 ms 384 KB
s3.txt AC 1 ms 384 KB