Codeforces Round 887(Div 2)(A-C)
A. Desorting
题目里说的无序是指后面的一个数大于前面一个数,所以只要有一个 a[i+1]-a[i]<0 就说明已经符合题目的无序要求了,不需要改变即可,即输出0
如果有该序列是非严格递增的根据题目所说的改变就只需要求出最小的差值即可,最后用最小的差值除以2(因为每次可以让选中的部分之前的加一,之后的减一,每次改变的差值为2)再加一
代码如下:
#include<bits/stdc++.h> using namespace std; #define int long long signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) { int n; cin>>n; vector<int>a(n); int diff=1e9; bool fg=true; for(int i=0;i<n;i++)cin>>a[i]; for(int i=0;i<n-1;i++) { if(a[i+1]-a[i]<0)fg=false; diff=min(diff,a[i+1]-a[i]); } if(fg==false)cout<<0<<endl; else cout<<diff/2+1<<endl; } return 0; }
B. Fibonaccharsis
标准代码说实话我没大看懂
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { int n; int k; cin >> n >> k; int ans = 0; for (int i = 1; i <= n; i++) { int second = n; //xth element where x is k int first = i; //fixing x-1th element where x is k-1 bool valid_seq = true; for (int j = 0; j < k - 2; j++) { //for s_x and s_x-1, s_x-2 = s_x - s_x-1 int fx = first; first = second - fx; second = fx; valid_seq &= first <= second; valid_seq &= min(first, second) >= 0; if (!valid_seq) break; //break if the sequence is not fibonacci-like } if (valid_seq) ans++; } cout << ans << endl; } }
C. Ntarsis' Set
假设这些数字按递增的顺序排列在一条直线上。在某一天之前看看每个数字x。如果这一天他没有被删除,那么他就占据了什么新的位置,他之前的位置对他有什么影响
答: 如果 x位于a[i]和a[i+1]之间,他将移动到x-i的新位置,因为在它之前的i个位置已经被删除
利用这一观察结果,反向模拟这一过程,我们就可以得到答案
#include<bits/stdc++.h> using namespace std; using LL = long long; const int N = 2e5 + 5; LL a[N]; inline void solve() { LL n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; LL j = 0, ans = 1; while (k--) { while (j < n && a[j] <= ans + j) j++; ans += j; } cout << ans << endl; } signed main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int T; cin >> T; while (T--) solve(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理