HD-ACM algorithm specializing in series (--crixalis ' s equipment

Source: Internet
Author: User

Title Description:

AC Source:
This study greedy algorithm, problem-solving ideas: the principle of greed is to make the largest space left, preference for BI and AI difference between the largest, as to why? Here with only 2 equipment for example, (A1,B1) and (A2,B2), assuming that the first transport A1, the moment of transport, the actual space to occupy should be a1+b2, then in order to ensure that the largest space left, there should be a1+b2<a2+b1 set up, to first transport A1, That is B1-A1>B2-B1. (N devices can be 22 to make this comparison, to achieve the optimal selection)

#include "iostream" #include "algorithm" using namespace std;struct equipment {int A; int B;}; BOOL CMP (equipment A, equipment B) {return (A.B-A.A) > (B.B-B.A);}    int main () {int T, V, N;    BOOL Flag;    Equipment eq[1000];    scanf ("%d", &t);            for (int i = 0; i < T; i++) {scanf ("%d%d", &v, &n); for (int j = 0; J < N; j + +) {scanf ("%d%d", &eq[j]. A, &eq[j].            B);            } sort (eq, eq+n, CMP);            Flag = true; for (int j = 0; J < N; j + +) {if (Eq[j]. B <= v) {v-= eq[j].                A                    } else {flag = false;                Break            }} if (flag) {printf ("yes\n");            } else {printf ("no\n"); }} return 0;}

  

HD-ACM Algorithm Specialization series (--crixalis) equipment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.