Codeforces Round 887(Div 2)(A-C)

发布时间 2023-07-24 20:24:27作者: liuwansi

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();
}