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
  • 相关阅读:
    阿里播放器踩坑记录 进度条重构 video loadByUrl失效解决方案
    liunx 安装nc/netcat centos安装netcat
    jquery实现显示textarea输入字符数
    SQL 时间戳转换为日期
    .Net WebRequest异步请求与WebClient异步请求
    SQL删除多列语句
    jQuery为元素设置css的问题
    关于调试WCF时引发的异常XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side
    SQL删除指定条件的重复数据,只保留一条
    net.exe use命令的使用
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9751392.html
Copyright © 2011-2022 走看看