UPC 2022 大中小联合训练赛第十六场

13 篇文章 0 订阅
本文汇总了多个算法题目,包括逛公园的最优排队策略、远足活动的海拔规划、交替01串的最长子串、回文数列的构建、数字替换以获得最大值、最小总价格购买双铃铛、找出最长偶数子串以及数字序列的推导。通过解决这些问题,展示了排序、搜索和优化策略在算法中的应用。
摘要由CSDN通过智能技术生成

目录

问题 A: 逛公园

问题 B: 远足活动

问题 C: 交替01串

问题 D: 回文数列

问题 F: 数字替换

问题 I: ringring

问题 J: ss

问题 K: pushpush


本人也太菜了 就把几个打出来的题发发吧

问题 A: 逛公园

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

临近五一,趵突泉景区游客日益高涨,门口非常拥挤,金明需要安排这些游客排成一条队伍。每名游客的耐心程度不是一样的。第i号游客的耐心值为 ai,它的含义是:如果排在 i号顾客前的人数超过了ai,他就会放弃排队;如果没有,他就会留下。请问金明应该如何安排这些游客排队次序,才能使得留下的游客数量最多呢?

输入

第一行:单个整数 n表示游客人数
第二行:n 个整数 表示每个游客的耐心值

输出

单个整数:表示能够留下的最大游客数量。

样例输入 Copy

5
4 1 0 2 1

样例输出 Copy

4

提示

对于 30% 的数据, 1≤n≤10;
对于 60% 的数据, 1≤n≤1000;
对于 100% 的数据, 1≤n≤1000000, 0<=ai<n

这道题呢 我也没太搞懂但是也一遍ac了 应该就是一个模拟题吧

#include<bits/stdc++.h>
using namespace std;
int a[1000005];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a,a+n);
    int ans=0,p=0;
    for(int i=0;i<n;i++){
        if(a[i]>=p){
            ans++;
            p++;
        }
    }
    cout<<ans;
    return 0;
}

问题 B: 远足活动

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

小明决定徒步完成一个N天的远足登山活动。第一天他带上一个大睡袋和足够的食物,从大本营出发。大本营的海拔高度为0,在第N天,他必须回到大本营。在这N天中,他可以在他的大睡袋和简易的帐篷中入睡。
在每天的登山活动中,他可以选择向山顶出发,也可以选择下山(同一天只能选择一种方式),而且有如下两条规则:
1、在登山过程中,他不能走的太快,海拔的升高的过程,空气的含氧量会发生变化,因此每天只能上升A米的海拔。
2、在下山过程中,他也不能走的太快,否则他的膝盖无法承受冲击。每天他只能下降B米的海拔。
例如:某天早晨小明处在470米的海拔位置,A=100且B=200,那么他在这天中可以向上到达570米的位置,也可以下山达到海拔270米的位置,因此一天结束后,他可能处在的海拔位置在270~570米之间。
现在,给定N,A和B,问小明最多能到达海拔多少米的高度。

输入

第一行三个整数,N,A和B,表示N天的登山远足活动,每天可以向上爬海拔A米,也可以下山下降海拔B米。

输出

输出一个整数,表示在第N天中能到达的最高海拔高度。

样例输入 Copy

【样例1】
3 7 10
【样例2】
5 40 30

样例输出 Copy

【样例1】
10
【样例2】
80

提示

样例1解释:第一天向上爬7,第二天向上爬3到达海拔10,第三天回到大本营。
样例2解释:第一天和第二天分别向上爬40,后面3天下山分别30+30+20


对于50%的数据,2<=n<=20,1<=A,B<=50
对于100%的数据,2<=n<=50,1<=A,B<=50

这题有一说一 我拿到题目的时候还是有点懵懵的 不知道要怎么下手 但是想了一下 好像就是判断一下就好了

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b;
    cin>>n>>a>>b;
    int ans=0;
    int res=0;
    while(n--){
        if((n-1)*b>=res+a){
            res+=a;
        }else{
            if(res+a>n*b){
                ans=n*b;
                break;
            }else{
                ans=res+a;
                break;
            }
        }
    }
    cout<<max(res,ans);
    return 0;
}

问题 C: 交替01串

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

一个交替01串被定义为一个字符串,只有字符“0”和“1”组成,且任意相邻两个字符都是不同的。例如:”1“,”10101“,”0101010“都是交替01串
现在给定一个01字符串s,问其中最长的连续子串,是交替01串的长度是多少?

输入

输入共一行,一个字符串s,只有字符”0“或者”1“组成。

输出

输出一个整数,表示最长的交替01串长度。

样例输入 Copy

【样例1】
1110111
【样例2】
0101011

样例输出 Copy

【样例1】
3
【样例2】
6

提示

样例1解释:最长串为“101”

对于30%的数据,s的长度<=100
对于70%的数据,s的长度<=1000
对于100%的数据,s的长度<=1000000

这道题怎么说呢有点水(给我打出来了还不水?)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    cin>>a;
    int ans=0,res=0;
    int r=0,t;
    while(r<a.size()){
        if(r==0&&res==0){
            if(a[r]=='0'){
                t=0,r++;
                res++;
            }else{
                t=1,r++;
                res++;
            }
        }
        if(a[r]=='0'){
            if(t==0)ans=max(ans,res),res=1,r++;
            else t=0,r++,res++;
        }else if(a[r]=='1'){
            if(t==1)ans=max(ans,res),res=1,r++;
            else t=1,r++,res++;
        }
    }
    cout<<max(ans,res);
    return 0;
}

问题 D: 回文数列

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

小明非常喜欢回文数列。回文数列是指一个包含N个整数的数列A,分别为A[1],A[2],……,A[n],对于第i(1<=i<=N)个数A[i],都有A[i]=A[N-i+1]。但是回文数字非常难得到。
现在小明想到了一个办法,他可以将数列中,任意两个相邻的数字合并,用它们的和来代替,合并完成的值还可以和其他值不断合并,直到只剩下一个数。要知道一个数肯定是回文数列。
当然,小明希望他的回文数列尽可能长,因此,请你帮助小明计算一下,对于一个长度为N的数列,经过最少多少次合并,可以构成一个回文数列。

输入

第一行一个整数N,表示数列中整数的个数。
第二行包含N个正整数,中间用空格分开,表示数列中的数字。

输出

输出一个最小合并次数,使得数列变成回文数列。

样例输入 Copy

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

样例输出 Copy

【样例1】
1
【样例2】
1
【样例3】
2

提示

样例1解释:将1,2合并得到回文数列3 3
样例2解释:将2,4合并,得到回文数列1 6 6 1
样例3解释:先将1和4合并,得到5 3 2,再将3 2合并得到5 5是回文数列

30%的数据,N<=10
60%的数据,N<=1000
100%的数据,N<=1000000,a[i]<=10^9。

这题也想了半天 发现最后都可以变成回文的

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;//相加可能爆int
LL a[1000005];
int main()
{
    int n,ans=0;
    cin>>n;
    for(int i=0;i<n;i++)scanf("%lld",&a[i]);
    int l=0,r=n-1;
    while(l<r){
        if(a[l]==a[r])l++,r--;
        if(l>=r)break;
        if(a[l]<a[r]){
            l++;
            a[l]+=a[l-1];
            ans++;
        }else if(a[l]>a[r]){
            r--;
            a[r]+=a[r+1];
            ans++;
        }
    }
    cout<<ans;
    return 0;
}

问题 F: 数字替换

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

味味很喜欢玩一个数字替换的游戏,数字替换游戏是这样的:给出一个 n 位正整数 a, 然后再给你一个长度为 m 的数字序列 b,味味可以用 b 中的一些数字与 a 中各个位置上的 数字进行一对一的交换(当然也可以选择不交换)。当然 b 中的每个位置上的数字最多只能 被使用一次。这个游戏的目的是经过一系列替换后,使 a 的数值达到最大。
味味很聪明,在位数不多的情况下,总能快速的求出最后 a 的最大数值,但是当 n 很 大时,味味就无能为力了,所以她希望会写程序的你帮助她快速的求解 a 最后能到达的那 个最大值。

输入

输入共包含三行。第一行两个用空格隔开的正整数 n,m。第二行一个正整数 a(a 的最高位必定不是 0)。第三行一个长度为 m 的数字序列 b。

输出

输出仅包含一行一个数值,表示 a 最大可能达到的数值(输出不能含前导0)。

样例输入 Copy

4 3
1024
010

样例输出 Copy

1124

提示

b 中的一个 1 和 a 中的第二位上的 0 进行交换。

对于 20%的数据 1≤n,m≤10 
对于 50%的数据 1≤n,m≤2000 
对于 100%的数据 1≤n,m≤100000

这道题呢之前打过一道字符串的题现在改成数字了 看到题一分钟就出来了

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string a,b;
    int n,m;
    cin>>n>>m;
    cin>>a>>b;
    sort(b.begin(),b.end());
    for(int i=0;i<n;i++){
        if(a[i]<b[m-1]){
            a[i]=b[m-1];
            m--;
        }
    }
    cout<<a;
    return 0;
}

问题 I: ringring

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

Snuke is buying a bicycle. The bicycle of his choice does not come with a bell, so he has to buy one separately.
He has very high awareness of safety, and decides to buy two bells, one for each hand.
The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively. Find the minimum total price of two different bells.

Constraints
1≤a,b,c≤10000
a, b and c are integers.

输入

Input is given from Standard Input in the following format:
a b c

输出

Print the minimum total price of two different bells.

样例输入 Copy

700 600 780

样例输出 Copy

1300

提示

·Buying a 700-yen bell and a 600-yen bell costs 1300 yen.
·Buying a 700-yen bell and a 780-yen bell costs 1480 yen.
·Buying a 600-yen bell and a 780-yen bell costs 1380 yen.
The minimum among these is 1300 yen.

这是一个大大的水题了 就连我这种英语水平不好的都看懂了 就是求三个数中两个小的和

#include<bits/stdc++.h>
using namespace std;
int a[4];
int main()
{
    for(int i=0;i<3;i++){
        cin>>a[i];
    }
    sort(a,a+3);
    cout<<a[0]+a[1];
    return 0;
}

问题 J: ss

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

We will call a string that can be obtained by concatenating two equal strings an even string. For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.
You are given an even string S consisting of lowercase English letters. Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S. It is guaranteed that such a non-empty string exists for a given input.

Constraints
2≤|S|≤200
S is an even string consisting of lowercase English letters.
There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.

输入

Input is given from Standard Input in the following format:
S

输出

Print the length of the longest even string that can be obtained.

样例输入 Copy

abaababaab

样例输出 Copy

6

提示

abaababaab itself is even, but we need to delete at least one character.
abaababaa is not even.
abaababa is not even.
abaabab is not even.
abaaba is even. Thus, we should print its length, 6.

这没什么好说的 直接看代码吧

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    cin>>a;
    int t=0;
    int n=a.size();
    while(--n){
        if(n%2==0){
            for(int i=0,j=n/2;i<n/2;i++,j++){
                if(a[i]!=a[j])t=1;
            }
            if(t==0){
                cout<<n;
                break;
            }else t=0;
        }
    }
    return 0;
}

问题 K: pushpush

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

You are given an integer sequence of length n, a1,…,an. Let us consider performing the following n operations on an empty sequence b.
The i-th operation is as follows:
1.Append ai to the end of b.
2.Reverse the order of the elements in b.
Find the sequence b obtained after these n operations.

Constraints
1≤n≤2×105
0≤ai≤109
n and ai are integers.

输入

Input is given from Standard Input in the following format:
n
a1 a2 … an

输出

Print n integers in a line with spaces in between. The i-th integer should be bi.

样例输入 Copy

4
1 2 3 4

样例输出 Copy

4 2 1 3

提示

After step 1 of the first operation, b becomes: 1.
After step 2 of the first operation, b becomes: 1.
After step 1 of the second operation, b becomes: 1,2.
After step 2 of the second operation, b becomes: 2,1.
After step 1 of the third operation, b becomes: 2,1,3.
After step 2 of the third operation, b becomes: 3,1,2.
After step 1 of the fourth operation, b becomes: 3,1,2,4.
After step 2 of the fourth operation, b becomes: 4,2,1,3.
Thus, the answer is 4 2 1 3.

这个如果按他一步步来就肯定超时了 所以只能找找规律

#include<bits/stdc++.h>
using namespace std;
deque<int>l;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        if(i%2==1)l.push_back(x);
        else l.push_front(x);
    }
    if(n%2==0){
        while(!l.empty()){
            cout<<l.front()<<" ";
            l.pop_front();
        }
    }else{
        while(!l.empty()){
            cout<<l.back()<<" ";
            l.pop_back();
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值