国庆第六场训练赛

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

猜你喜欢

转载自blog.csdn.net/qq_38984851/article/details/82973693
今日推荐