[关闭]
@zhshh 2018-07-13T05:13:32.000000Z 字数 5679 阅读 1425

贪心问题

@zhshh OI cpp 贪心


作业调度(区间不重叠覆盖问题)

回到顶部

特征

在一段长度内,有可选择的区间[a,b]的N段,求在这段长度内最大数目。

思路

先排序右区间,右区间相同排序左区间。
理论依据:从左往右来看,如果如果要尽可能多的安排,首先选择的应该是尽量早些结束的(对于剩下的相同的数据来说,结束早的带来的影响一定不劣于晚的。早些结束的有可能给更多的时间去结束别的)
因此排序时按照结束时间排序。而起始时间按照什么顺序排序似乎没有影响。(因为一定是从左往右扫描一遍的,选中那个一样)

例题

设有n个活动的集合E={1,2,…,n},其中每个活动都要求使用同一资源,如演讲会场等,而在同一时间内只有一个活动能使用这一资源。每个活动i都有一个要求使用该资源的起始时间si和一个结束时间fi,且si <fi。如果选择了活动i,则它在半开时间区间[si, fi)内占用资源。若区间[si, fi)与区间[sj, fj)不相交,则称活动i与活动j是相容的。也就是说,当si≥fj或sj≥fi时,活动i与活动j相容。活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合。 

如下代码。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. using namespace std;
  5. struct node{
  6. int b,f;
  7. node(){
  8. b=f=0;//begin finish
  9. }
  10. }a[1050];
  11. int n;
  12. int cmp(node x,node y){
  13. if(x.f!=y.f){
  14. return x.f<y.f;
  15. }else{
  16. return x.b<y.b;
  17. }
  18. }
  19. void init(){
  20. cin>>n;
  21. for(int i=1;i<=n;i++){
  22. cin>>a[i].b>>a[i].f;
  23. }
  24. }
  25. void work(){
  26. sort(a+1,a+n+1,cmp);
  27. int cnt=-1;
  28. int all=0;
  29. for(int i=1;i<=n;i++){
  30. if(a[i].b>=cnt){
  31. cnt=a[i].f;
  32. all++;
  33. }
  34. }
  35. cout<<all;
  36. }
  37. int main(){
  38. freopen("act.in","r",stdin);
  39. freopen("act.out","w",stdout);
  40. init();
  41. work();
  42. return 0;
  43. }
  44. /*
  45. 4
  46. 1 3
  47. 4 6
  48. 2 5
  49. 1 7
  50. */

区间种树问题(重叠覆盖重复计数多次)

回到顶部

特征

有重复区间,在这些重复区间内的数目被重复计算。求最小种树数目

思路

如果尽可能少,则应该优选重复尽可能多的。而且重复一定是在区间首尾。
从左往右计算,现在第一个区间尾种树。然后再计算差多少,就在尾部种多少。

例题

codevs1653
现在我们国家开展新农村建设,农村的住房建设纳入了统一规划,统一建设,政府要求每一住户门口种些树。门口路边的地区被分割成块,并被编号成1..N。每个部分为一个单位尺寸大小并最多可种一棵树。每个居民房子门前被指定了三个号码B,E,T。这三个数表示该居民想在B和E之间最少种T棵树。当然,B≤E,居民必须记住在指定区不能种多于区域地块数的树,所以T≤E-B+l。居民们想种树的各自区域可以交叉。你的任务是求出能满足所有要求的最少的树的数量,尽量较少政府的支出。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <string>
  5. #include <cstdio>
  6. #define MX 30010
  7. using namespace std;
  8. struct node {
  9. int b,e,w;
  10. }a[MX];
  11. int n,m;
  12. int tr[MX];
  13. void init(){
  14. cin>>m>>n;
  15. for(int i=1;i<=n;i++){
  16. cin>>a[i].b>>a[i].e>>a[i].w;
  17. }
  18. }
  19. int cmp(node x,node y){
  20. if(y.e!=x.e){
  21. return x.e<y.e;
  22. }else{
  23. return x.b<y.b;
  24. }
  25. }
  26. void work(){
  27. int tot=0;
  28. sort(a+1,a+n+1,cmp);
  29. for(int i=1;i<=n;i++){
  30. for(int j=a[i].e;j>=a[i].b;j--){
  31. if(tr[j]==1){
  32. a[i].w--;
  33. }
  34. }
  35. for(int j=a[i].e;j>=a[i].b;j--){
  36. if(tr[j]==0 && a[i].w>0){
  37. a[i].w--;
  38. tr[j]=1;
  39. tot++;
  40. }
  41. if(a[i].w==0){
  42. break;
  43. }
  44. }
  45. }
  46. // for(int i=1;i<=m;i++){
  47. // printf("%3d",i);
  48. // }
  49. // cout<<endl;
  50. // for(int i=1;i<=m;i++){
  51. // printf(" %c"," *"[tr[i]]);
  52. // }
  53. cout<<tot;
  54. }
  55. int main(){
  56. init();
  57. work();
  58. return 0;
  59. }

喷水问题(区间连续覆盖问题(多端区间连续覆盖到一定长度,求最小数目))

回到顶部

特征

多个固定可选的区间。选择最少数目覆盖连续的一段长度。

思路




例题

一块长l,宽w的长方形草坪,在其中心线的不同位置处装有n个点状的喷水装置。每个喷水装置i可将以它为中心,半径为ri的圆形区域润湿。请选择尽量少的喷水装置,把整个草坪全部润湿。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. int n,l,w,tot=0;
  8. struct node{
  9. double mid;
  10. double r;
  11. int used;
  12. double left,right;
  13. }a[10010];
  14. void init(){
  15. n=l=w=tot=0;
  16. memset(a,0,sizeof(a));
  17. int temp,t2;
  18. cin>>n>>l>>w;
  19. for(int i=1;i<=n;i++){
  20. cin>>t2>>temp;
  21. tot++;
  22. a[tot].mid=t2;
  23. if(temp*1.0>=w*1.0/2){
  24. a[tot].r=sqrt(1.0*temp*temp-1.0*w*w/4);
  25. a[tot].left=a[tot].mid-a[tot].r;
  26. a[tot].right=a[tot].mid+a[tot].r;
  27. }else{
  28. tot--;
  29. }
  30. }
  31. return ;
  32. }
  33. //int cmp(node a,node b){
  34. // if(a.right!=b.right){
  35. // return a.right<b.right;
  36. // }else{
  37. // return a.left
  38. // }
  39. //}
  40. void work(){
  41. // sort(a+1,a+tot+1,cmp);
  42. double fns=0;
  43. int mx=0;
  44. double mxx=0;
  45. int ans=0;
  46. while(ans!=tot){
  47. mx=0;
  48. mxx=fns;
  49. for(int i=1;i<=tot;i++){
  50. if(!a[i].used && a[i].left<=fns){
  51. if(mxx<=a[i].right){
  52. mx=i;
  53. mxx=a[i].right;
  54. }
  55. }
  56. }
  57. if(mx!=0){
  58. a[mx].used=1;
  59. // cout<<"mark"<<mx<<endl;
  60. fns=mxx;
  61. ans++;
  62. }
  63. if(fns>=l){
  64. cout<<ans<<endl;
  65. return ;
  66. }else{
  67. if(ans==tot || (ans<tot && mx==0)){
  68. cout<<-1<<endl;
  69. return ;
  70. }
  71. }
  72. }
  73. if(fns>=l){
  74. cout<<ans<<endl;
  75. }else{
  76. if(ans==tot){
  77. cout<<-1;
  78. return ;
  79. }
  80. }
  81. }
  82. int main(){
  83. int NNN;
  84. freopen("data.in","r",stdin);
  85. freopen("data.out","w",stdout);
  86. cin>>NNN;
  87. for(int i=1;i<=NNN;i++){
  88. init();
  89. work();
  90. }
  91. return 0;
  92. }

家庭作业(相同时长任务,收益和截止期限不同。求最大收益)

回到顶部

特征

任务时长相同。但是收益不同,且截止日期不同。合理安排时间,得到最大收益。

思路

按照收益排序。然后优选收益最大的,从后往前安排(尽量少挤占空间),如果安排不下就放弃换任务。
其中如果要有优化,就是使用链表(数组模拟)快速跳过已经安排的位置。

例题

老师在开学第一天就把所有作业都布置了,每个作业如果在规定的时间内交上来的话才有学分.每个作业的截止日期和学分可能是不同的
.例如如果一个作业学分为10,要求在6天内交,那么要想拿到这10学分,就必须在第6天结束前交.
每个作业的完成时间都是只有一天.例如,假设有7次作业的学分和完成时间如下:
1 2 3 4 5 6 7
期限 1 1 3 3 2 2 6
学分 6 7 2 1 4 5 1
最多可以获得15学分,其中一个完成作业的次序为2,6,3,1,7,5,4,注意可能还有其他方法.
你的任务就是找到一个完成作业的顺序获得最大学分.

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #define MX 1000100
  5. using namespace std;
  6. struct node{
  7. int w;
  8. int e;
  9. }a[MX];
  10. int pre[MX]={0};
  11. int pos[MX]={0};
  12. int n,m;
  13. int operator < (const node a,const node b){
  14. if(a.w!=b.w){
  15. return a.w>b.w;
  16. }else{
  17. return a.e<b.e;
  18. }
  19. }
  20. void init(){
  21. cin>>n;
  22. for(int i=1;i<=n;i++){
  23. // cin>>a[i].e>>a[i].w;
  24. scanf("%d%d",&a[i].e,&a[i].w);
  25. m=max(m,a[i].e);
  26. }
  27. for(int i=0;i<=m+1;i++){
  28. pre[i]=i-1;
  29. pos[i]=i+1;
  30. }
  31. }
  32. void work(){
  33. int ans=0;
  34. int mxj=0,mxv=0;
  35. sort(a+1,a+n+1);
  36. for(int i=1;i<=n;i++){
  37. int v=a[i].e;
  38. while(pre[v]!=-1){
  39. }
  40. if(v==-1){
  41. continue;
  42. }else{
  43. pre[pos[v]]=pre[v];
  44. pos[pre[v]]=pos[v];
  45. }
  46. }
  47. cout<<ans;
  48. }
  49. int main(){
  50. init();
  51. work();
  52. return 0;
  53. }

钓鱼(。。。描述不出来)

回到顶部

特征

在同一个点可以长时间钓鱼(单位时间收益递减)或者通过一定路程(N个单位时间损耗)换一个钓鱼点
钓鱼点线性排列,一个挨着一个。

思路

分情况贪心。。依次不走/走一个/走两个,先刨去走路时间(常数)。。。。。进行贪心,每次选取收益最大值并更新这一组数,如果最大收益为负数即可停止。之后依次比较每次最大收益取最大值

例题

在一条水平路边,有n(2<=n<=100)个钓鱼湖,从左到右编号为1、2、…、n。佳佳有H(1<=H<=20)个小时的空于时间,他希望利用这个时间钓到更多的鱼。他从1出发,向右走,有选择的在一些湖边停留一定的时间(是5分钟的倍数)钓鱼。最后在某一个湖边结束钓鱼。佳佳从第i个湖到第i+1个湖需要走5*Ti分钟路,还测出在第i个湖停留,第一个5分钟可以钓到Fi条鱼,以后每再钓5分钟,鱼量减少Di。为了简化问题,佳佳假定没有其他人钓鱼,也没有其他因素影响他钓到期望数量的鱼。请编程求出佳佳最多能钓鱼的数量。

  1. #include <iostream>
  2. #include <queue>
  3. #include <algorithm>
  4. using namespace std;
  5. int n,h;
  6. int a[110],b[110],c[110];
  7. struct node{
  8. int x,d;//number && decrease
  9. void dec(){
  10. x-=d;
  11. }
  12. node(int a,int b){
  13. x=a;
  14. d=b;
  15. }
  16. };
  17. int operator <(node a,node b){
  18. return a.x<b.x;
  19. }
  20. priority_queue<node>q;
  21. void init(){
  22. cin>>n>>h;
  23. h*=12;
  24. for(int i=1;i<=n;i++){
  25. cin>>a[i];
  26. }
  27. for(int i=1;i<=n;i++){
  28. cin>>b[i];
  29. }
  30. for(int i=2;i<=n;i++){
  31. cin>>c[i];
  32. }
  33. }
  34. int workone(int x){//if goto x pools ,spend this time can do much is
  35. int tme=h;
  36. int num=0;
  37. for(int i=1;i<=x;i++){
  38. tme-=c[i];
  39. }
  40. for(int i=1;i<=x;i++){
  41. q.push(node(a[i],b[i]));
  42. }
  43. // cout<<"qwe"<<tme<<endl;
  44. while(tme>0){
  45. node temp=q.top();
  46. q.pop();
  47. if(temp.x<=0){
  48. break;
  49. }
  50. num+=temp.x;
  51. temp.dec();
  52. q.push(temp);
  53. tme--;
  54. }
  55. return num;
  56. }
  57. void work(){
  58. int qwe=0;
  59. for(int i=1;i<=n;i++){
  60. qwe=max(workone(i),qwe);
  61. }
  62. cout<<qwe;
  63. }
  64. int main(){
  65. init();
  66. work();
  67. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注