当前位置: 首页 > news >正文

牛客周赛 Round 54 (c++题解)

比赛地址 : 

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

输出'o'的个数;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef long long LL;inline void solve(){string s ; cin >> s ;int t  = 0 ;for(char c : s) t += (c=='o') ;cout << t << endl ;
}signed main(){IOSint _ = 1;while(_ --) solve();return 0;
}

B

模拟 , 因为至少买x , 要把x+1,x+2的情况也考虑上 ;

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;inline void solve(){LL a , b , x ; cin >> a >> b >> x ;LL ans =  0 ;LL g = a * 3 ;if(g<=b) ans = 1LL * x * a ;else {LL s1 = 1LL*(x%3)*a + 1LL * b * (x/3) ;LL s2 = 1LL*((x+1)%3)*a + 1LL * b * ((x+1)/3) ;LL s3 = 1LL*((x+2)%3)*a + 1LL * b * ((x+2)/3) ;ans = min(s1,min(s2,s3)) ;}cout << ans << endl ;
}signed main(){IOSint _ = 1;while(_ --) solve();return 0;
}

模拟  : 

1 . 排序

2 . 对于每一个饲料等级取和上一个较小的数量;

3 . 累加每一级,要求级数从1开始且连续 ;

详细请看代码 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'using namespace std;
typedef long long LL;inline void solve(){int n , m ; cin >> n >> m ;vector<int> a(n) ;int t = 0 ; for(int& x : a) {cin >> x ;if(x==1) t ++ ;}sort(a.begin(),a.end()) ;LL ans = 0 ;int pre = 0 ;int ps = n ;for(int i=0;i<n;i++){int j = i ;while(j<n && a[j]==a[i]) j++ ;int len = j - i  ;if(a[i]-pre==1){pre = a[i] ;ps = min(ps,len) ;ans += ps ;}else{break ;}i = j - 1 ;}cout << ans << endl ;
}signed main(){IOSint _ = 1;// cin >> _;while(_ --) solve();return 0;
}

D

bfs暴力即可 : 

#include<bits/stdc++.h>
const int nn = 1e3+5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;ll n;
vector<ll> a(nn),dis(nn,inf);
void solve(){cin>>n;for(int i=1;i<=n;i++) cin>>a[i];queue<ll> q;dis[1] = 0;q.push(1);while(q.size()){ll v=q.front();q.pop();for(ll j=1;j<=n;j++){if(v==j)continue;if(a[v] % labs(v-j) == 0){if(dis[j]>dis[v]+1){dis[j] = dis[v]+1;q.push(j);}}}}if(dis[n]==inf) cout<<-1;else cout<<dis[n]<<endl;
}
int main() {ios::sync_with_stdio(), cin.tie(), cout.tie();int t=1;while(t--) solve();return 0;
}

但是数据小,dp几次也能过 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef long long LL;
const int N = 1e3 + 10 ;LL a[N] ;inline void solve(){int n ; cin >> n ;for(int i=1;i<=n;i++) cin >> a[i] ;vector<int> dp(n+1,n) ;dp[1] = 0 ;// 往左跳// 从前面和后面转移而来// dp[j] = min(dp[j],dp[i]+1) ;for(int k=0;k<15;k++){for(int i=1;i<=n;i++){for(int j=1;j<i;j++){// 判断j是否能跳到i ;int d = i - j ;if(a[j]%d==0) dp[i] = min(dp[i],dp[j]+1) ;}for(int j=n;j>i;j--){int d = j-i ;if(a[j]%d==0) dp[i] = min(dp[i],dp[j]+1) ;}}}cout << dp[n] << endl ;
}signed main(){IOSint _ = 1;while(_ --) solve();return 0;
}

E

dp , 一个线性dp ;

dp[i]代表长度为i个单位的ans ,最后输出dp[n] ;

枚举每个位置,分为使用第i张覆盖 和 使用i前面的覆盖两种情况 ;

使用第i张覆盖 : dp[i] = min(dp[i],dp[i-a[i]]+1) ;

不使用第i张覆盖 : 枚举j -> [1,i-1] , dp[i] = min(dp[i],dp[j-p-1]+1) ;

详情想想然后看代码便可理解 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef long long LL;// 01 背包
// dp[i] 使用到第i位的ansinline void solve(){int n ; cin >> n ;vector<int> a(n+1) , dp(n+1,n+1);for(int i=1;i<=n;i++) cin >> a[i] ;if(a[1]<=n) dp[a[1]]=1;dp[0] = 0;for(int i=1;i<=n;i++){// 遍历每个位置// 使用第i张if(i-a[i]>=0&&dp[i-a[i]]!=(n+1)){dp[i] = min(dp[i],dp[i-a[i]]+1) ;}// 不使用第i张 , 使用前面的第j张   for(int j=1;j<i;j++){int d = i - j ;if(a[j]>=d+1){int p = a[j]-(d+1) ;if(j-p-1>=0&&dp[j-p-1]!=(n+1)){dp[i] = min(dp[i],dp[j-p-1]+1) ;}}}}    if(dp[n]==n+1) cout << -1 << endl ;else cout << dp[n] << endl ;
}signed main(){IOSint _ = 1;cin >> _;while(_ --) solve();return 0;
}

F

不会 , 输出0骗了48分 ;

欢迎交流和讨论!

http://www.lryc.cn/news/414882.html

相关文章:

  • htsjdk库Genotype及相关类介绍
  • C++ 最短路(spfa) 洛谷
  • MySQL的数据类型
  • xss漏洞(四,xss常见类型)
  • 繁简之争:为什么手机芯片都是 ARM
  • 【nnUNetv2进阶】十九、nnUNetv2 使用ResidualEncoder训练模型
  • Unity3D ShaderGraph 场景扫描光效果实现详解
  • JS中运算符优先级
  • 分享6款有助于写论文能用到的软件app!
  • Python图形验证码的识别:一步步详解
  • Jenkins未授权访问漏洞
  • 什么情况下跑代码内存才会爆
  • 基于arcpro3.0.2运行报错问题:不能加载文件System.Text.Encoding.CodePages, Version=8.0.0.0
  • elk+filebeat+kafka集群部署
  • C++生化危机1.5源码
  • RMAN-06618不同版本之间RMAN无法连接
  • 鸿蒙HarmonyOS开发:多种内置弹窗及自定义弹窗的详细使用指南
  • Python文件
  • 超越标注:合成数据引领下的文本嵌入技术革新
  • IT运维中,如何快速进行故障排查?(以银行APP交易故障为例)
  • 入门mem0.NET
  • 虚拟机(CentOS7)安装jenkins
  • 尚品汇-首页三级分类实现-nginx静态代理生成的静态页面(二十六)
  • 对象存储及其相关概念介绍
  • TypeScript 研发系列
  • 三维世界,一图打尽!Matplotlib带你玩转3D绘图,让数据跳舞的魔法棒!
  • 计算机常识与NOIP历史-CSP初赛知识点整理
  • 代码随想录算法训练营第二天 | 209. 长度最小的子数组、59. 螺旋矩阵 II
  • 鼻咽癌综述
  • 中国AI PC行业研究报告