提出 #69548129


ソースコード 拡げる

#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
#define mkp make_pair
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;

int T,n,m;
vector<vector<ll> >calc(vector<ll>a) {
    vector<vector<ll> >ans(m,vector<ll>(2,inf));
    for(int i=0;i<m;i++) {
        if(i>0) ans[i][0]=ans[i-1][0],ans[i][1]=ans[i-1][1];
        if(a[i]<ans[i][0]) {
            ans[i][1]=ans[i][0];
            ans[i][0]=a[i];
        }
        else if(a[i]<ans[i][1]) ans[i][1]=a[i];
    }
    return ans;
}

tuple<ll,ll,ll>gen(vector<ll>a,vector<ll>b) {
    auto pre1=calc(a),pre2=calc(b);
    reverse(a.begin(),a.end()); reverse(b.begin(),b.end());
    auto suf1=calc(a),suf2=calc(b);
    reverse(suf1.begin(),suf1.end());
    reverse(suf2.begin(),suf2.end());
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());
    ll l=inf,mid=inf,r=inf;
    for(int i=0;i<m;i++) {
        ll wl=inf,wr=inf;
        if(i>0&&i<m-1) {
            wl=min(wl,pre1[i-1][0]+suf1[i+1][0]);
            wr=min(wr,pre2[i-1][0]+suf2[i+1][0]);
        }
        if(i>0) {
            wl=min(wl,a[i]+pre1[i-1][0]);
            wr=min(wr,b[i]+pre2[i-1][0]);
        }
        if(i<m-1) {
            wl=min(wl,a[i]+suf1[i+1][0]);
            wr=min(wr,b[i]+suf2[i+1][0]);
        }
        mid=min(mid,wl+wr);
        if(i>1&&i<m-1) {
            mid=min(mid,a[i]+suf1[i+1][0]+b[i-1]+pre2[i-2][0]);
            mid=min(mid,b[i]+suf2[i+1][0]+a[i-1]+pre1[i-2][0]);
        }
    }
    for(int i=1;i<m-1;i++) {
        l=min(l,pre1[i-1][0]+pre1[i-1][1]+suf2[i+1][0]+suf2[i+1][1]);
        r=min(r,pre2[i-1][0]+pre2[i-1][1]+suf1[i+1][0]+suf1[i+1][1]);
    }
    return make_tuple(l,mid,r);
} 
ll brute(vector<ll>a,int cnt) {
    if(cnt>m) return inf;
    sort(a.begin(),a.end());
    ll ans=0;
    for(int i=0;i<cnt;i++) ans+=a[i];
    return ans;
}
ll solve() {
    m=n-2;
    ll ans=inf;
    vector<ll>U(m),D(m),L(m),R(m);
    for(int i=0;i<m;i++) scanf("%lld",&U[i]);
    for(int i=0;i<m;i++) scanf("%lld",&D[i]);
    for(int i=0;i<m;i++) scanf("%lld",&L[i]);
    for(int i=0;i<m;i++) scanf("%lld",&R[i]);
    auto s1=gen(U,D),s2=gen(L,R);
    vector<ll>f(3),g(3);
    auto [w1,w2,w3]=s1;
    f[0]=w1; f[1]=w2; f[2]=w3;
    auto [ww1,ww2,ww3]=s2;
    g[0]=ww1; g[1]=ww2; g[2]=ww3;
    for(int i=0;i<3;i++) {
        for(int j=0;j<3;j++) {
            if(i==0&&j==2||i==2&&j==0) continue;
            ans=min(ans,f[i]+g[j]);
        }
    }
    ans=min(ans,brute(U,3)+brute(D,3)+brute(L,2)+brute(R,2));
    ans=min(ans,brute(L,3)+brute(R,3)+brute(U,2)+brute(D,2));
    return ans;
}

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        printf("%lld\n",solve());
    }
}

提出情報

提出日時
問題 E - Rectangle Coloring
ユーザ cjh_hhz
言語 C++ 20 (gcc 12.2)
得点 800
コード長 2772 Byte
結果 AC
実行時間 64 ms
メモリ 16996 KiB

コンパイルエラー

Main.cpp: In function ‘ll solve()’:
Main.cpp:80:20: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
   80 |             if(i==0&&j==2||i==2&&j==0) continue;
      |                ~~~~^~~~~~
Main.cpp:68:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |     for(int i=0;i<m;i++) scanf("%lld",&U[i]);
      |                          ~~~~~^~~~~~~~~~~~~~
Main.cpp:69:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   69 |     for(int i=0;i<m;i++) scanf("%lld",&D[i]);
      |                          ~~~~~^~~~~~~~~~~~~~
Main.cpp:70:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |     for(int i=0;i<m;i++) scanf("%lld",&L[i]);
      |                          ~~~~~^~~~~~~~~~~~~~
Main.cpp:71:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   71 |     for(int i=0;i<m;i++) scanf("%lld",&R[i]);
      |                          ~~~~~^~~~~~~~~~~~~~
Main.cpp: In function ‘int main()’:
Main.cpp:90:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   90 |     scanf("%d",&T);
      |     ~~~~~^~~~~~~~~
Main.cpp:92:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   92 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 800 / 800
結果
AC × 1
AC × 71
セット名 テストケース
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 02_max_00.txt, 02_max_01.txt, 02_max_02.txt, 02_max_03.txt, 02_max_04.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt, 03_handmade_05.txt, 03_handmade_06.txt, 03_handmade_07.txt, 03_handmade_08.txt, 03_handmade_09.txt, 03_handmade_10.txt, 03_handmade_11.txt, 03_handmade_12.txt, 03_handmade_13.txt, 03_handmade_14.txt, 03_handmade_15.txt, 03_handmade_16.txt, 03_handmade_17.txt, 03_handmade_18.txt, 03_handmade_19.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3684 KiB
01_random_00.txt AC 61 ms 12932 KiB
01_random_01.txt AC 62 ms 14872 KiB
01_random_02.txt AC 60 ms 12332 KiB
01_random_03.txt AC 58 ms 10116 KiB
01_random_04.txt AC 56 ms 8080 KiB
01_random_05.txt AC 58 ms 9560 KiB
01_random_06.txt AC 57 ms 9032 KiB
01_random_07.txt AC 60 ms 12068 KiB
01_random_08.txt AC 63 ms 15652 KiB
01_random_09.txt AC 63 ms 15772 KiB
01_random_10.txt AC 60 ms 12704 KiB
01_random_11.txt AC 64 ms 16744 KiB
01_random_12.txt AC 61 ms 13604 KiB
01_random_13.txt AC 63 ms 16152 KiB
01_random_14.txt AC 63 ms 15792 KiB
01_random_15.txt AC 33 ms 3688 KiB
01_random_16.txt AC 33 ms 3648 KiB
01_random_17.txt AC 33 ms 3816 KiB
01_random_18.txt AC 33 ms 3884 KiB
01_random_19.txt AC 32 ms 3640 KiB
01_random_20.txt AC 33 ms 3788 KiB
01_random_21.txt AC 32 ms 3824 KiB
01_random_22.txt AC 33 ms 3824 KiB
01_random_23.txt AC 33 ms 3768 KiB
01_random_24.txt AC 33 ms 3748 KiB
01_random_25.txt AC 33 ms 3900 KiB
01_random_26.txt AC 33 ms 3888 KiB
01_random_27.txt AC 33 ms 3764 KiB
01_random_28.txt AC 33 ms 3884 KiB
01_random_29.txt AC 33 ms 3888 KiB
01_random_30.txt AC 29 ms 3756 KiB
01_random_31.txt AC 29 ms 3760 KiB
01_random_32.txt AC 29 ms 3748 KiB
01_random_33.txt AC 29 ms 3684 KiB
01_random_34.txt AC 29 ms 3720 KiB
01_random_35.txt AC 29 ms 3684 KiB
01_random_36.txt AC 29 ms 3640 KiB
01_random_37.txt AC 29 ms 3684 KiB
01_random_38.txt AC 29 ms 3816 KiB
01_random_39.txt AC 30 ms 3636 KiB
01_random_40.txt AC 30 ms 3800 KiB
01_random_41.txt AC 30 ms 3884 KiB
01_random_42.txt AC 29 ms 3680 KiB
01_random_43.txt AC 29 ms 3812 KiB
01_random_44.txt AC 29 ms 3548 KiB
02_max_00.txt AC 63 ms 16916 KiB
02_max_01.txt AC 40 ms 16920 KiB
02_max_02.txt AC 35 ms 16920 KiB
02_max_03.txt AC 64 ms 16888 KiB
02_max_04.txt AC 63 ms 16876 KiB
03_handmade_00.txt AC 30 ms 3692 KiB
03_handmade_01.txt AC 30 ms 3752 KiB
03_handmade_02.txt AC 30 ms 3792 KiB
03_handmade_03.txt AC 30 ms 3884 KiB
03_handmade_04.txt AC 30 ms 3640 KiB
03_handmade_05.txt AC 30 ms 3644 KiB
03_handmade_06.txt AC 30 ms 3824 KiB
03_handmade_07.txt AC 30 ms 3764 KiB
03_handmade_08.txt AC 30 ms 3760 KiB
03_handmade_09.txt AC 30 ms 3792 KiB
03_handmade_10.txt AC 57 ms 16996 KiB
03_handmade_11.txt AC 58 ms 16952 KiB
03_handmade_12.txt AC 57 ms 16920 KiB
03_handmade_13.txt AC 58 ms 16900 KiB
03_handmade_14.txt AC 61 ms 16776 KiB
03_handmade_15.txt AC 58 ms 16924 KiB
03_handmade_16.txt AC 58 ms 16996 KiB
03_handmade_17.txt AC 58 ms 16924 KiB
03_handmade_18.txt AC 58 ms 16732 KiB
03_handmade_19.txt AC 59 ms 16936 KiB