#include<bitset> // char to int int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } const int FLAG_NUM = 10; // 1023 => 00111 11111 vector<int> num_to_flags(int x){ stringstream ss; ss << static_cast<std::bitset<FLAG_NUM> >(x); string s = ss.str(); vector<int> V(FLAG_NUM); FOR(i, 0, FLAG_NUM){ V[i] = ctoi(s[i]); } return V; }
実際に使っているコード
https://atcoder.jp/contests/abc080/submissions/3887402
追記:それ、bitsetでよくない?(2020/10/18)
- とは思うものの、以前はそれを知らなかったのであろう。上の問題はbit全探索を使うと簡潔に書ける
- AC https://atcoder.jp/contests/abc080/submissions/17504329
int main(){ cin.tie(0); ios::sync_with_stdio(false); // input ll N; cin>>N; VV F(N, VI(10)); rep(i,N){ rep(j,10){ cin>>F[i][j]; } } // profit VV P(N, VI(11)); rep(i,N){ rep(j,11){ cin>>P[i][j]; } } ll ma = -inf; // bit全探索 FOR(flag, 1, 1<<10){ VI C(N); // count rep(i,10){ if(flag>>i&1){ // 営業します rep(j,N){ if(F[j][i]==1)C[j]++; } } } ll profit=0; rep(i,N){ ll cnt = C[i]; profit += P[i][cnt]; } chmax(ma,profit); } p(ma); return 0; }