Hduj 3177 crix?'s equipment greedy

Source: Internet
Author: User

Crixalis's equipment Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2568 accepted submission (s): 1059


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 Input
220 310 203 101 710 21 102 11
 
Sample output
YesNo

Analysis: This question is about moving. It takes a lot of space to carry things and space required for carrying. The greedy principle of this question is to load things as much as possible, that is

Move parking volume
First item A1 B1
Item 2 B2
Assume that the moving volume of these two items is not greater than the volume of the hole V
When we compare two items separately, we will find that A1 + B2 puts the maximum instantaneous volume of the first item and then the second item.
A2 + B1 puts the maximum instantaneous volume of the first item after the second item
We should select A1 + B2 and A2 + B1 to expand from two items to N items first.
Assume that the Movement volume of N items is not greater than the volume V of the hole (if the movement volume is greater than the hole volume, the result must be no)
Sort n items by b1-a1> b2-a2 and put them in the cave in sequence, that is, put them in the cave according to the difference value from large to small




#include<iostream>#include<cstring>#include<algorithm>using namespace std;struct node{int a,b,c;}s[1005];int cmp(node x,node y){return x.c>y.c;}int main(){int T;cin>>T;while(T--){int n,m;cin>>n>>m;int i,flag=0;;for(i=0;i<m;i++){cin>>s[i].a>>s[i].b;s[i].c=s[i].b-s[i].a;}sort(s,s+m,cmp);for(i=0;i<m;i++){if(n>=s[i].b)   n-=s[i].a;else{flag=1;break;}}if(flag)   cout<<"No\n";elsecout<<"Yes\n";}return 0;}


Hduj 3177 crix?'s equipment greedy

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.