結果

問題 No.2869 yuusaan's Knapsacks
コンテスト
ユーザー yuusaan
提出日時 2024-07-17 10:31:26
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 2,721 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,433 ms
コンパイル使用メモリ 349,504 KB
実行使用メモリ 11,296 KB
最終ジャッジ日時 2026-04-03 20:01:30
合計ジャッジ時間 8,689 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1 TLE * 1 -- * 1
other -- * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'std::pair<int, int> pairmax(std::pair<int, int>, std::pair<int, int>, int)':
main.cpp:19:1: warning: no return statement in function returning non-void [-Wreturn-type]
   19 | }
      | ^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/vector:67,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/functional:66,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:55,
                 from main.cpp:1:
In function '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = int*; _ForwardIterator = int*]',
    inlined from 'constexpr _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _Sentinel, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = int*; _Sentinel = int*; _ForwardIterator = int*; _Tp = int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_uninitialized.h:635:32,
    inlined from 'constexpr std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/vector.tcc:257:35,
    inlined from 'int main()' at main.cpp:51:18:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_uninitialized.h:273:31: warning: 'void* __builtin_memcpy(void*, const void*, long unsigned int)' writing between 1 and 404 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
  273 |               __builtin_memcpy(std::__niter_base(__result),
      |               ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  274 |                                std::__niter_base(__first),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  275 |                                __n * sizeof(_ValT));
      |                                ~~~~~~~~~~

ソースコード

diff #
raw source code

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

int Power(int a, int n){
    int ret = 1;
    int p = a; 
    while (n){
        if (n & 1) ret = ret * p;
        p = p * p;
        n >>= 1;
    } 
    return ret; 
}

pair<int,int> pairmax(pair<int,int> a,pair<int,int> b,int pre){
    if(b > a)swap(a,b);
    if(a.first > b.first){
    }
}


int main(){
    int N,M;
    cin >> N >> M;

    vector<int> e(N),v(M),w(M);
    for(int i = 0;i < N;i++)cin >> e[i];
    for(int i = 0;i < M;i++)cin >> v[i] >> w[i];

    vector<vector<int>> knap(Power(2,M),vector<int>(101,0));
    for(int i = 0;i < (1 << M);i++){
        vector<int> nowlug(0);
        int lugnum = 0;
        for(int j = 0;j < M;j++){
            if(i & (1 << j)){
                nowlug.push_back(j);
                lugnum++;
            }
        }
        vector<int> dp(101,0);
        vector<int> predp(101,0);
        vector<int> emp(101,0);
        for(int j = 0;j < lugnum;j++){
            for(int iw = 1;iw <= 100;iw++){
                dp[iw] = max(predp[iw],dp[iw-1]);
                if(iw >= w[nowlug[j]]){
                    dp[iw] = max(dp[iw], predp[iw-w[nowlug[j]]] + v[nowlug[j]]);
                }
            }
            predp = dp;
            dp = emp;
        }
        knap[i] = predp;
    }

    int ans = 0;
    vector<vector<pair<int,int>>> dp(N+1,vector<pair<int,int>>(Power(2,M),{0,0}));
    for(int i = 0;i < N;i++){
        for(int j = 1;j < (1 << M);j++){
            int nowbit = j;
            while(true){
                //dp[i+1][j] = max(dp[i+1][j],dp[i][nowbit] + knap[j ^ nowbit][e[i]]);
                if(dp[i+1][j].first < dp[i][nowbit].first + knap[j^nowbit][e[i]]){
                    dp[i+1][j].first = dp[i][nowbit].first + knap[j^nowbit][e[i]];
                    dp[i+1][j].second = j ^ nowbit;
                }
                if(nowbit == 0)break;
                nowbit--;
                nowbit &= j;
            }
            ans = max(ans,dp[i+1][j].first);
        }
    }

    cout << ans << endl;
    vector<int> ans2(1 + N,0);
    for(int i = 1;i < N;i++){
        for(int j = 1;j < (1 << M);j++){
            if(dp[i][j].first == ans){
                while(i > 0){
                    ans2[i] = dp[i][j].second;
                    i--;
                    j -= dp[i][j].second;
                }
            }
        }
    }

    for(int j = 1;j <= N;j++){
        int count = 0;
        vector<int> out = vector<int> (0);
        for(int i = 0;i < M;i++){
            if(j & (1 << i)){
                count++;
                out.push_back(i+1);
            }
        }
        cout << count << " ";
        for(int i : out)cout << i << " ";
        cout << endl;
    }

    return 0;
}
0