标签:style blog io ar color os sp for on
一. 题意
按照孩子们需要的积木块数排序(从小到大),先处理需要积木块数少的孩子。
二. 代码
1 // 2 // main.cpp 3 // sicily-1134 4 // 5 // Created by ashley on 14-10-25. 6 // Copyright (c) 2014年 ashley. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <algorithm> 11 using namespace std; 12 13 typedef struct 14 { 15 long ownCake; 16 long wantCake; 17 }child; 18 19 child childs[10000]; 20 21 bool compare(child left, child right) 22 { 23 return left.wantCake < right.wantCake; 24 } 25 26 int main(int argc, const char * argv[]) 27 { 28 int childrenNum; 29 long currentNum, cakeNum; 30 bool success; 31 while (cin >> childrenNum >> cakeNum) { 32 if (childrenNum == 0) { 33 break; 34 } 35 success = true; 36 currentNum = cakeNum; 37 for (int i = 0; i < childrenNum; i++) { 38 cin >> childs[i].ownCake >> childs[i].wantCake; 39 } 40 sort(childs, childs + childrenNum, compare); 41 for (int i = 0; i < childrenNum; i++) { 42 if (childs[i].wantCake > currentNum) { 43 success = false; 44 cout << "NO" << endl; 45 break; 46 } else { 47 currentNum = currentNum + childs[i].ownCake; 48 } 49 } 50 if (success) { 51 cout << "YES" << endl; 52 } 53 } 54 return 0; 55 }
标签:style blog io ar color os sp for on
原文地址:http://www.cnblogs.com/ashley-/p/4151836.html