poj3614 二分图最大匹配 or 贪心

我不是女神ヾ 2022-08-20 06:16 270阅读 0赞

Sunscreen














Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5926   Accepted: 2072

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; minSPFimaxSPFi ≤ 1,000) that will work. If the SPF 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

  1. 3 2
  2. 3 10
  3. 2 5
  4. 1 5
  5. 6 2
  6. 4 1

Sample Output

  1. 2

题意: 给定n个区间, m个值以及这个值对应的数目, 求区间与值的最大匹配;

分析:开始看到n不是太大, 果断想到了用二分图的最大匹配, 只是这个匹配是有数目要求的,

但是变化一下就行了.

  1. #include<bitset>
  2. #include<map>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<string>
  8. #include<algorithm>
  9. #include<cmath>
  10. #include<stack>
  11. #include<queue>
  12. #include<set>
  13. #define inf 0x3f3f3f3f
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15. using namespace std;
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18. typedef pair<int,int> pii;
  19. inline int in()
  20. {
  21. int res=0;char c;int f=1;
  22. while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
  23. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  24. return res*f;
  25. }
  26. const int N=2505;
  27. int n,m,flag,vis[N];
  28. pii a[N],b[N];
  29. vector<int> v[N],match[N]; //match不能定义成数组了, 要用邻接矩阵的形式, 因为每个值可能匹配多个区间
  30. vector<int>::iterator it;
  31. bool dfs(int x)
  32. {
  33. for(int i=0;i<v[x].size();i++)
  34. {
  35. int t=v[x][i];
  36. if(vis[t]==flag) continue;
  37. vis[t]=flag;
  38. if(match[t].size()==0 && b[t].second > 0) //没匹配, 数目也大于0
  39. {
  40. match[t].push_back(x);
  41. b[t].second--;
  42. return 1;
  43. }
  44. if(match[t].size()>0 && b[t].second > 0) //匹配了, 但是数目还是大于0
  45. {
  46. match[t].push_back(x);
  47. b[t].second--;
  48. return 1;
  49. }
  50. if(match[t].size()>0 && b[t].second == 0) //遍历匹配这个值的每个区间, 看是否还有增广路
  51. {
  52. for(int j=0;j<match[t].size();j++)
  53. {
  54. int y=match[t][j];
  55. if(dfs(y))
  56. {
  57. match[t].push_back(x); //有的话 就把这个区间push进去
  58. for(it=match[t].begin();;it++)
  59. {
  60. if(*it==y)
  61. {
  62. match[t].erase(it); //同时还要删除让地方的区间
  63. break;
  64. }
  65. }
  66. return 1;
  67. }
  68. }
  69. }
  70. }
  71. return 0;
  72. }
  73. int main()
  74. {
  75. while(~scanf("%d%d",&n,&m))
  76. {
  77. for(int i=1;i<=n;i++)
  78. {
  79. a[i].first=in();
  80. a[i].second=in();
  81. }
  82. for(int i=1;i<=m;i++)
  83. {
  84. b[i].first=in();
  85. b[i].second=in();
  86. }
  87. for(int i=1;i<=n;i++)
  88. {
  89. for(int j=1;j<=m;j++)
  90. {
  91. if(b[j].first>=a[i].first && b[j].first<=a[i].second)
  92. {
  93. v[i].push_back(j);
  94. }
  95. }
  96. }
  97. mem(vis,0);
  98. int ans=0;
  99. flag=0;
  100. for(int i=1;i<=n;i++)
  101. {
  102. flag=i;
  103. if(dfs(i)) ans++;
  104. }
  105. printf("%d\n",ans);
  106. for(int i=1;i<=n;i++) v[i].clear(),match[i].clear();
  107. }
  108. return 0;
  109. }

贪心策略: 把区间按照左边界排序, 把值按照大小排序, 然后遍历每个值, 把左边界小于当前值的区间全部扔到multiset中, 每次取右边界距离当前的值最近的区间进行贪心,

因为值是逐渐增大的, 右边界越大的区间, 选择也就越大.

  1. #include<bitset>
  2. #include<map>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<string>
  8. #include<algorithm>
  9. #include<cmath>
  10. #include<stack>
  11. #include<queue>
  12. #include<set>
  13. #define inf 0x3f3f3f3f
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15. using namespace std;
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18. typedef pair<int,int> pii;
  19. inline int in()
  20. {
  21. int res=0;char c;int f=1;
  22. while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
  23. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  24. return res*f;
  25. }
  26. const int N=2505;
  27. pii a[N],b[N];
  28. multiset<int > s;
  29. int main()
  30. {
  31. int n,m;
  32. while(~scanf("%d%d",&n,&m))
  33. {
  34. s.clear();
  35. for(int i=0;i<n;i++)
  36. {
  37. a[i].first=in();
  38. a[i].second=in();
  39. }
  40. for(int i=0;i<m;i++)
  41. {
  42. b[i].first=in();
  43. b[i].second=in();
  44. }
  45. sort(a,a+n);
  46. sort(b,b+m);
  47. int ans=0,j=0;
  48. for(int i=0;i<m;i++)
  49. {
  50. for(;j<n && a[j].first<=b[i].first;j++)
  51. {
  52. s.insert(a[j].second);
  53. }
  54. for(;b[i].second && !s.empty();)
  55. {
  56. if(*s.begin() < b[i].first) //如果区间的右边比当前值还要小, 那么直接丢弃, 匹配不了.
  57. {
  58. s.erase(s.begin());
  59. continue;
  60. }
  61. ans++;
  62. s.erase(s.begin()); //当前值匹配右边界距离当前值最近的点;
  63. b[i].second--;
  64. }
  65. }
  66. printf("%d\n",ans);
  67. }
  68. return 0;
  69. }

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 [二分]匹配

    二分图的定义,以及判断图是否为二分图都很简单了。 现在要说二分图的最大匹配。 首先是定义吧,完美匹配就是一一对应,而最大匹配则是最大可以匹配的条数 完美匹配一定是最大匹配