Submission #378684

#TimeUsernameProblemLanguageResultExecution timeMemory
378684fhvirusGift (IZhO18_nicegift)C++17
7 / 100
1103 ms524292 KiB
// Knapsack DP is harder than FFT. #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ff first #define ss second #define pb emplace_back #define FOR(i,n) for(int i = 0; i < (n); ++i) #define FOO(i,a,b) for(int i = (a); i <= (b); ++i) #define AI(x) begin(x),end(x) template<class I> bool chmax(I &a, I b){ return a < b ? (a = b, true) : false;} template<class I> bool chmin(I &a, I b){ return a > b ? (a = b, true) : false;} #ifdef OWO #define debug(args...) LKJ("[ " + string(#args) + " ]", args) void LKJ(){ cerr << endl;} template<class I, class...T> void LKJ(I&&x, T&&...t){ cerr<<x<<", ", LKJ(t...);} template<class I> void DE(I a, I b){ while(a < b) cerr << *a << " \n"[next(a) == b], ++a;} #else #define debug(...) 0 #define DE(...) 0 #endif const int N = 1e5 + 225; int n, k; ll a[N]; template<typename T> using maxheap = priority_queue<T, vector<T>>; int32_t main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> k; if(k != 2) return 7122; FOO(i,1,n) cin >> a[i]; ll sum = 0; int cnt = 0; FOO(i,1,n) sum += a[i], cnt += (a[i] > 0); if(sum % 2 == 1 or cnt == 1){ cout << -1; return 0; } maxheap<pair<ll, int>> pq; FOO(i,1,n) if(a[i]) pq.emplace(a[i], i); vector<pair<int, vector<int>>> ans; while(!pq.empty()){ auto [s, i] = pq.top(); pq.pop(); auto [t, j] = pq.top(); pq.pop(); ans.pb(a[j], vector<int>()); ans.back().ss.pb(i); ans.back().ss.pb(j); a[i] -= a[j]; a[j] = 0; if(a[i]) pq.emplace(a[i], i); } cout << ans.size() << '\n'; for(auto [x, v]: ans){ cout << x << ' '; for(auto i: v) cout << i << ' '; cout << '\n'; } return 0; }
#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...