1. 程式人生 > >國慶第六場訓練賽

國慶第六場訓練賽

A

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 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沒有時間的點,求兩個人可以聊的時間
思路:我們考慮當a1<a2時,我們要考慮後面的區間段b1和b2的誰短,然後才能求出區間,同時我們還要考慮的是k是否在這個區間內,如果在的話,需要進行-1
當a1>=a2時,同上考慮。
最後要考慮的是len有可能是負數,就是b1比a1小的情況。兩個區間沒有交集時

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
int main()
{
    LL a1,b1,a2,b2,k;
    LL len=0;
    LL a;
    cin>>a1>>b1>>a2>>b2>>k;
    if(a1<=a2)
    {
        a=min(b1,b2);
        len=a-a2+1;
        if(k>=a2&&k<=a)
            len=len-1;
    }
    else
    {
        a=min(b1,b2);
        len=a-a1+1;
        if(k>=a1&&k<=a)
            len=len-1;
    }
    if(len<0)
        cout<<"0"<<endl;
    else
    {
        cout<<len<<endl;
    }
    return 0;
}

D

Description

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.
題意:給你n個數字,找出一個數x,使他們能夠進行加減操作,使他們能夠相等。
思路:我們很容易分析出,如果超過三個數的話,我們不可能讓他們能夠加相等的數或者減相同的數使他們能夠相等。
滿足條件的一共三種情況:
①那麼如果n個數字全部相同的話,肯定滿足條件
②如果n個只有兩個數字不同的話,我們也可以判斷出來,用大數字減小數字就可以得出x
③三個數字的話,我們第三個數字+第一個數字=第二個數字*2
此題用set和map都可以,set比較好點,下面是兩個程式碼:

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
int main()
{
    LL n;
    LL a[100010],b[100010];
    map<LL,int> mp;
    while(cin>>n)
    {
        for(LL i=0;i<n;i++)
        {
            cin>>a[i];
            mp[a[i]]++;
        }
        map<LL,int>::iterator it;
        LL j=0;
        for(it=mp.begin();it!=mp.end();it++)
        {
            b[j]=it->first;
            j++;
        }
        if(j>3)
        {
            cout<<"NO"<<endl;
        }
        else
        {
            if(j<=2)
                cout<<"YES"<<endl;
            else
            {
                if(b[1]*2==b[2]+b[0])
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
    }
    return 0;
}

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
int main()
{
    LL n,i;
    LL a[100010],b[100010],len=0;
    set<LL> st;
    cin>>n;
    for(i=0;i<n;i++)
    {
         cin>>a[i];
         st.insert(a[i]);
    }
    set<LL>::iterator it;
    for(it=st.begin();it!=st.end();it++)
    {
        b[len++]=*it;
    }
    if(len>3)
        cout<<"NO"<<endl;
    else
    {
        if(len<=2)
            cout<<"YES"<<endl;
        else
        {
            if((b[0]+b[2])==(b[1]*2))
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }

    return 0;
}

c

Sequence Transformation

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Let’s call the following process a transformation of a sequence of length nn.

If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of nn integers: the greatest common divisors of all the elements in the sequence before each deletion.

You are given an integer sequence 1,2,…,n1,2,…,n. Find the lexicographically maximum result of its transformation.

A sequence a1,a2,…,ana1,a2,…,an is lexicographically larger than a sequence b1,b2,…,bnb1,b2,…,bn, if there is an index ii such that aj=bjaj=bj for all j<ij<i, and ai>biai>bi.

Input

The first and only line of input contains one integer nn (1≤n≤1061≤n≤106).

Output

Output nn integers — the lexicographically maximum result of the transformation.

Examples

input

Copy

3

output

Copy

1 1 3

input

Copy

2

output

Copy

1 2

input

Copy

1

output

Copy

1

Note

In the first sample the answer may be achieved this way:

Append GCD(1,2,3)=1(1,2,3)=1, remove 22.
Append GCD(1,3)=1(1,3)=1, remove 11.
Append GCD(3)=3(3)=3, remove 33.
We get the sequence [1,1,3][1,1,3] as the result.

這道題感覺思維性很強,要找規律。
題意:給你1~N,N個數,每次可以刪除一個數,在刪除數之前,把他們的GCD算出來,記錄到答案裡。問最後刪除完所有數後,答案列表的字典序最大的答案是多少
思路:我們首先考慮題目給的是刪除任意數字,我們可以考慮到如果讓字典序最大,我們應該先把奇數給刪除,因為奇數存在的話,gcd只能是1,所以我們先把奇數篩選出,讓其儘快出現gcd為:2,4,8,16,長度不夠要特殊判斷,因為3的時候需要刪除偶奇奇。
舉個例子:123,如果先刪除奇數的話得到的序列是112,我們刪除偶奇奇得到的是113,所以小於3要特判。
其他大神思路:有一條規則可以推出來,兩個連續的數的gcd是1,所以第一步是將原數列變成奇數數列或偶數數列,又因為對於長度n大於3時,偶數數列肯定要先出現大的gcd,所以第一步將原數列轉成偶數數列。

之後有趣的事情就出現了,可以發現,可以將形成的數列,奇數位上的數看“奇數數列”,偶數位上的數看成“偶數數列”,又重複第一步的過程。

在以上整個程中n都是大於3的,對於小於3的直接按“偶奇奇”的順序刪。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
int a[1000010];
int gcd(int a,int b)
{
    return a%b?a:gcd(b,a%b);
}
int main()
{
    int n;
    cin>>n;
    int k=1;
    while(n)
    {
        if(n==3)
        {
            cout<<k<<" "<<k<<" "<<k*3<<endl;
            return 0;
        }
        for(int i=0; i<(n+1)/2; i++)
            cout<<k<<" ";
        n/=2;
        k*=2;
    }
    return 0;
}

B

Description

Student Andrey has been skipping physical education lessons for the whole term, and now he must somehow get a passing grade on this subject. Obviously, it is impossible to do this by legal means, but Andrey doesn’t give up. Having obtained an empty certificate from a local hospital, he is going to use his knowledge of local doctor’s handwriting to make a counterfeit certificate(證明) of illness. However, after writing most of the certificate, Andrey suddenly discovered that doctor’s signature is impossible to forge. Or is it?

For simplicity, the signature(簽名) is represented as an n×m

xxx
x.x
xxx
Determine whether is it possible to forge the signature on an empty n×m

Input

The first line of input contains two integers n

Then n

Output

If Andrey can forge the signature, output “YES”. Otherwise output “NO”.

You can print each letter in any case (upper or lower).

Sample Input

Input
3 3

#.#

Output
YES
Input
3 3

Output
NO
Input
4 3

Output
YES
Input
5 7

.#####.
.#.#.#.
.#####.

Output
YES
Hint

In the first sample Andrey can paint the border of the square with the center in (2,2)

In the second sample the signature is impossible to forge.

In the third sample Andrey can paint the borders of the squares with the centers in (2,2)

we have a clear paper:





use the pen with center at (2,2)

#.#


use the pen with center at (3,2)

In the fourth sample Andrey can paint the borders of the squares with the centers in (3,3)

題意:要想去偽造醫生的簽名。醫生的簽名被簡化為中間一個點,周圍八個點為#的圖形,問所給的圖形能否被這個簽名填充。
思路:在給定的圖中尋找特定的點,該點的所有相鄰點均被塗色,即為#,找到該點。將另一張空白紙上的該點相鄰的點塗色,最後比較兩張圖是否相同。

#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#define ll long long int
using namespace std;
int n,m;
char a[1010][1010];
char b[1010][1010];
void init()///初始化b紙
{
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            b[i][j]='.';
        }
    }
}
int judge(int x,int y)///在a紙中尋找符合條件的點
{
    int i,j;
    for(i=-1; i<=1; i++)
    {
        for(j=-1; j<=1; j++)
        {
            if(i==0&&j==0)
            {
                continue;
            }
            if(a[x+i][y+j]=='.')///四周為#
            {
                return 0;
            }
        }
    }
    return 1;
}
void draw(int x,int y)///將a中符合條件的點,畫到b中
{
    int i,j;
    for(i=-1; i<=1; i++)
    {
        for(j=-1; j<=1; j++)
        {
            if(i==0&&j==0)
            {
                continue;
            }
            b[x+i][y+j]='#';
        }
    }
}
int compare_AB()///比較a、b兩張紙
{
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            if(a[i][j]!=b[i][j])
            {
                return 0;
            }
        }
    }
    return 1;
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        init();
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                scanf("%c",&a[i][j]);
            }
            getchar();
        }
        for(i=1; i<n-1; i++)
        {
            for(j=1; j<m-1; j++)
            {
                if(judge(i,j))
                {
                    draw(i,j);
                }
            }
        }
        if(compare_AB())
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}