結果
問題 | No.1606 Stuffed Animals Keeper |
ユーザー |
![]() |
提出日時 | 2021-07-16 22:56:18 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 188 ms / 3,000 ms |
コード長 | 1,635 bytes |
コンパイル時間 | 1,105 ms |
コンパイル使用メモリ | 119,584 KB |
最終ジャッジ日時 | 2025-01-23 02:32:20 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
ソースコード
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<bitset> #include<set> #include<map> #include<stack> #include<queue> #include<deque> #include<list> #include<iomanip> #include<cmath> #include<cstring> #include<functional> #include<cstdio> #include<cstdlib> #include<numeric> #include<ctime> //#include<atcoder/all> using namespace std; //using namespace atcoder; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 //#define MOD 1000000007 #define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 #define PI acos(-1.0) const double EPS = 1e-10; using ll = long long; using Pi = pair<int,int>; using Pl = pair<ll,ll>; //using mint = modint998244353; int dh[] = {1,0,-1,0}; int dw[] = {0,1,0,-1}; ll dp[5010][10010]; int main(){ int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; int cnt0 = 0,cnt1 = 0; vector<Pi> v; rep(i,n){ if(a[i] == 2){ v.emplace_back(cnt0,cnt1); cnt0 = 0; cnt1 = 0; } else if(a[i] == 0) cnt0++; else cnt1++; } v.emplace_back(cnt0,cnt1); int m = v.size(); rep(i,5010)rep(j,10010){ dp[i][j] = LINF; } dp[0][5000] = 0; rep(i,m){ int u = v[i].first; int w = v[i].second; rep(j,10010){ if(dp[i][j] == LINF) continue; dp[i+1][j+u] = min(dp[i+1][j+u],dp[i][j]+u); dp[i+1][j-w] = min(dp[i+1][j-w],dp[i][j]); } } if(dp[m][5000] == LINF) cout << -1 << endl; else cout << dp[m][5000] << endl; }