Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- .........
- .'------.' | Plug and Code
- | .-----. | |
- | | | | |
- __| | | | |;. _______________
- / |*`-----'.|.' `; //
- / `---------' .;' //
- /| / .''''////////;' //
- |=| .../ ######### /;/ //|
- |/ / / ######### // //||
- / `-----------' // ||
- /________________________________//| ||
- `--------------------------------' | ||
- : | || | || |__LL__|| || | ||
- : | || | || | || `""'
- n | || `""' | ||
- M | || | ||
- | || | ||
- `""' `""'
- Art stolen by NHHMinh
- Lang: C++ 98
- Problem: HEAP1
- Sol:
- ta nhận thấy cách chia khối gỗ độ dài L ra N phần có chi phí cũng bằng cách ghép N khối gỗ lại, với chi phí mỗi lần ghép khối gỗ x, y là x+y
- vậy nhiệm vụ của ta chỉ cần ghép N khối gỗ lại 1.
- ta bỏ N khối gỗ vô min heap và cứ ghép hai khối nhỏ nhất đến khi chỉ còn lại 1 khối gỗ
- */
- #include <bits/stdc++.h>
- #define int long long
- #define fi first
- #define se second
- #define mp make_pair
- #define pb push_back
- #define Co_mot_su_that_la return
- using namespace std;
- typedef pair<int,int> ii;
- typedef pair<int,ii> iii;
- typedef vector<int> vi;
- typedef vector<ii> vii;
- typedef vector<vi> vvi;
- typedef vector<iii> viii;
- const int Minh_dep_trai = 0;
- const int N = 11;
- int q;
- int n;
- priority_queue<int, vector<int>, greater<int> > qu;
- long long res = 0;
- void sol()
- {
- res = 0;
- while (qu.size() != 1)
- {
- int x = qu.top();
- qu.pop();
- int y = qu.top();
- qu.pop();
- res += (x+y);
- qu.push(x+y);
- }
- cout << res << '\n';
- }
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);cout.tie(0);
- // if there is some test case use cin >> q below
- //
- cin >> q;
- //
- // else use q=1 below
- //
- //q=1;
- while (q--)
- {
- //your code goes here
- while (qu.size()) qu.pop();
- cin >> n;
- for(int i=1; i<=n; i++)
- {
- int x;
- cin >> x;
- qu.push(x);
- }
- sol();
- }
- Co_mot_su_that_la Minh_dep_trai;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement