国庆个人赛——NO.6

1、

Codeforces-714A

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 l1, r1, l2, r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018, l1 ≤ r1, l2 ≤ 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.

题意:

A要去看望B,B在【L1,L2】间清醒,但是要在k这一时间点化化妆不能见人

A非常忙,在【L3,L4】之间不工作它去看望A,求A、B待在一起的最长时间?

思路:

区间包不包含求长度的问题

注意k这一时间点,如果见面时间包含k的话,见面时间要减 1

首先有3种情况:

1、A、B区间不包含,此时见面时间为 0

2、A、B区间有一个全包含另一个,此时见面区间为长度最小的一个

3、A、B区间相交,求出相交的时间即可

首先说一下我的思路,我是直接去讨论的这三种情况,但是最后一种相交的又分为4种

L1\leftarrow \rightarrowL2       L1\leftarrow \rightarrowL2     L3\leftarrow \rightarrowL4         L3\leftarrow \rightarrowL4

   L3\leftarrow \rightarrowL4     L4\leftarrow \rightarrowL3                           L1 \leftarrow \rightarrowL2                       L2\leftarrow \rightarrowL1

我的这种笨方法写起来就比较麻烦,而且容易漏情况

正确思路:

左区间求一个较大的,右区间求一个较小的,这样返回的是包含区间的左右端点

如果你求的左端点比右端点还大的话,说明是不包含的情况

否则

如果k再此区间内,时间减1,否则输出该区间的长度

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int main()
{
    LL l,r,ll,rr,k;
    scanf("%lld %lld %lld %lld %lld",&l,&r,&ll,&rr,&k);

    LL xx,yy;
    xx=max(l,ll);
    yy=min(r,rr);

    LL ans;
    ans=yy-xx+1;

    if(yy<xx)
        printf("0\n");
    else
    {
        if(k>=xx&&k<=yy)
            ans-=1;
        printf("%lld\n",ans);
    }
}

2、

4、

Codeforces-714B

Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.

Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.

Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.

Output

If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).

Sample Input

Input

5
1 3 3 2 1

Output

YES

Input

5
1 2 3 4 5

Output

NO

Hint

In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.

题意:

给定 a1, a2, ..., an. 进行以下操作:

有的数加x(仅加一次),有的数减x(仅减一次),有的数不变化,最后使得所有的数都相等

如果可以实现的话,输出YES、否则输出NO

思路:

a1, a2, ..., an. 这一个串中,去掉重复的数字之后,(用unique()函数去重求个数)

如果个数大于 3的话,肯定变不成全部都相等的(因为只能加减一次)

如果个数 是3个的话,对三个数从小到大排序,满足等差数列,利用性质 a1 + a3 = 2*a2

    (不能用 a1+a3 对a2 求余,因为 1 2 5求余的话满足题意,但它并不是等差数列)

如果个数是1或者2的话,是满足题意得,输出YES

CODE:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

LL a[100000+10];

int main()
{
    LL n;

   cin>>n;


    LL sum=0,sum1=0;

    for(LL i=0; i<n; i++)
        cin>>a[i];

    if(n==1||n==2)
        cout<<"YES"<<endl;
    else
    {
        sort(a,a+n);

        sum=unique(a,a+n)-a;

        if(sum==1)
            cout<<"YES"<<endl;

        else
        {
            if(sum>3)
                cout<<"NO"<<endl;

            else
            {

                if(sum==2)
                {
                   cout<<"YES"<<endl;
                    return 0;
                }


                LL ans=0;

                ans=a[0]+a[2];

                
                if(ans==a[1]*2)
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }

    }

}

猜你喜欢

转载自blog.csdn.net/JKdd123456/article/details/82972585