2018国庆第六场个人赛

版权声明:转载时 别忘了注明出处 https://blog.csdn.net/ZCY19990813/article/details/82951882

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.

题意:soney L1和R1醒来 Filya在L2和R2去拜访他,k是soney没有时间的点,求两个人可以聊的时间

这个题有好多坑,要保证L1小于L2,分3种情况,没有交集,包含,有一点交集。然后判断k在不在这个范围内

幸亏自己考虑的情况多

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <deque>
#include <set>
using namespace std;
typedef long long ll;
#define MAX 1e5+10
map<ll,ll> M;
int main()
{
    ll l1,r1,l2,r2,k,ans=0;
    cin>>l1>>r1>>l2>>r2>>k;
    if(l1>l2)
    {
        swap(l1,l2);
        swap(r1,r2);
    }
    if(r1<l2)
        ans=0;
    if(r1==l2)
    {
        ans=1;
        if(k==r1)
            ans=0;
    }
    else
    if(l1<=l2&&r2<=r1)
    {
        ans=r2-l2+1;
        if(k>=l2&&k<=r2)
            ans-=1;
    }
    else
    {
        ans=r1-l2+1;
        if(k>=l2&&k<=r1)
            ans-=1;
    }
    if(ans>=0)
        cout<<ans<<endl;
    else
        cout<<"0"<<endl;
    return 0;
}

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.

题意:一组数,可以加x,也可以减x,但只能减一次,问是否可以全相等

思路:先去重,去重后的数组最多有3个数,有1个2个时一定可以,3个数时要判断是不是等差数列,其他都NO

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <deque>
#include <set>
using namespace std;
typedef long long ll;
#define MAX 1e5+10
map<ll,ll> M;
set<ll>s;
ll a[101000];
ll c[101010];
int main()
{
    ll n,i;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        s.insert(a[i]);
    }
    set<ll>::iterator it;
    ll e=0;
    for(it=s.begin();it!=s.end();it++)
    {
        c[e++]=*it;
    }
    //cout<<e<<endl;
    if(e>3)
        cout<<"NO"<<endl;
    else
    if(e<=2)
       cout<<"YES"<<endl;
    else
    {
        sort(c,c+e);
        if(c[0]+c[2]==c[1]*2)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ZCY19990813/article/details/82951882