黑龙江大学程序设计竞赛(重现赛) 个人笔记 题解

版权声明:点个关注(^-^)V https://blog.csdn.net/weixin_41793113/article/details/89923531

题目链接:https://ac.nowcoder.com/acm/contest/877#question

A.Find the Nth Character

链接:https://ac.nowcoder.com/acm/contest/877/A
来源:牛客网
 

题目描述

今天在给

的同学们上程序算法课的时候出了一道找规律的题目,题目表述如下

假设:

现在要求上课的同学们把所有的串依次连接起来,于是得到:

那么你能告诉串中的第个字母是多少吗?
 


 

输入描述:

 

输入首先是一个数字,代表有次询问

接下来的行每行有一个整数

输出描述:

 

对于每次询问,输出串中第个位置对应的字母。

示例1

输入

复制

6
1
2
3
4
5
10

输出

复制

a
a
b
a
b
d
#include<iostream>
#include<cstdio>
using namespace std;

char ch[10555];

int main(){
    int tot=0;

    while(tot<=10000)
    for(int i=1;i<=1000;i++){
        for(int j=0;j<i;j++){
            ch[++tot] = (char)('a'+j%26);
            if(tot>10000) break;
        }
            if(tot>10000) break;
    }


    int T,n;
    scanf("%d",&T);

    while(T--){
        scanf("%d",&n);
        printf("%c\n",ch[n]);
    }

	return 0;
}

B.Help Me

链接:https://ac.nowcoder.com/acm/contest/877/B
来源:牛客网
 

题目描述

有一天Miss Quan出了一个题,给出一个长度为的数组

Miss Quan说给我计算下这个权值等于多少,然后随手写了一个式子,把她的小伙伴们都给惊呆了,这是什么鬼......

比如

听到这里,小伙伴们说这不是很简单吗,于是写出了下面的代码:

Miss Quan会心一笑,大家意识到事情并不简单,这个解法时间复杂度太高了,你能想出更好的计算Value的方法吗?只需要输出Value的值即可。

输入描述:

 

第一行输入一个整数代表共有组数据

对于每组测试用例第一行输入一个整数第二行输入个数

输出描述:

 

对于每组数据,输出一行,一个整数代表的值.

示例1

输入

复制

2
3
1 4 2
2
10 20

输出

复制

14
100

示例2

输入

复制

1
4
5 6 8 7

输出

复制

20

 思路 : 比如4个数a_{1}a_{2}a_{3}a_{4},答案应该是它们(a_{1}-a_{2})^{2},(a_{1}-a_{4})^{2},(a_{2}-a_{4})^{2},(a_{3}-a_{4})^{2},(a_{1}-a_{3})^{2},(a_{2}-a_{3})^{2}的和,我们拆开发现,(n-1)*(a_{1}^{2}+a_{2}^{2}+a_{3}^{2}+a_{4}^{2})-2a_{1}a_{2}-(a_{1}+a_{2})*a_{3}-(a_{1}+a_{2}+a_{3})*a_{4},这样我们维护个前缀和就好了。

前缀和 找规律+思维,空间不爆就longlong

#include<iostream>
#include<cstdio>
using namespace std;

const int MAX  = 5*1e5+5;
typedef long long ll;
ll sum[MAX],a[MAX];
int T,n;

int main(){
    scanf("%d",&T);
    while(T--){
        sum[0]=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            sum[i] = sum[i-1] + a[i];
        }
        ll ans = (n-1)*a[1]*a[1];
        for(int i=2;i<=n;i++){
            ans+=(n-1)*a[i]*a[i];
            ans+=(-2)*sum[i-1]*a[i];
        }

        printf("%lld\n",ans);
    }

	return 0;
}

C.Challenge IQ

链接:https://ac.nowcoder.com/acm/contest/877/C
来源:牛客网
 

题目描述

爱的魔力转圈圈,想你想到心花怒放黑夜白天,可是我害怕爱情只是一瞬间,转眼会不见,我要慢慢冒险。经过了无数的思想斗争,他要做出这个决定,和喜欢的女孩子表白,但女孩只是留给他一个排列,排列的定义是一个由组成的序列每个数出现并且只出现1次。

现在他需要把个数通过一定顺序首尾连接形成一个圆环,使得相邻元素互质的对数尽可能多,请输出最大的对数。两个数互质的定义是这两个数的GCD(最大公约数)为1。比如6和4的最大公约数为2,不互质。4和3的最大公约数为1,互质。

输入描述:

 

第一行是一个数字,表示共有T组测试数据.

接下来行,每行一个数字.

输出描述:

一行一个整数表示答案

示例1

输入

复制

2
3
5

输出

复制

3
5

不知道想考什么,就普通的排序1,2,3,...,n都已经是n了 

#include<iostream>
#include<cstdio>
using namespace std;

int main(){
    int T,n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        printf("%d\n",n);
    }

	return 0;
}

D.Schedules

链接:https://ac.nowcoder.com/acm/contest/877/D
来源:牛客网
 

题目描述

输入描述:

 
 

输出描述:

 
 

示例1

输入

复制

3
1 3
4 6
2 5

输出

复制

2

示例2

输入

复制

2
0 4
4 5

输出

复制

1

示例3

输入

复制

4
2 4
1 3
5 9
3 8

输出

复制

2

E.Pig-Palindromic

 链接:https://ac.nowcoder.com/acm/contest/877/E
来源:牛客网
 

题目描述

输入描述:

 
 

输出描述:

 
 

示例1

输入

复制

AabBAD

输出

复制

4

示例2

输入

复制

ACda

输出

复制

0

示例3

输入

复制

CCcc

输出

复制

4

示例4

输入

复制

A

输出

复制

0

类最长回文子串,中心扩展 

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;

int main(){
    string s;
    cin>>s;
    int n=s.size();
    int ans=0;

    for(int i=0;i<n;i++){
        int res=0;
        for(int j=i,k=i+1;j>=0&&k<n;j--,k++)//中心扩展,类回文串处理
            if(abs(s[j]-s[k])==32)
                res+=2;
            else
                break;

        ans = max(ans,res);
    }

    cout<<ans<<endl;
	return 0;
}

F.GCD Problem

链接:https://ac.nowcoder.com/acm/contest/877/F
来源:牛客网
 

题目描述

输入描述:

 

输出描述:

 
 

示例1

输入

复制

4
5 45 20 65
7
1 1 4
0 1 4
1 1 4
1 3 4
0 3 4
1 3 4
1 2 2

输出

复制

5
2
4
2
6

G.Bash Game

链接:https://ac.nowcoder.com/acm/contest/877/G
来源:牛客网
 

题目描述

The two new cute boys(Alice and Bob) in the ACM group of HLJU science and technology association have been dreaming of getting these six books written by Mr Jin.

As we all know, there is no such thing as a free lunch. Mr Jin now lets Alice and Bob play a game,and only the winner of the game can get these six books.The rules of the game are as follows.

 Suppose(假设) the price of these six books is P, two people take turns in bidding(竞拍), Suppose one party(一方) bid A yuan during the bidding process and the other party(另一方) bid B yuan. Rules require . In this way, the bidding goes on in turn until the price of one party is greater than or equal toP,the party fails and the game ends, Mr Jin awarded these books to another party.

Alice first bid, Bob bid behind him every time, and Alice and Bob use the best strategy(最优策略), who can get these books?

输入描述:

 
 

输出描述:

 
 

示例1

输入

复制

4
1 1
20 6
10 2
23 7

输出

复制

Bob
Alice
Bob
Alice

巴什博奕,模板是 p%(m+1)==0,大概靠一下就能出答案了

#include<iostream>
#include<cstdio>
using namespace std;

int main(){
    int T,p,m;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&p,&m);
        if(p%(m+1)==1)
            printf("Bob\n");
        else
            printf("Alice\n");
    }

	return 0;
}

H.Pass CET 6

链接:https://ac.nowcoder.com/acm/contest/877/H
来源:牛客网
 

题目描述

Marzz liked a strange way of memorizing words.

 Now these words are placed in an  Matrix. He decided to memorize only one line or one column of words every day. She has memorized days in total, and now she wants to know: for each word, when was the last time she memorized it?

输入描述:

 
 

输出描述:

 
 

示例1

输入

复制

3 3 3
1 2
2 3
1 3

输出

复制

0 0 2
1 1 2
3 3 3

猜你喜欢

转载自blog.csdn.net/weixin_41793113/article/details/89923531