【cf】CodeForces Round 887(Div.2)题解 A - C

A. Desorting

题意

给一个数列,每次操作可以把前一部分每个数加1,后一部分每个数减1,问至少操作多少次可以让数列非递增

思路

先遍历每一个数,如果有逆序的直接输出0

否则找到相邻元素最小的差值,最少的操作次数就是让这个最小的差值的两个元素变成逆序,除以2加1即可

代码

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t; cin >> t;
    while (t -- )
    {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i ++ ) cin >> a[i];
        int l, r, minn = 0x3f3f3f3f;
        bool flag = true;
        for (int i = 1; i < n; i ++ )
        {
            if (a[i] < a[i - 1])
            {
                cout << 0 << endl;
                flag = false;
                break;
            }
            if (a[i] - a[i - 1] < minn)
            {
                minn = a[i] - a[i - 1];
                l = i - 1, r = i;
            }
        }
        int ans = minn / 2 + 1;
        if (flag) cout << ans << endl;
    }
}

B. Fibonaccharsis

题意

给定斐波那契数列的第 k 项是 n,问存在多少这样的数列

思路

设第一个数是 x,第二个数是 y
那么第三个数 x + y ,第四个数x + 2 * y,第五个数2 * x + 3 * y
可以推出,x 的系数是 0 1 1 2 3 5 …,y 的系数是 1 1 2 3 5 8…
现有斐波那契数列(下标从1开始) 0 1 1 2 3 5 8…
那么 x 的系数 a 就是 fib[i - 1] y 的系数 b 就是fib[i]
于是问题转换成了a * x + b * y = n,求xy有多少种取值情况,要求xy均为整数且 x <= y,对 x 逐项枚举即可

代码

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    vector<int> fib(31);
    function<void(void)> init = [&](void)
    {
        fib[1] = 0, fib[2] = 1;
        for (int i = 3; i <= 30; i ++ )
            fib[i] = fib[i - 1] + fib[i - 2];
    };
    init();
    while (t -- )
    {
        int n, k;
        cin >> n >> k;
        if (k > 30)
        {
            cout << "0\n";
            continue;
        }
        int x, y;
        int a, b;
        int ans = 0;
        a = fib[k - 1], b = fib[k];
        for (x = 0; x <= n / 2; x ++ )
            if ((n - (a * x)) % b == 0 && (n - (a * x)) / b >= x) ans ++ ;
        cout << ans << '\n';
    }
}

C. Ntarsis’ Set

题意

有个从1开始逐渐递增的集合,每次操作给出任意个位数,删去给出位数上的数字,得到新数列,现给出操作次数和每次操作删去的位数,求操作完后的第一位是几

思路

从每一轮当前的第一位数字开始遍历,如果当前遍历到的这位数字位于a[i]a[i - 1]之间,那么它的前 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; // ans表示目前的第一位 j表示截至目前已经处理过的位数
    while (k--)
    {
        while (j < n && a[j] <= ans + j) j++;
        ans += j; // 更新该轮结束后的第一位,也就是该轮刚开始时的第一位加上该轮已处理过的位数
    }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    
    int T;
    cin >> T;
    while (T--) solve();
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Texcavator

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值