POJ 3614 [zoj 3508] -- Sunscreen


http://poj.org/problem?id=3614
Description
To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If theSPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn't tan at all........
The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.
What is the maximum number of cows that can protect themselves while tanning given the available lotions?
Input
* Line 1: Two space-separated integers: C and L
* Lines 2..C+1: Line i describes cow i's lotion requires with two integers: minSPFi and maxSPFi
* Lines C+2..C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri
Output
A single line with an integer that is the maximum number of cows that can be protected while tanning
Sample Input
3 2
3 10
2 5
1 5
6 2
4 1
Sample Output
2
设C个奶牛的防晒区间分别为 (L1,R1) ( L2 , R2 ) ( L3 ,R3)  ……( Ln ,Rn) ,L 种防晒霜(SPF1, num1)   (SPF2,num2)  …… (SPFn,  num n)

①要想使尽可能多的奶牛能够享受阳光, 主要取决于防晒霜,应尽量使防晒霜SPF小的应用于防晒区间小的,并且在SPF,num值允许的情况下尽量涉及到每个区间,才是最佳的

②将C个奶牛的防晒区间按照左端点从小到大的顺序排序,左端点相同的按照右端点从小到大排序,将L种防晒霜按照防晒系数SPF从小到大排序

③从小到大遍历每种防晒霜,针对防晒霜Li ,首先确定能够被其作用的奶牛区间,即满足条件    L <= Li. SPF<=R d的奶牛区间 ,在这些区间当中优先选择R最小的那个区间,因为大区间(R比较大)的奶牛有更大的选择空间,而R小的能选择的空间比较小,让其优先使用防晒霜。这样就能保证整个过程尽可能多的奶牛使用合适的防晒霜。

https://blog.csdn.net/Akatsuki__Itachi/article/details/75634181
题意:有C头奶牛,她们去海边玩耍吧!但是阳光强度特别高,她们能忍受的辐射强度有个范围,即min~max,高于max奶牛就被晒伤了,低于max奶牛没什么感觉。现在有L瓶防晒霜,前面的数据代表这种防晒霜能把SPF值固定在某个值上,使奶牛不被晒伤,后面数据代表这种防晒霜就几瓶。

解题思路:将奶牛能承受的最小值min从小到大排序,防晒霜的SPF值也从小到大排序。对于每一瓶防晒霜将所有合适的奶牛遍历一遍,即如果防晒霜的SPF值大于奶牛所能承受的min值,就将她的最大值入到优先队列(从小到大排序的),然后再次判断这个spf值是否小于队列的队首,如果符合就出队,同时数量-1.

https://blog.csdn.net/sdj222555/article/details/10698641
从最小的防晒霜枚举,将所有符合  最小值小于等于该防晒霜的 奶牛的 最大值 放入优先队列之中。
然后优先队列是小值先出
所以就可以将这些最大值中的最小的取出来。更新答案。
http://www.voidcn.com/article/p-mqestlhe-ec.html
https://www.cnblogs.com/douzujun/p/6853516.html
把牛按minSPF从小到大排序一下,防晒霜按SPF排序一下。
然后遍历防晒霜lontion[1...L],对于lotion[i],把所有满足minSPF小于等于lontion[i].SPF的牛都找出来,入队。
我们将这个队列定义为按cow[i].maxSPF的从小到大排序的优先队列,那么,每次取出的队首,都是当前要求最苛刻(SPF要求范围最小的)那头牛,先满足这些牛,如果当前这 种防晒霜能满足这头牛,就用掉一瓶,否则就放弃这头牛。
反复如此,直到所有防晒霜用完。
 5 struct Cows{
 6     int minSPF,maxSPF;
 7     bool operator<(const Cows &oth)const
 8     {
 9         return maxSPF>oth.maxSPF;
10     }
11 }cow[2505];
12 struct Lotions{
13     int SPF,cover;
14 }lotion[2505];
15 bool cmp1(Lotions a,Lotions b) {return a.SPF<b.SPF;}
16 bool cmp2(Cows a,Cows b) {return a.minSPF<b.minSPF;}
17 int main()
18 {
19     int C,L;
20     priority_queue<Cows> q;
21     scanf("%d%d",&C,&L);
22     for(int i=1;i<=C;i++) scanf("%d%d",&cow[i].minSPF,&cow[i].maxSPF);
23     for(int i=1;i<=L;i++) scanf("%d%d",&lotion[i].SPF,&lotion[i].cover);
24     sort(lotion+1,lotion+L+1,cmp1);
25     sort(cow+1,cow+C+1,cmp2);
26     int now=1,ans=0;
27     for(int i=1;i<=L;i++)
28     {
29         while(now <= C && cow[now].minSPF <= lotion[i].SPF)//由于事先排过序,所以当前这头一旦不满足cow[now].minSPF <= lotion[i].SPF,之后的牛都不满足 
30         {
31             q.push(cow[now]);
32             now++;
33         }
34         while(!q.empty() && lotion[i].cover>0)//要么是当前这种防晒霜很多,把所有牛都涂了;要么就是当前这种防晒霜用完了
35         {
36             if(q.top().maxSPF >= lotion[i].SPF)//这种防晒霜可以给这头牛涂
37             {
38                 ans++;//能涂的牛的数量增加1 
39                 lotion[i].cover--;//当前这种防晒霜用去一瓶
40             }
41             q.pop();//涂的上就涂,涂不上就放弃
42         }
43     }
44     printf("%d\n",ans);
45 } 

http://www.hankcs.com/program/cpp/poj-3614-sunscreen.html奶牛美容:有C头奶牛日光浴,每头奶牛分别需要minSPF_i和maxSPF_i单位强度之间的阳光。现有L种防晒霜,分别能使阳光强度稳定为SPF_i,其瓶数为cover_i。求最多满足多少头奶牛
最小堆
2.4 加工并储存数据的数据结构
优先队列

首先得确定一个贪心策略,在满足minSPF的条件下,尽量把SPF小的防晒霜用在maxSPF小的奶牛身上,因为maxSPF大的奶牛有更大的选择空间。用一个最小堆q维护maxSPF的最小值,可以高效解决问题。
pair<intint> cow[2500 + 16];
pair<intint> bottle[2500 + 16];
priority_queue<int, vector<int>, greater<int> > q;    // 优先级队列,小元素优先出队
///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
    freopen("out.txt""w", stdout);
#endif
    int C, L;
    cin >> C >> L;
    for (int i = 0; i < C; ++i)
    {
        cin >> cow[i].first >> cow[i].second;
    }
    for (int i = 0; i < L; ++i)
    {
        cin >> bottle[i].first >> bottle[i].second;
    }
    sort(cow, cow + C);
    sort(bottle, bottle + L);
    int cur = 0; // 现在正等待涂防晒霜的奶牛的index
    int result = 0;
    for (int i = 0; i < L; ++i)
    {
        while (cur < C && cow[cur].first <= bottle[i].first)
        {
            q.push(cow[cur].second);
            ++cur;
        }
        while (!q.empty() && bottle[i].second)
        {
            int maxSPF = q.top(); q.pop();
            // “奶牛上限”比这一瓶的上限大,说明这头奶牛可以被涂上防晒霜
            if (maxSPF >= bottle[i].first)
            {
                ++result;
                --bottle[i].second;
            }
            // else 这头奶牛不能被涂上,因为bottle是按SPF排过序的,没有比这瓶更小的SPF了
        }
    }
    cout << result << endl;
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("out.txt");
#endif
    return 0;
}
http://blog.csdn.net/qq7366020/article/details/18186277
  1. #define MAX 2600  
  2. struct snode{  
  3.     int x,y;  
  4. }Num1[MAX],Num2[MAX];  
  5.   
  6. bool cmp(struct snode a,struct snode b)  
  7. {  
  8.     return a.y<b.y?true:false;  
  9. }  
  10.   
  11. bool comp(struct snode a,struct snode b)  
  12. {  
  13.     return a.x<b.x?true:false;  
  14. }  
  15.   
  16. int main()  
  17. {  
  18.     int i,j,n,m;  
  19.     while(scanf("%d%d",&n,&m)!=EOF)  
  20.     {  
  21.         for(i=1;i<=n;i++)  
  22.         {  
  23.             scanf("%d%d",&Num1[i].x,&Num1[i].y);  
  24.         }  
  25.         for(i=1;i<=m;i++)  
  26.         {  
  27.             scanf("%d%d",&Num2[i].x,&Num2[i].y);  
  28.         }  
  29.         sort(Num1+1,Num1+1+n,cmp);     //Cow按MaxSPF从小到大排  
  30.         sort(Num2+1,Num2+1+m,comp);    //bottle按从小到大排  
  31.         int sum=0;  
  32.         for(i=1;i<=n;i++)  
  33.         {  
  34.             for(j=1;j<=m;j++)  
  35.             {  
  36.                 if(Num2[j].y>=1&&Num2[j].x>=Num1[i].x&&Num2[j].x<=Num1[i].y) //找到第一个符合条件  
  37.                 {  
  38.                     sum++;  
  39.                     Num2[j].y--;  
  40.                     break;                       
  41.                 }  
  42.                 else if(Num2[j].x>Num1[i].y)  //若bottle的SPF比cow的MaxSPF还大则退出循环  
  43.                     break;                    //剪枝  
  44.             }  
  45.         }  
  46.         printf("%d\n",sum);  
  47.     }  
  48.     return 0;  
  49. }  
http://www.acmsearch.com/article/show/20416
数据弱用网络流水过,Dinic只用了125ms,二分图多重匹配也行。

Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts