首页 > 代码库 > PAT 1037

PAT 1037

蛋疼题目系列

把正数和负数都排一遍序,然后就ok了

 1 #include <vector> 2 #include <iostream> 3 #include <string> 4 #include <fstream> 5 #include <queue> 6 #include <algorithm> 7  8 using namespace std; 9 10 struct ComLarge{11     bool operator()(const int i1, const int i2){12         return i1 > i2;13     }14 };15 16 struct ComSmall{17     bool operator()(const int i1, const int i2){18         return i1 < i2;19     }20 };21 22 //#define OJ23 24 #ifdef OJ25 #define fin cin26 #endif27 28 int main(){29 #ifndef OJ30     ifstream fin("in.data");31 #endif32 33     vector<int> pos_coupon;34     vector<int> neg_coupon;35     vector<int> pos_val;36     vector<int> neg_val;37 38     int NC, NP, val;39     fin >> NC;40     for (int i = 0; i < NC; i++){41         fin >> val;42         if (val > 0)43             pos_coupon.push_back(val);44         else45             neg_coupon.push_back(val);46     }47     fin >> NP;48     for (int i = 0; i < NP; i++){49         fin >> val;50         if (val > 0)51             pos_val.push_back(val);52         else53             neg_val.push_back(val);54     }55 56     sort(pos_coupon.begin(), pos_coupon.end(), ComLarge());57     sort(pos_val.begin(), pos_val.end(), ComLarge());58     sort(neg_coupon.begin(), neg_coupon.end(), ComSmall());59     sort(neg_val.begin(), neg_val.end(), ComSmall());60 61     int pos_cnt = (pos_coupon.size() < pos_val.size() ? pos_coupon.size() : pos_val.size());62     int neg_cnt = (neg_coupon.size() < neg_val.size() ? neg_coupon.size() : neg_val.size());63 64     int total = 0;65     for (int i = 0; i < pos_cnt; i++)66         total += pos_coupon[i] * pos_val[i];67     for (int i = 0; i < neg_cnt; i++)68         total += neg_coupon[i] * neg_val[i];69 70     cout << total << endl;71 72     return 0;73 }

 

PAT 1037