HDU Crixalis's Equipment(贪心)

贪心策略:

数据(v1, v2),按 v2 - v1 的差值从大到小进行排序

分析:

例(1):

v = 10  2件物品:a(2, 5)  b(6, 8)

-----如果先进a,再进b:

进a:v = 10 --> v >= 5 (成立) --> v -= 2 --> v = 8

进b:v = 8 --> v >= 8 (成立) --> v -= 6 --> v = 2  物品全部进入  输出Yes

-----如果先进b,再进a:

进b:v = 10 --> v >= 8 (成立) --> v -= 6 --> v = 4

进a:v = 4 --> v >= 5 (不成立)   物品未全部进入  输出No

例(2):

v = 13  2件物品:a(1, 3)  b(9, 13)

-----如果先进a,再进b:

进a:v = 13 --> v >= 3 (成立) --> v -= 1 --> v = 12

进b:v = 12 --> v >= 13 (不成立)  物品未全部进入  输出No

-----如果先进b,再进a:

进b:v = 13 --> v >= 13 (成立) --> v -= 9 --> v = 4

进a:v = 4 --> v >= 3 (成立) --> v -= 1 --> v = 3  物品全部进入  输出Yes

总结:

两件物品时:a(x1, y1)  b(x2, y2),此时,如果V的最小值为:min( max(y1, x1 + y2), max(y2, x2 + y1) ),max(y1, x1 + y2)即先进a,后进b,max(y2, x2 + y1)即先进b,后进a,得出两种进入方式的需要的最小容量值

当a(2, 5)  b(6, 8),a差值3,b差值2 ==> min( x1 + y2 , x2 + y1) ==> x1 + y2 < x2 + y1 ==> y2 - x2 < y1 - x1 ==> 当a的差值大于b的差值时,此时有最小的容量v,所以先进a,后进b,即按物品差值从大到小排序

当a(1, 3)  b(9, 13),a差值2,b差值4 ==> min( x1 + y2 , y2) ==> x1 + y2 > y2 ==> 因为y2 > x2 + y1,所以x1 + y2 > x2 + y1 ==> y2 - x2 > y1 - x1 ==> 当b的差值大于a的差值时,此时有最小容量v,所以先进b后进a,即按照物品差值从大到小排序

额外测试数据:

Input:

3

11 2
4 5
6 8

10 2
2 5
6 8

13 2
1 3
9 13

Output:

Yes
Yes
Yes

数据(x, y),刚开始以为贪心策略是以 y 值大小进行从大到小排序,问题考虑不周到......

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 #define MAX 1010
 8
 9 struct Node
10 {
11     int a, b;
12 }node[MAX];
13
14 bool cmp(Node x, Node y)
15 {
16     return x.b - x.a > y.b - y.a;
17 }
18
19 int main(void)
20 {
21     int t;
22     scanf("%d", &t);
23     while (t--)
24     {
25         memset(node, 0, sizeof(node));
26         int v, n;
27         scanf("%d%d", &v, &n);
28         for (int i = 0; i < n; i++)
29         {
30             scanf("%d%d", &node[i].a, &node[i].b);
31         }
32         sort(node, node + n, cmp);
33         int flag = 0;
34         for (int i = 0; i < n; i++)
35         {
36             if (v >= node[i].b)
37             {
38                 v -= node[i].a;
39             }
40             else
41             {
42                 flag = 1;
43                 break;
44             }
45         }
46         if (flag)
47         {
48             printf("No\n");
49         }
50         else
51         {
52             printf("Yes\n");
53         }
54     }
55     return 0;
56
57 }

HDU Crixalis's Equipment(贪心)

原文地址:https://www.cnblogs.com/changeFeng/p/9329276.html

时间: 2024-10-13 23:56:49

HDU Crixalis's Equipment(贪心)的相关文章

HDUJ 3177 Crixalis&#39;s Equipment 贪心

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 Description Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts

hdu 3177 Crixalis&#39;s Equipment(贪心)

Problem Description Crixalis - Sand King used to be a giant 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 Crixalis decides

[HDU] 3177.Crixalis&#39;s Equipment (贪心)

Problem Description 题目大意: 有V的空间以及N个物品,对于放入每个物品有Ai(放入物品后剩余空间的减少大小),Bi(放入该物品之前需要的最小剩下空间大小,如果大于当前剩余空间大小则无法放入) Input T组数据,每组数据有N+1行,每组数据第一行输入V,N,之后的N行每行描述一个物品的Ai,Bi 0<T<= 10, 0<V<10000, 0<N<1000, 0 <Ai< V, Ai <= Bi < 1000. Output

Hdu 3177 Crixalis&#39;s Equipment

Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2795    Accepted Submission(s): 1141 Problem Description Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts

hdu acm-step 1.3.8 Crixalis&#39;s Equipment

本题题意:蝎子王搬家,洞的空间是V,共有N件物品要搬,每件物品在搬运过程中要占用Bi空间,搬运完后占用Ai,问蝎子王能否搬完所有的物品. 代码如下: #include <cstdio> #include <algorithm> using namespace std; struct Node { int A,B; bool operator < (const Node &node){if(B-A>node.B-node.A)return true;else if

hdoj 3177 Crixalis&#39;s Equipment 【贪心】

Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3073    Accepted Submission(s): 1250 Problem Description Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts

ZCMU Problem H: Crixalis&#39;s Equipment(贪心,排序)

#include<stdio.h> #include<stdlib.h> struct node { int a,b; }c[1002]; int cmpxy(const struct node *c,const struct node *d) { return (d->b - d->a) - (c->b - c->a); } int main() { int n,i,v,t,flag; // freopen("a.txt","

Crixalis&#39;s Equipment

Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3097 Accepted Submission(s): 922 Problem Description Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts of Kal

HD-ACM算法专攻系列(23)——Crixalis&#39;s Equipment

题目描述: AC源码:此次考察贪心算法,解题思路:贪心的原则是使留下的空间最大,优先选择Bi与Ai差值最大的,至于为什么?这里用只有2个设备为例,(A1,B1)与(A2,B2),假设先搬运A1,搬运的那一瞬间,实际将要占用的空间应该为A1+B2,那么为了保证留下的空间最大,则应该有A1+B2<A2+B1成立,才能先搬运A1,即B1-A1>B2-B1.(n个设备可以两两做这样的比较,来达到选择的最优) #include"iostream" #include"algo