流利说9.11第一题派发礼物 贪心
//只通过66.67,不知道哪里出问题 #include "stdafx.h" #include <string> #include <vector> #include <set> #include <map> #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include<cmath> #include<sstream> #include <bitset> #include <stack> using namespace std; int cmp(pair<int,int> p1, pair<int,int> p2) { if (p1.first - p1.second <= p2.first - p2.second) { return 1; } else { return 0; } } int main() { int n; int nA,nB; int nTema, nTemb; cin>>n; cin>>nA>>nB; vector<pair<int,int>> vecNums; while (cin>>nTema>>nTemb) { vecNums.push_back(make_pair(nTema,nTemb)); } sort(vecNums.begin(),vecNums.end(),cmp); int sum = 0; int i = 0, j = vecNums.size()-1; while (i <= j) { if (vecNums[i].first - vecNums[i].second <= 0 && nA > 0) { sum += vecNums[i].first; ++i; --nA; } else { sum += vecNums[j].second; --j; } } cout<<sum<<endl; system("pause"); return 0; }
#流利说##笔试题目#