tangs il y a 7 ans
Parent
commit
ff8c9fcbbc
1 fichiers modifiés avec 14 ajouts et 12 suppressions
  1. 14 12
      section3-7/main.cpp

+ 14 - 12
section3-7/main.cpp

@@ -1,4 +1,5 @@
 #include <iostream>
+#include <algorithm>
 
 using namespace std;
 
@@ -11,13 +12,16 @@ bool compare(Box a, Box b) {
     return a.Bi > b.Bi;
 }
 
-bool judge(int V, int N, int maxAi, int maxBi, Box *boxes) {
-    if (maxAi > maxBi) {
+bool judge(int V, int N, int record, int maxAi, int maxBi, int allAi, Box *boxes) {
+    if (allAi > V || maxAi > V || maxBi > V) {
         return false;
     }
     sort(boxes, boxes + N, compare);
-    for (int i = 0; i < N; i++) {
-        
+    for (int i = 0; i < record; i++) {
+        if (boxes[i].Bi > V) {
+            return false;
+        }
+        V -= boxes[i].Ai;
     }
     return true;
 }
@@ -31,20 +35,18 @@ int main() {
 
             int maxAi = 0, maxBi = 0;
             int allAi = 0;
+            int record = 0;
             boxes = new Box[N];
             for (int j = 0; j < N; j++) {
                 Box box;
                 cin >> box.Ai >> box.Bi;
-                if (box.Ai > maxAi) {
-                    maxAi = box.Ai;
-                }
-                if (box.Bi > maxBi) {
-                    maxBi = box.Bi;
-                }
                 allAi += box.Ai;
-                boxes[j] = box;
+                if (box.Bi > box.Ai) {
+                    boxes[j] = box;
+                    record++;
+                }
             }
-            cout << (judge(V, N, maxAi, maxBi, boxes) ? "YES" : "NO") << endl;
+            cout << (judge(V, N, record, maxAi, maxBi, allAi, boxes) ? "Yes" : "No") << endl;
             free(boxes);
         }
     }