HDU 3177 crixalis's equipment (Greedy)

Source: Internet
Author: User
Problem descriptioncrix‑sand King used to be a giant scorpion (scorpion) in the deserts of Kalimdor. though he's a guardian of Lich King now, he keeps the living habit of a scorpion like living underground and digging holes.

Someday crixincludecides to move to another nice place and build a new house for himself (actually it's just a new hole ). as he collected a lot of equipment, he needs to dig a hole beside his new house to store them. this hole has a volume of V units, and crix?has n equipment, each of them needs AI units of space. when dragging his equipment into the hole, crix?finds that he needs more space to ensure everything is placed well. actually, the ith equipment needs Bi units of space during the moving. more precisely crix?can not move equipment into the hole unless there are bi units of space left. after it moved in, the volume of the hole will decrease by AI. crix?wonders if he can move all his equipment into the new hole and he turns to you for help.

 

Inputthe first line contains an integer T, indicating the number of test cases. then follows T cases, each one contains N + 1 lines. the first line contains 2 integers: V, volume of a hole and N, number of equipment respectively. the next n lines contain N pairs of integers: AI and Bi.
0 <t <= 10, 0 <v <10000, 0 <n <1000, 0 <AI <v, AI <= Bi <1000.

 

Outputfor each case output "yes" if crix?can move all his equipment into the new hole or else output "no ".

 

Sample input220 310 203 101 710 21 102 11

 

Sample outputyes No

I want to move some equipment into the cave. the space in the cave is V. He has n pieces of equipment, and each piece of equipment requires AI space. When he moves the equipment into the cave, bi space is required. After a piece of equipment is placed, the volume of Ai In the cave is reduced. If you can move all the equipment into the cave, you can lose yes or no.

 

Ideas:

This question cannot be sorted by Bi size.

For example, in a group of data, V = 22

There are 2 pieces of equipment a1 = 19 b1 = 21

A2 = 1 b2 = 20

Then, if we sort data by the size of B from large to small, we will first move the first item and then the second one, then we will not be able to move both pieces of equipment into the cave, if the second item is moved first, and there is still 21 remaining space in the cave after the second item is moved, you can move all the two pieces of equipment into the cave.

 

Therefore, the instantaneous maximum volume should be considered. For example, if you move the first item first and then the second item, the maximum instantaneous space required is 19 + 20 = 39, first move the second item and then move the first item. The maximum instantaneous space required is 1 + 21 = 22.

Therefore, we need to compare the sizes of A1 + B2 and A2 + B1. If A1 + b2 <A2 + B1

That is, A1-B1 <A2-B2 first moves the first one. So you can sort by AI-bi.

 

 

#include <iostream>#include <cstring>#include <cstdio>#include <string>#include <algorithm>using namespace std;#define maxn 1010int T, N, V;bool flag;struct Node{    int a, b;}node[maxn];bool cmp(Node x, Node y){    return (x.a - x.b) < (y.a - y.b);}int main(){    scanf("%d", &T);    while(T--){        scanf("%d%d", &V, &N);        for(int i = 1; i <= N; i++){            scanf("%d%d", &node[i].a, &node[i].b);        }        sort(node+1, node+1+N, cmp);                flag = true;        for(int i = 1; i <= N; i++){            if(node[i].b <= V){                V -= node[i].a;            }            else{                flag = false;                break;            }        }        if(flag) printf("Yes\n");        else printf("No\n");            }        return 0;}

 

 

 

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.