Submission #1086629

#TimeUsernameProblemLanguageResultExecution timeMemory
1086629CDuongCouncil (JOI23_council)C++17
100 / 100
578 ms64852 KiB
/* #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt") */ #include <bits/stdc++.h> #define taskname "" #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define i64 long long #define isz(x) (int)x.size() using namespace std; void solve() { int n, m; cin >> n >> m; vector<int> cnt(m); vector<vector<int>> a(n, vector<int>(m)); vector<pair<pair<int, int>, pair<int, int>>> dp(1 << m, {{-1, -1}, {-1, -1}}); auto upd = [&](int msk, pair<int, int> val) -> void { if (val.second == dp[msk].first.second or val.second == dp[msk].second.second) return; if (dp[msk].first.first < val.first) dp[msk].first = val; else if (dp[msk].second.first < val.first) dp[msk].second = val; }; for (int i = 0; i < n; ++i) { int msk = 0; for (int j = 0; j < m; ++j) { cin >> a[i][j]; cnt[j] += (a[i][j] ? 1 : -1); if (not a[i][j]) msk |= 1 << j; } // cout << msk << " " << __builtin_popcount(msk) << " " << i << endl; upd(msk, {__builtin_popcount(msk), i}); } for (int msk = (1 << m) - 1; msk >= 0; --msk) { for (int i = 0; i < m; ++i) if (msk >> i & 1) { upd(msk ^ (1 << i), {dp[msk].first.first - 1, dp[msk].first.second}); upd(msk ^ (1 << i), {dp[msk].second.first - 1, dp[msk].second.second}); } } for (int i = 0; i < m; ++i) for (int msk = 0; msk < 1 << m; ++msk) if (msk >> i & 1) { int omsk = msk ^ (1 << i); upd(msk, dp[omsk].first); upd(msk, dp[omsk].second); } // for (int i = 0; i < 1 << m; ++i) { // cout << i << ":\n"; // cout << "{" << dp[i].first.first << ", " << dp[i].first.second << "}" << "\n"; // cout << "{" << dp[i].second.first << ", " << dp[i].second.second << "}" << "\n"; // } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cnt[j] -= (a[i][j] ? 1 : -1); } int needed_msk = 0; for (int j = 0; j < m; ++j) { if (cnt[j] == 0 or cnt[j] == 1) needed_msk |= (1 << j); } int needed_idx = (dp[needed_msk].first.second != i ? dp[needed_msk].first.second : dp[needed_msk].second.second); assert(needed_idx != -1); for (int j = 0; j < m; ++j) { cnt[j] -= (a[needed_idx][j] ? 1 : -1); } int res = 0; for (int j = 0; j < m; ++j) if (cnt[j] > 0) ++res; cout << res << "\n"; for (int j = 0; j < m; ++j) { cnt[j] += (a[needed_idx][j] ? 1 : -1); cnt[j] += (a[i][j] ? 1 : -1); } } } signed main() { #ifndef CDuongg if (fopen(taskname".inp", "r")) assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout)); #else freopen("bai3.inp", "r", stdin); freopen("bai3.out", "w", stdout); auto start = chrono::high_resolution_clock::now(); #endif ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; //cin >> t; while(t--) solve(); #ifdef CDuongg auto end = chrono::high_resolution_clock::now(); cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '='; cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl; #endif }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...