经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
A.Activity planning - cafu-chino
来源:cnblogs  作者:cafu-chino  时间:2018/11/20 10:36:00  对本文有异议

题目描述
There is a collection of n activities E={1,2,..,n}, each of which requires the same resource, such as
a lecture venue, etc., and only one activity at a time Use this resource. Each activity i has a start
time of si and an end time of fi and si<fi. If the activity i is selected, it occupies resources within
the time interval [si,fi). If the interval [si,fi) does not intersect the interval [sj,fj), then the activity i is
said to be compatible with the activity j. That is, when fi<=sj or fj<=si, the activity i is compatible
with the activity j . Choose the largest collection of activities that are compatible with each other.
输入格式
The first line is an integer n;
The next n line, two integers per line, si and fi.
输出格式
Excluding mutual and compatible maximum active number.
样例输入1
4
1 3
4 6
2 5
1 7
样例输出1
2

数据范围与提示
1<=n<=1000

 

这道题解法与杭电今年暑假不AC解法相同,采用均摊法

代码实例

  1. #include<stdio.h>
  2.  
  3. struct huodong
  4. {
  5. int begin;
  6. int end;
  7. } J[1002];
  8. int main()
  9. {
  10. int n,i,j,sum,temp;
  11. sum = 1;
  12. scanf("%d",&n);
  13. for(i=0; i<n; i++)
  14. scanf("%d %d",&J[i].begin,&J[i].end);
  15. for(i=0; i<n-1; i++)
  16. {
  17. for(j=0; j<n-i-1; j++)
  18. {
  19. if(J[j].end>J[j+1].end)
  20. {
  21. temp = J[j].end;
  22. J[j].end = J[j+1].end;
  23. J[j+1].end = temp;
  24. temp = J[j].begin;
  25. J[j].begin = J[j+1].begin;
  26. J[j+1].begin = temp;
  27. }
  28. }
  29. }
  30. temp = J[0].end;
  31. for(i=1; i<n; i++)
  32. {
  33. if(J[i].begin>=temp)
  34. {
  35. sum++;
  36. temp = J[i].end;
  37. }
  38. }
  39. printf("%d\n",sum);
  40. return 0;
  41. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号