Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- struct dbl {
- unsigned long x,y;
- };
- bool mysort_byy (dbl i,dbl j) {
- double ratio1 = ((double)i.x)/(i.y);
- double ratio2 = ((double)j.x)/(j.y);
- return (ratio1 > ratio2);
- }
- int main() {
- freopen("calatorie.in","r",stdin);
- freopen("calatorie.out","w",stdout);
- vector<dbl> vec;
- vector<dbl>::iterator it;
- dbl tmp;
- int n, m;
- int i;
- unsigned long maxsum = 0, minsum = 0, tmpsum, lastsum, othersum;
- cin >> n;
- while (n--) {
- cin >> m;
- if (m == 1) {
- cout << "Consumul minim = 0." << endl;
- continue;
- }
- while (--m) {
- cin >> tmp.x >> tmp.y;
- vec.push_back(tmp);
- }
- sort (vec.begin(), vec.end(), mysort_byy);
- maxsum = 0;
- for (it=vec.begin(); it!=vec.end(); ++it) {
- maxsum+=(*it).x;
- }
- minsum = 0;
- tmpsum = maxsum;
- lastsum = maxsum;
- i = 0;
- it = vec.begin();
- while (it != vec.end()) {
- minsum += (*it).y;
- tmpsum -= (*it).x;
- othersum = lastsum;
- lastsum = (minsum*minsum*minsum*minsum + tmpsum);
- if (lastsum > othersum) {
- lastsum = othersum;
- break;
- }
- it++;
- }
- if (lastsum < othersum) {
- othersum = lastsum;
- }
- cout << "Consumul minim = " << othersum << "." << endl;
- vec.clear();
- }
- //getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement