Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long i64;
- struct Stare {
- i64 sleft;
- i64 pmodif, puntil;
- int id; /// adica acum vreau sa modifi tipul care e pe poztia
- ///pmodif si am voie sa il duc pana la puntil
- int last;
- bool operator< (const Stare & s) const {
- return sleft < s.sleft;
- }
- };
- vector <Stare> Solve(int nr, vector <i64> & vals, vector <i64> & spans, i64 smax, int & need, vector <pair <i64, i64>> & buffer)
- {
- /// pun nr oameni
- vector <Stare> ans;
- priority_queue <Stare> stari;
- i64 sleft = smax - spans[nr - 1];
- //cerr << "VALS" << endl;
- //for (auto x : vals) cerr << x << " "; cerr << endl;
- if (sleft < 0)
- return vector <Stare> ();
- stari.push({ sleft, nr - 1, (i64)vals.size() - 1, nr - 1, -1 });
- while (!stari.empty()) {
- auto act = stari.top();
- stari.pop();
- ans.push_back(act), need--;
- buffer.push_back({ nr, smax - act.sleft });
- if (need == 0) break;
- //cerr << nr << " " << smax - act.sleft << endl;
- /// trebuie sa ma extind
- if (act.pmodif != act.puntil) {
- i64 nsleft = act.sleft - vals[act.pmodif + 1] + vals[act.pmodif];
- if (nsleft >= 0) {
- stari.push({ nsleft, act.pmodif + 1, act.puntil, act.id, ans.back().last });
- }
- }
- if (act.id != 0 && act.pmodif > act.id) {
- i64 nsleft = act.sleft - vals[act.id] + vals[act.id - 1];
- if (nsleft >= 0)
- stari.push({ nsleft, act.id, act.pmodif - 1, act.id - 1, (int)ans.size() - 1 });
- }
- }
- return ans;
- }
- vector <int> reconstruct(vector <Stare> & ans, vector <int> real, int k)
- {
- vector <int> rez(k);
- /// plec de la ans.back()
- int poz = ans.size() - 1;
- for (int i = 0; i < k; ++i)
- rez[i] = real[i];
- while (poz != -1) {
- rez[ans[poz].id] = real[ans[poz].pmodif];
- // assert(ans[ans[poz].last].id == ans[poz].id + 1);
- poz = ans[poz].last;
- }
- return rez;
- }
- void test(int n, i64 k, int m, vector <i64> vals)
- {
- vector <pair <i64, int>> for_sort(n);
- for (int i = 0; i < n; i++)
- for_sort[i] = { vals[i], i };
- sort(for_sort.begin(), for_sort.end());
- vector <int> real(n);
- for (int i = 0; i < n; i++)
- real[i] = for_sort[i].second;
- sort(vals.begin(), vals.end());
- vector <i64> spans = vals;
- for (int i = 1; i < n; i++)
- spans[i] += spans[i - 1];
- vector <Stare> ans;
- vector <pair <i64, i64>> buffer;
- int ch = 1;
- for (int i = n; i > 0 && m; i--) {
- ans = Solve(i, vals, spans, k, m, buffer);
- if (m == 0) {
- ch = i;
- break;
- }
- }
- cout << buffer.size() << '\n';
- for (auto i : buffer)
- cout << i.first << ' ' << i.second << '\n';
- if (buffer.size()) {
- auto x = reconstruct(ans, real, ch);
- for (auto i : x)
- cout << i + 1 << ' ';
- cout << '\n';
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- i64 n, k, m;
- cin >> n >> k >> m;
- vector <i64> vals(n);
- for (auto & i : vals)
- cin >> i;
- test(n, k, m, vals);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement