国庆10.6

版权声明:莉莉莉 https://blog.csdn.net/qq_41700151/article/details/82983349

A-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.
一个猫头鹰在l1到r1时间内不睡觉,他的朋友l2到r2时间内来看她,但是在k分钟的时候她要收拾自己,问她俩能够会面的时间有多少,数据量很大,用longlong
简洁代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long

using namespace std;
int main()
{
    ll l1,l2,r1,r2,k;
    cin>>l1>>r1>>l2>>r2>>k;
    ll l=max(l1,l2);
    ll r=min(r1,r2);
    ll ans=0;
    if(r>=l)
        ans=r-l+1;
    if(k>=l&&k<=r)
        ans-=1;
    cout<<ans<<endl;



}

自己写的复杂代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
    ll l1,l2,r1,r2,k;
    ll sum;
    cin>>l1>>r1>>l2>>r2>>k;
    if(l1>=l2&&r2>=r1)
    {
        sum=r1-l1+1;
        if(k>=l1&&k<=r1)
            sum-=1;
        cout<<sum<<endl;
        return 0;
    }
    if(l1<=l2&&r2<=r1)
    {
        sum=r2-l2+1;
        if(k>=l2&&k<=r2)
            sum-=1;
        cout<<sum<<endl;
        return 0;
    }
    if(l1<l2&&r1<l2)
    {
        sum=0;
        cout<<sum<<endl;
        return 0;
    }
    if(l2<l1&&r2<l1)
    {
        sum=0;
        cout<<sum<<endl;
        return 0;
    }
    if(l2>=l1&&l2<=r1&&r2>=r1)
    {
        sum=r1-l2+1;
        if(k>=l2&&k<=r1)
            sum-=1;
        cout<<sum<<endl;
        return 0;

    }
    if(l1>=l2&&l1<=r2&&r2<=r1)
    {
        sum=r2-l1+1;
        if(k>=l1&&k<=r2)
            sum-=1;
        cout<<sum<<endl;
        return 0;
    }



}

B-CodeForces 1059B
题目戳这里

一个难理解的英文题,一个人能印印章,但是只能印
####.####这个样子的,点表示没有墨水,#表示有墨水,问他能否印出来。
遍历一遍所有的点,以这个点作为第一行第一列的那个点,找他周围是否都是#,如果都是就把这些点都标记上
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long

using namespace std;
string a[1005];
int vis[1005][1005];
int n,m;
void BFS(int x,int y)
{
    if(x+2>n||y+2>m||x+1>n||y+1>m)
        return ;
    if(a[x][y+1]=='.'||a[x][y+2]=='.'||a[x+1][y]=='.'||a[x+2][y]=='.'||a[x+2][y+1]=='.'||a[x+1][y+2]=='.'||a[x+2][y+2]=='.')
        return ;
    vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x][y+1]=vis[x+2][y+1]=vis[x][y+2]=vis[x+1][y+2]=vis[x+2][y+2]=1;

}
int main()
{

    scanf("%d%d",&n,&m);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        a[i]=" "+a[i];
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(a[i][j]=='#')
              BFS(i,j);

    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(a[i][j]=='#'&&vis[i][j]==0)
            {
                cout<<"NO"<<endl;
                return 0;
            }
    cout<<"YES"<<endl;


}

C—CodeForces 1059C
题目戳这里
题意:每次删除一个数然后求剩下的数的最大公约数,输出字典序最大的序列
这堆数里有奇数有偶数的时候最大公约数都是1,就是最小的字典序,当n>=3时,先把所有奇数都删除,这样就得到n/2个1,把剩下的那些偶数都除以2,再把他们的奇数全都删掉,再把剩的数都除以2
代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
const int MAX=1e6;
using namespace std;
int ans[MAX+5];
int d=0;
void f(int n,int flag)
{
    if(n==1)
    {
        ans[++d]=flag;
        return ;
    }
    else if(n==2)
    {
        ans[++d]=flag,ans[++d]=flag*2;
        return ;
    }
    else if(n==3)
    {
        ans[++d]=flag,ans[++d]=flag,ans[++d]=flag*3;
        return ;
    }
    for(int i=1; i<=n; i+=2)
    {
        ans[++d]=flag;
    }
    f(n>>1,flag*2);     //除以2是右移1呀~~用的不熟练,一开始一直写的>>2

}
int main()
{
    int n;
    ios::sync_with_stdio(false);
    cin>>n;
    memset(ans,0,sizeof(ans));
    f(n,1);
    cout<<ans[1];
    for(int i=2; i<=d; i++)
        cout<<" "<<ans[i];



}

D-CodeForces 714B
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个数全部相等
如果这一堆数里一共只有三种并且这三种数是等差数列,就可以实现,先去重,再判断是不是等差数列
代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
   map <int ,int> m;
   int n,a,flag=0;
   int s[4];
   memset(s,0,sizeof(s));
   ios::sync_with_stdio(false);
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a;
       m[a]++;
   }
   if(n==1||n==2)
   {
       cout<<"YES"<<endl;
       return 0;
   }
   int d=0;
   std::map<int,int>::iterator it;
   for(it=m.begin();it!=m.end();it++)
   {
       d++;
       s[d]=(*it).first;
       if(d>3)
       {
           flag=1;
           break;
       }
   }
  if(d>=3)
     if(s[1]+s[3]!=s[2]*2)
       flag=1;
   if(flag==0)
   cout<<"YES"<<endl;
   else
    cout<<"NO"<<endl;



}

猜你喜欢

转载自blog.csdn.net/qq_41700151/article/details/82983349