2020-2-8赛

*我也不知道这个是什么方法,但是很奇妙*

问题 J: Build Stairs

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

There are N squares arranged in a row from left to right. The height of the i-th square from the left is Hi.
For each square, you will perform either of the following operations once:
·Decrease the height of the square by 1.
·Do nothing.
Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Constraints
·All values in input are integers.
·1≤N≤105
·1≤Hi≤109

输入

Input is given from Standard Input in the following format:

N
H1 H2 ... HN

输出

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

样例输入 Copy

【样例1】
5
1 2 1 1 3
【样例2】
4
1 3 2 1
【样例3】
5
1 2 3 4 5
【样例4】
1
1000000000

样例输出 Copy

【样例1】
Yes
【样例2】
No
【样例3】
Yes
【样例4】
Yes

提示

样例1解释:You can achieve the objective by decreasing the height of only the second square from the left by 1.

#include<bits/stdc++.h>
using namespace std;
const int mod=1e5+5;
int A[100005],B[100005];
bool cmp(int x,int y)
{
    return x<y;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&A[i]);
        B[i]=A[i];
    }
    sort(B+1,B+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        if(fabs(B[i]-A[i])>1)
        {printf("No");
            return 0;}
    }
     printf("Yes");
    return 0;
}
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int mod=1e9+7;
const int N=1e5+5;
int a[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",a+i);
    }
    for(int i=1;i<n;i++){
        if(a[i]-a[i+1]>1){
            cout<<"No"<<endl;
            return 0;
        }
        if(a[i]<a[i-1]){
            cout<<"No"<<endl;
            return 0;
        }
        if(a[i]>a[i-1]){
            a[i]--;
        }
    }
    if(a[n]<a[n-1])cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
    return 0;
}

问题 F: Why Did the Cow Cross the Road II

时间限制: 5 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn. 
As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns exactly 26 cows, which he has lazily named A through Z (he is not sure what he will do if he ever acquires a 27th cow...), so there are precisely 52 crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the name of the cow for each crossing point, ultimately forming a string with 52 characters in which each letter of the alphabet appears exactly twice. He does not record which crossing points are entry points and which are exit points.

Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a,b) a "crossing" pair if cow a's path from entry to exit must cross cow b's path from entry to exit. Please help Farmer John count the total number of crossing pairs.

输入

The input consists of a single line containing a string of 52 upper-case characters. Each letter of the alphabet appears exactly twice.

输出

Please print the total number of crossing pairs.

样例输入 Copy

ABCCABDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ

样例输出 Copy

1

提示

In this example, only cows A and B are a crossing pair.

#include<bits/stdc++.h>
using namespace std;
const int mod=1e5+5;
char str[105];
struct node
{
    int st;
    int ed;
    int f;
}pos[26];
int main()
{
   scanf("%s",str);
   int cnt=0;
   int len=strlen(str);
   for(int i=0;i<len;i++)
   {
     if(pos[str[i]-'A'].f==0)
     {
         pos[str[i]-'A'].st=i+1;
         pos[str[i]-'A'].f=1;
     }
     else if(pos[str[i]-'A'].f==1) pos[str[i]-'A'].ed=i+1;
   }
//sort(pos,pos+26,cmp);
   for(int i=0;i<26;i++)
   {
       for(int j=0;j<26;j++)
       {
           if((pos[i].st<pos[j].st)&&(pos[j].st<pos[i].ed)&&(pos[j].ed>pos[i].ed))
           {
               cnt++;
           }
           else if((pos[i].st<pos[j].st)&&(pos[j].st<pos[i].ed)&&(pos[i].st>pos[j].ed))
           {
               cnt++;
           }
       }
   }
   printf("%d",cnt);
    return 0;
}

问题 B: 奇怪的数列

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

有这么一个奇怪的数列,当an是偶数的时候an+1=an/2 ;当an是奇数的时候an+1=3an+1。现在给出a1,当数列的第n项an=1时,我们称n为这个数列的回归数字。
比如说a1=22时,数列为:22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1......
这时候a16=1,16就是这个数列的回归数字。
经猜测每一个的a1 都存在对应的回归数字。
现在给出一个范围,当x≤a1≤y (1<x≤y≤3000000)时,求在这个范围里面的最大和最小的回归数字。

输入

有一行,包含整数x,y。

输出

有两个整数,用一空格隔开,表示x≤a1≤y (1<x≤y≤3000000)时,在这个范围里面的最大和最小的回归数字。

样例输入 Copy

【样例1】
1 10
【样例2】
100 200
【样例3】
900 1000

样例输出 Copy

【样例1】
1 20
【样例2】
8 125
【样例3】
16 174
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int mod=1e9+7;
const int N=105;
char a[N];
int ans[3000005];
int dfs(ll now)
{
    if(now<=3000000&&ans[now])
        return ans[now];
    if(now==1)
    {
        return 1;
    }
    ll t=now;
    if(now%2==0)
    {
        now/=2;
    }
    else
        now=now*3+1;
    if(t<=3000000)
    {
        ans[t]=dfs(now)+1;
        return ans[t];
    }
    else
        return dfs(now)+1;
}
int main()
{
    int l,r;
    int a=0x3f3f3f3f,b=0;
    //cout<<dfs(3000000)<<endl;
    scanf("%d%d",&l,&r);
    for(ll i=l; i<=r; i++)
    {
        a=min(a,dfs(i));
        b=max(b,dfs(i));
    }
    cout<<a<<" "<<b<<endl;
    return 0;
}
 

问题 K: Gathering Children

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

Given is a string S consisting of L and R.
Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.
The character written on the leftmost square is always R, and the character written on the rightmost square is always L.
Initially, one child is standing on each square.
Each child will perform the move below 10100 times:
Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.
Find the number of children standing on each square after the children performed the moves.

Constraints
·S is a string of length between 2 and 10^5 (inclusive).
·Each character of S is L or R.
·The first and last characters of S are R and L, respectively.

输入

Input is given from Standard Input in the following format:

S

输出

Print the number of children standing on each square after the children performed the moves, in order from left to right.

样例输入 Copy

【样例1】
RRLRL
【样例2】
RRLLLLRLRRLL
【样例3】
RRRLLRLLRRRLLLLL

样例输出 Copy

【样例1】
0 1 2 1 1
【样例2】
0 3 3 0 0 0 1 1 0 2 2 0
【样例3】
0 0 3 2 0 2 1 0 0 0 4 4 0 0 0 0

提示

样例解释:After each child performed one move, the number of children standing on each square is 0,2,1,1,1 from left to right.
After each child performed two moves, the number of children standing on each square is 0,1,2,1,1 from left to right.
After each child performed 10100 moves, the number of children standing on each square is 0,1,2,1,1 from left to right.

第一次暴力模拟 运行错误????
#include<bits/stdc++.h>
using namespace std;//100005
int    R[100010],L[100010];
int C[1005][100010];
int main()
{
 
    char str[100010];
    scanf("%s",str);
    int len=strlen(str),cnt=2;
    for(int i=0; i<len+2; i++)
        C[2][i]=1;
    while(1)
    {
        for(int i=0; i<len; i++)
        {
            if(str[i]=='R')
            {
                R[i+1]=C[cnt][i];
            }
            if(str[i]=='L')
            {
                L[i-1]=C[cnt][i];
            }
        }
 
        cnt++;
 
        for(int i=0; i<len; i++)
        {
            C[cnt][i]=R[i]+L[i];
            R[i]=0;
            L[i]=0;
        }
        int f=1;
       for(int i=0;i<len;i++)
       {
           if(C[cnt-2][i]!=C[cnt][i])
           {
               f=0;
               break;
           }
       }
       if(f==1)
        break;
    }
   // printf("%d\n",cnt);
    for(int i=0;i<len;i++)
    {
       if(cnt%2==1) printf("%d ",C[cnt-1][i]);
       else printf("%d ",C[cnt][i]);
    }
 
        return 0;
    }
 
/**************************************************************
    Problem: 14527
    User: 2019UPC110
    Language: C++
    Result: 运行错误
****************************************************************/
第二次错误,我也不知道咋地了。。。。
#include<bits/stdc++.h>
using namespace std;//100005
int   A[100010];
char str[100005];
int main()
{
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0; i<len; i++)
    {
        if(str[i]=='R'&&str[i+1]=='L')
        {

            int cnt1=0,cnt2=0;
           for(int j=i;j>=0;j--)
           {
              if(str[j]=='R') cnt1++;
              else break;
           }
           for(int j=i+1;j<len;j++)
           {
               if(str[j]=='L')cnt2++;
               else break;
           }
           if((cnt1+cnt2)%2==0)
            printf("%d %d ",(cnt1+cnt2)/2,(cnt1+cnt2)/2);
           else
           {
               if(cnt1%2==1)
               printf("%d %d ",max(cnt1,cnt2),min(cnt1,cnt2));
               else printf("%d %d ",min(cnt1,cnt2),max(cnt1,cnt2));

           }
           i++;
         }
        else
        {
           printf("0 ");
        }
    }


    return 0;
}
#include<bits/stdc++.h>
using namespace std;//100005
int   A[100010];
char str[100005];
int main()
{
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0; i<len; i++)
    {
        if(str[i]=='R'&&str[i+1]=='L')
        {

            int cnt1=0,cnt2=0;
           for(int j=i;j>=0;j--)
           {
              if(str[j]=='R') cnt1++;
              else break;
           }
           for(int j=i+1;j<len;j++)
           {
               if(str[j]=='L')cnt2++;
               else break;
           }
           if((cnt1+cnt2)%2==0)
            printf("%d %d ",(cnt1+cnt2)/2,(cnt1+cnt2)/2);
           else
           {
               if(cnt1%2==1)
               printf("%d %d ",(cnt1+cnt2)/2+1,(cnt1+cnt2)/2);
               else printf("%d %d ",(cnt1+cnt2)/2,(cnt1+cnt2)/2+1);

           }
           i++;
         }
        else
        {
           printf("0 ");
        }
    }


    return 0;
}
发布了32 篇原创文章 · 获赞 1 · 访问量 1340

猜你喜欢

转载自blog.csdn.net/QXK_Jack/article/details/104226072
今日推荐