• Codeforces 1371E2


    Description

    思路

    cf题解中合法的x的处于一段连续区间不太明白。在知道这个前提下,将E1的代码改成二分即可。
    有空再补回来。

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <vector>
    #include <cstring>
    #include <string>
    #include <stack>
    #include <deque>
    #include <cmath>
    #include <iomanip>
    #include <cctype>
     
    #define endl '
    '
    #define IOS std::ios::sync_with_stdio(0);
    #define FILE freopen("..//data_generator//in.txt","r",stdin),freopen("res.txt","w",stdout)
    #define FI freopen("..//data_generator//in.txt","r",stdin)
    #define FO freopen("res.txt","w",stdout)
    #define pb push_back
    #define mp make_pair
    #define seteps(N) fixed << setprecision(N) 
     
    typedef long long ll;
    using namespace std;
    /*-----------------------------------------------------------------*/
     
    #define INF 0x3f3f3f3f
    const int N = 3e5 + 10;
    const double eps = 1e-8;
     
    int arr[N];
    vector<int> ans;
     
    bool check(int x, int p, int n){
        bool ok = true;
        for(int i = 0; i < n; i++) {
            int num = min(i + 1, i + 1 + (x - arr[i]));
            if(num <= 0 || !(num % p)) {
                ok = false;
                break;
            } 
        }
        return ok;
    }
     
    int main() {
        IOS;
        int n, p;
        cin >> n >> p;
        int mx = -INF;
        int mi = INF;
        for(int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        sort(arr, arr + n);
        for(int i = 0; i < n; i++) {
            mx = max(arr[i], mx);
            mi = min(i + 1 - arr[i], mi);
        }
        int s = max(0, 0 - mi + 1);
        int e = mx;
        int l = s, r = e;
        while(l <= r) {
            int mid = (l + r) / 2;
            if(check(mid, p, n)) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        e = r;
        if(s > e) {
            cout << 0 << endl;
        } else {
            cout << e - s + 1 << endl;
            for(int x = s; x <= e; x++) cout << x << " ";
        }
    }
    
  • 相关阅读:
    iOS 记录近期遇到的几个bug
    iOS转场动画封装
    iOS 页面滑动与标题切换颜色渐变的联动效果
    Realm数据持久化方案的简单介绍和使用(二)
    某互联网科技公司的一道逻辑面试题
    js获取本月,本季度,上个季度,本周,上周的起始和结束时间
    CSS样式说明
    a标签的样式
    CSS3的> 和@
    CSS中的position定位
  • 原文地址:https://www.cnblogs.com/limil/p/13235869.html
Copyright © 2020-2023  润新知