結果
問題 |
No.393 2本の竹
|
ユーザー |
![]() |
提出日時 | 2016-07-14 19:35:56 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3 ms / 1,000 ms |
コード長 | 1,334 bytes |
コンパイル時間 | 839 ms |
コンパイル使用メモリ | 67,372 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-08 09:26:26 |
合計ジャッジ時間 | 1,708 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
#include <iostream> #include <algorithm> #include <vector> using namespace std; int n1, n2; vector<int> a; vector<int> DP; int n0,n1sa,n2sa; bool solve(int sum,int first); int main() { int d,m; cin >> d; for (int i = 0; i < d; i++) { cin >> n1 >> n2; cin >> m; a.resize(m); for (int j = 0; j < m; j++) { cin >> a[j]; } sort(a.begin(), a.end()); n0 = n1 + n2; for (int j=0; j < a.size(); j++) { if (a[j] <= n0) { n0 -= a[j]; }else { a.erase(a.begin() + j); j--; } } if(n0>=n1){ cout<<a.size()<<endl; continue; } n1sa=n1-n0; n2sa=n2-n0; reverse(a.begin(), a.end()); DP.assign(100010, 100); cout << a.size() - !solve(0, 0) << endl; } return 0; } bool solve(int sum,int first) { int tmp; for (int i = first; i < a.size(); i++) { tmp = sum + a[i]; if ((tmp >= n1sa&&tmp <= n1) || (tmp >= n2sa&&tmp <= n2)) { return true; } else if (tmp > n1) continue; if (DP[tmp] <= i) continue; else { DP[tmp] = i; if (solve(tmp,i+1)) return true; } } return false; }