zoukankan      html  css  js  c++  java
  • CodeForces 714A

    Description

    Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya!

    Sonya is an owl and she sleeps during the day and stay awake from minute l1 to minute r1 inclusive. Also, during the minute k she prinks and is unavailable for Filya.

    Filya works a lot and he plans to visit Sonya from minute l2 to minute r2 inclusive.

    Calculate the number of minutes they will be able to spend together.

    Input

    The only line of the input contains integers l1r1l2r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018l1 ≤ r1l2 ≤ r2), providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks.

    Output

    Print one integer — the number of minutes Sonya and Filya will be able to spend together.

    Sample Input

    Input
    1 10 9 20 1
    Output
    2
    Input
    1 100 50 200 75
    Output
    50

    Hint

    In the first sample, they will be together during minutes 9 and 10.

    In the second sample, they will be together from minute 50 to minute 74 and from minute 76 to minute 100.

    算是一道非常非常简单的贪心做法题吧。

    但我还是WA了无数发,只因为那个被我犯过好几次的错误,不是第一次犯这种错了。

    只考虑到了两个区间相交和相离的情况,没有考虑到内含的情况。

    这题应该注意的是k是在相交区间断点的问题。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<stack>
     8 #include<deque>
     9 #include<iostream>
    10 using namespace std;
    11 typedef long long  LL;
    12 
    13 struct con{
    14     LL head;
    15     LL tail;
    16 }con[10];
    17 LL cmp(struct con a,struct con b)
    18 {
    19     if(a.head==b.head)
    20         return a.tail<b.tail;
    21     return a.head<b.head;
    22 }
    23 int main()
    24 {
    25     LL i,p,j,n;
    26     LL k,ans=0;
    27 //    scanf("%lld",&p);
    28 //    printf("%lld
    ",p);
    29     for(i=0;i<=1;i++)
    30     {
    31         scanf("%lld%lld",&con[i].head,&con[i].tail);
    32     }
    33     scanf("%lld",&k);
    34     sort(con,con+2,cmp);
    35 
    36     if(con[1].head<=con[0].tail&&(k>con[0].tail||k<con[1].head))
    37     {
    38         ans=con[0].tail-con[1].head+1;
    39     }
    40     if(con[1].head<=con[0].tail&&(k<=con[0].tail&&k>=con[1].head))
    41     {
    42         ans=con[0].tail-con[1].head;
    43     }
    44     if(con[0].tail>=con[1].tail&&(k>con[1].tail||k<con[1].head))
    45     {
    46         ans=con[1].tail-con[1].head+1;
    47     }
    48     if(con[0].tail>=con[1].tail&&(k<=con[1].tail&&k>=con[1].head))
    49     {
    50         ans=con[1].tail-con[1].head;
    51     }
    52     printf("%lld
    ",ans);
    53     return 0;
    54 }
    View Code
  • 相关阅读:
    斯坦福第十六课:推荐系统(Recommender Systems)
    斯坦福第十五课:异常检测(Anomaly Detection)
    斯坦福第十四课:降维(Dimensionality Reduction)
    斯坦福第十三课:聚类(Clustering)
    斯坦福第十二课:支持向量机(Support Vector Machines)
    斯坦福第十一课:机器学习系统的设计(Machine Learning System Design)
    斯坦福第十课:应用机器学习的建议(Advice for Applying Machine Learning)
    斯坦福第九课:神经网络的学习(Neural Networks: Learning)
    斯坦福第八课:神经网络表述(Neural Networks: Representation)
    斯坦福第七课:正则化(Regularization)
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9751392.html
Copyright © 2011-2022 走看看