牛客小白月赛101 (A-E)C++ 题解

news2024/9/22 5:22:37

比赛地址

牛客小白月赛101_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ

A . tb的区间问题

每次删掉前面的,后面的,最后剩下连续的n-k个数,答案就是求连续n-k个数的最大和;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}


inline void solve(){
	int n , k ; cin >> n >> k ;
	vector<int> a(n) ;
	for(int& x : a) cin >> x ;
	int ans = 0 ;
	int t = n - k ;
	vector<int> b(n+1,0) ;
	for(int i=1;i<=n;i++) b[i] = b[i-1] + a[i-1] ;
	for(int i=t;i<=n;i++) {
		ans = max(ans,b[i]-b[i-t]) ;
	}
	cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
}

B . tb的字符串问题

用一个栈维护就好了,类似括号匹配问题 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}


inline void solve(){
	int n ; cin >> n ;
	string s ; cin >> s ;
	int ans = 0 ;
	stack<char> st ;
    for(int i=0;i<n;i++){
        if(s[i]=='f'){
        	st.push(s[i]) ;
		}else if(s[i]=='t'){
			st.push(s[i]) ;
		}else if(s[i]=='c'){
			if(!st.empty()&&st.top()=='f'){
				ans += 2 ;
				st.pop() ;
			}else{
                st.push(s[i]) ;
            }
		}else if(s[i]=='b'){
			if(!st.empty()&&st.top()=='t'){
				ans += 2 ;
				st.pop() ;
			}else{
                st.push(s[i]) ;
            }
		}else{
             while(!st.empty()) st.pop() ;
         }
    }
	int res = n - ans  ;
    cout << res << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    while(_ --) solve();
    return 0;
}

C . tb的路径问题

数学找规律题,多次打表猜结论就ok ;

对于n>=4 : 都是先走到(2,2),然后再走到离(n,n)最近的2 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

//#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 1e3 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}

int a[N][N] ;

inline void solve(){
	int n ; cin >> n ;
//	for(int i=1;i<=n;i++){
//		for(int j=1;j<=n;j++){
//			a[i][j] = gcd(i,j) ;
//			cout << a[i][j] << " " ;
//		}
//		cout << endl ;
//	}
	if(n==1){
		cout << 0 << endl ;
	}
	else if(n==2){
		cout << 2 << endl ;
	}else if(n==3){
		cout << 4 << endl ;
	}else{
        if(n&1) cout << 6 << endl ;
		else cout << 4 << endl ;
	}
}
 
signed main(){
    IOS
    int _ = 1;
    // cin >> _ ;
    while(_ --) solve();
    return 0;
}

D . tb的平方问题

差分数组 + 前缀和问题

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 1e3 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };


LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}

bool pd(int x){
	int t = sqrt(x) ;
	return t*t==x ;
}

inline void solve(){
	int n , q ; cin >> n >> q ;
	vector<int> a(n+1) ;
	for(int i=1;i<=n;i++) cin >> a[i] ;
	vector<int> b(n+1,0) , c(n+2,0) , d(n+2,0) ; 
	for(int i=1;i<=n;i++) b[i] = b[i-1] + a[i] ;
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			if(pd(b[j]-b[i-1])){
				c[i]++;
				c[j+1]-- ;
			}
		}
	}
	for(int i=1;i<=n+1;i++){
		d[i] = d[i-1] + c[i] ;
	}
	while(q--){
		int x ; cin >> x ;
		cout << d[x] << endl ;
	}
	return  ;
}
 
signed main(){
    IOS
    int _ = 1;
    // cin >> _ ;
    while(_ --) solve();
    return 0;
}

E . tb的数数问题

将没有出现的数,以及它的倍速全删了,最后统计答案即可 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define all(x) x.begin(), x.end()

bool pd(int x){
    if(x<2) return false ;
    for(int i=2;i<=x/2;i++){
        if(x%i==0) return false ;
    }
    return true ;
}

const int N = 1e6  + 10 ;

int b[N] ;

inline void solve(){
// 	int n ; cin >> n ;
// 	vector<int> a(n) ;
//     for(int& x : a) cin >> x ;
//     sort(all(a)) ;
//     a.erase(std::unique(all(a)), a.end());
//     int res = 0 ;
//     n = a.size() ;
//     for(int x : a) b[x]++ ;
//     if(a[0]!=1){cout << 0 << endl ;return ;}
//     for(int j=0;j<n;j++){
//         int x = a[j] ; 
//         bool tag = true ;
//         for(int i=1;i<=x/i;i++)
//             if(x%i==0){
//                 if(!b[i]) { tag = false ;break ;}
//                 if(!b[x/i]) { tag = false ;break ;}
//             }
//         if(tag) res++ ;
//     }
//     cout << res << endl ;
        int n;
    std::cin >> n ;
    std::vector<int> vis(1000005, 0), flag(1000005, 0), a(n + 1);
    bool f = 0;
    int mx = -1;
    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
        if (a[i] == 1) {
            f = 1;
        }
        mx = std::max(mx, a[i]);
        vis[a[i]] = 1;
    }

    if (!f) {
        std::cout << 0;
        return;
    }

    for (int i = 2; i <= mx; i++) {
        if (!vis[i] && !flag[i]) {
            for (int j = i * 2; j <= mx; j += i) {
                vis[j] = 0;
                flag[j] = 1; 
            }
        }
    }

    int ans = 0;
    for (int i = 1; i <= mx; i++) {
        if (vis[i]) {
            ans++;
        }
    }
    std::cout << ans << '\n';
}
 
signed main(){
    IOS
    int _ = 1;
    while(_ --) solve();
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2154246.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于python的api扫描器系统的设计与实现

&#x1f497;博主介绍&#x1f497;&#xff1a;✌在职Java研发工程师、专注于程序设计、源码分享、技术交流、专注于Java技术领域和毕业设计✌ 温馨提示&#xff1a;文末有 CSDN 平台官方提供的老师 Wechat / QQ 名片 :) Java精品实战案例《700套》 2025最新毕业设计选题推荐…

『功能项目』QFrameWork拾取道具UGUI【69】

本章项目成果展示 我们打开上一篇68QFrameWork扔到地上UGUI的项目&#xff0c; 本章要做的事情是实现当物品在地上时&#xff0c;点击物品将对应物品转移到道具栏中 制作一个提示UI界面 添加Button组件设置为点击即将父物体隐藏 拖拽到文件夹中在场景中删除 创建脚本&#xf…

Postman cURL命令导入导出

导入cURL命令 cURL是一种用于发出HTTP请求的流行命令行工具。在测试Web应用程序或API时&#xff0c;cURL使您能够直接从命令行进行交互&#xff0c;使用API开发人员社区中常见的完善语法。如果在不同的地方有多个cURL命令&#xff0c;可以将它们导入Postman。 ​ 将cURL命令导入…

医院伤员消费点餐限制———未来之窗行业应用跨平台架构

一、点餐上限 医院点餐上限具有以下几方面的意义&#xff1a; 1. 控制成本 - 有助于医院合理规划餐饮预算&#xff0c;避免食物的过度供应造成浪费&#xff0c;从而降低餐饮成本。 2. 保障饮食均衡 - 防止患者或陪护人员过度点餐某一类食物&#xff0c;有利于引导合…

游戏淡入淡出效果

一、制作UIdocument 注&#xff1a;是全黑的&#xff1b;并且Picking Mode设置为Igore 通过调节display中的值&#xff0c;实现淡入淡出效果 二、建立空物体 增加uiDocument 拖入相关的物体 注&#xff1a;层级必须设置为最高&#xff0c;此处为20&#xff0c;这个效果必须遮…

(done) 声音信号处理基础知识(5) (Types of Audio Features for Machine Learning)

参考&#xff1a;https://www.youtube.com/watch?vZZ9u1vUtcIA 声学特征描述了声音&#xff0c;不同特征捕捉声音的不同方面性质 声学特征有助于我们构建智能声学系统 声学特征分类有&#xff1a; 1.抽象等级 2.时域视野 3.音乐的部分 4.信号域 5.机器学习方法 如下图展示…

828华为云征文 | 云服务器Flexus X实例:开源项目 LangChain 部署,实例测试

目录 一、LangChain 介绍 二、部署 LangChain 2.1 安装 langchain 2.2 安装 langchain_community 2.3 安装 qianfan 三、实例运行 3.1 Chat Models 3.2 LLMs 3.3 Embedding Models 四、总结 本篇文章主要通过 Flexus云服务器X实例 部署开源项目 LangChain&#xff0c…

【Delphi】通过 LiveBindings Designer 链接控件示例

本教程展示了如何使用 LiveBindings Designer 可视化地创建控件之间的 LiveBindings&#xff0c;以便创建只需很少或无需源代码的应用程序。 在本教程中&#xff0c;您将创建一个高清多设备应用程序&#xff0c;该应用程序使用 LiveBindings 绑定多个对象&#xff0c;以更改圆…

[SAP ABAP] 生成表维护视图

SAP由于数据量较大&#xff0c;很多自定义表都需要通过用户自行去维护&#xff0c;一般可以直接在SE16N对数据字典进行维护数据&#xff0c;但不是每个用户都有其操作权限&#xff0c;而且直接在数据字典上操作数据有很高的风险&#xff0c;因此SAP提供了表维护视图生成器&…

算法学习2

学习目录 一.插入排序 一.插入排序 从数组的第一个元素开始&#xff0c;当前元素与其前一个元素进行比较&#xff1b; 大于&#xff08;或小于时&#xff09;将其进行交换&#xff0c;即当前元素替换到前一位&#xff1b; 再将该元素与替换后位置的前一个元素进行交换&#xf…

【全网最全】2024年华为杯研赛A题保奖思路+matlab/py代码+成品论文等(后续会更新完整

您的点赞收藏是我继续更新的最大动力&#xff01; 一定要点击如下卡片链接&#xff0c;那是获取资料的入口&#xff01; 点击链接加入【2024华为杯研赛资料汇总】&#xff1a;https://qm.qq.com/q/goQLLNwfgQhttps://qm.qq.com/q/goQLLNwfgQ A 风电场有功功率优化分配思路 这是…

分页插件、代码生成器

01-分页插件、代码生成器 分页插件使用 首先在pom.xml文件中导入依赖 然后再mybatis-config.xml文件中写入插件 在测试类中写入方法 在mybatis.xml文件中设置plugins标签里的属性helperDialectkeyi自动检查当前数据库用的什么,不用设置也行,默认就设置了 分页插件里面属性详解…

XXL-JOB分片概念讲解

3. 分片功能讲解 3.1 案例需求&#xff1a; 1.我们现在实现这样的需求&#xff0c;在指定节假日&#xff0c;需要给平台的所有用户去发送祝福的短信 3.2.编码实现&#xff1a; a.初始化数据 1.在数据库中导入xxl_job_demo.sql数据 b.集成Druid&MyBatis 1.添加依赖 &…

VisualPromptGFSS

COCO-20 i ^i i太大&#xff0c;不建议复现

利士策分享,华为三折叠手机:重塑未来科技生活的里程碑

利士策分享&#xff0c;华为三折叠手机&#xff1a;重塑未来科技生活的里程碑 在这个日新月异的科技时代&#xff0c;华为再次以惊人的创新力&#xff0c;引领我们迈向智能设备的全新纪元——华为三折叠手机&#xff0c; 不仅是技术的飞跃&#xff0c;更是对未来生活方式的一次…

初识set,map

已知快速查找&#xff1a; 1.暴力查找 2.排序二分查找&#xff08;插入删除麻烦&#xff09; 3.搜索树->二叉搜索树&#xff08;极端情况n&#xff09;->平衡树(AVL树&#xff0c;红黑树&#xff09;(logn高度太高&#xff0c;搜索次数多&#xff09;->多叉平衡搜索…

发现编程的全新境界——明基RD280U显示器使用体验

前言 在大学的四年里&#xff0c;我几乎每天都泡在实验室&#xff0c;盯着电脑屏幕&#xff0c;一行行地码代码。那时&#xff0c;学校提供的显示器是非常基础的款式&#xff0c;功能简单&#xff0c;几乎没有任何特别之处&#xff0c;甚至配置也比较低。那个时候&#xff0c;…

【MySQL 01】数据库基础

目录 1.数据库是什么 2.基本操作 数据库服务器连接操作 数据库和数据库表的创建 服务器&#xff0c;数据库&#xff0c;表关系 数据逻辑存储 3.MySQL架构 4.SQL分类 5.存储引擎 1.数据库是什么 mysql&&mysqld&#xff1a; mysql&#xff1a;这通常指的是 MySQL …

PMBOK® 第六版 排列活动顺序

目录 读后感—PMBOK第六版 目录 职场中有句玩笑话&#xff1a;“工作是永远做不完的&#xff0c;任何时候都不可能做完。”这里所吐槽的要点就在于工作任务繁多以及工作缺乏秩序。工作确实是做不完的&#xff0c;倘若工作都能完成&#xff0c;那也就不需要工作了。 工作中令人…

统信服务器操作系统【搭建FTP】设置介绍

如何在操作系统上安装vsftp服务。设置匿名用户登录、设置授权用户密码访问功能,并介绍使用匿名方式、授权用户方式访问vsftp服务。本文适用于A、D、E三个服务器操作系统版本,除安装方式的差异,其他设置均相同。 文章目录 功能概述一、功能介绍二、准备环境三、安装步骤1. 在…