1. 程式人生 > >2018國慶第六場個人賽

2018國慶第六場個人賽

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;
}