• USACO Training 1.3 Milking Cows


    题干

    Milking Cows

    Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).

    Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):

    • The longest time interval at least one cow was milked.
    • The longest time interval (after milking starts) during which no cows were being milked.

    NOTE: Milking from time 1 through 10, then from time 11 through 20 counts as two different time intervals.

    PROGRAM NAME: milk2

    INPUT FORMAT

    Line 1:The single integer, N
    Lines 2..N+1:Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500

    SAMPLE INPUT (file milk2.in)

    3
    300 1000
    700 1200
    1500 2100
    
    

    OUTPUT FORMAT

    A single line with two integers that represent the longest continuous time of milking and the longest idle time.

    SAMPLE OUTPUT (file milk2.out)

    900 300

    思路

    我们读取时间列表,按开始时间排序,然后遍历列表一次,合并重叠时间。然后我们遍历列表,观察长时间的挤奶期和长时间的非挤奶期。

    另一种方法是只保留一个大小为一百万的数组并标记时间。在一个不错的快速处理器上,这可能已经足够快了,但是我们上面的算法即使在慢速处理器上也能工作,而且编写起来并不难。

    解题

    1. /*
    2. ID: choiyin1
    3. LANG: C++
    4. PROG: milk2
    5. */
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. int cmp(int a,int b){
    11. return a<=b;
    12. }
    13. int main(){
    14. // freopen("milk2.in","r",stdin);
    15. // freopen("milk2.out","w",stdout);
    16. int n;
    17. cin>>n;
    18. int begin[6000];
    19. int end[6000];
    20. begin[0]=0;
    21. end[0]=0;
    22. for(int i=1;i<=n;i++){
    23. scanf("%d %d",&begin[i],&end[i]);
    24. }
    25. sort(begin,begin+n+1,cmp);
    26. sort(end,end+n+1,cmp);
    27. begin[0]=begin[1];
    28. end[0]=end[1];
    29. sort(begin,begin+n+1,cmp);
    30. sort(end,end+n+1,cmp);
    31. int have[6000];
    32. int havent[6000];
    33. for(int i=1;i<=n;i++){
    34. have[i]=0;
    35. }
    36. for(int i=1;i<=n;i++){
    37. havent[i]=0;
    38. }
    39. for(int i=1;i<=n;i++){
    40. if(end[i-1]>=begin[i]){
    41. begin[i]=begin[i-1];
    42. }
    43. have[i]=end[i]-begin[i];
    44. }
    45. for(int i=1;i<=n;i++){
    46. havent[i]=begin[i]-end[i-1];
    47. }
    48. sort(have,have+n+1,cmp);
    49. sort(havent,havent+n+1,cmp);
    50. if(havent[n]<0){
    51. havent[n]=0;
    52. }
    53. cout<" "<
    54. return 0;
    55. }
  • 相关阅读:
    QT基础教程(QT中的文件操作)
    Win10右键 nvidia rtx desktop manager 怎么删除(最新)
    【NSDictionary数组的使用 Objective-C语言】
    为什么要学习Flink系统管理及优化课程?
    基于php工厂车辆登记系统
    防火墙双机热备实验
    Java版工程行业管理系统源码-专业的工程管理软件- 工程项目各模块及其功能点清单
    PostgresSQL----基于Kubernetes部署PostgresSQL
    详解企业财务数字化转型路径|推荐收藏
    利用excel表格进行分包和组包
  • 原文地址:https://blog.csdn.net/GeekAlice/article/details/126703951