Codeforces 1371E2 - Asterism (二分)

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 '\n'
#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 << " ";
    }
}
posted @ 2020-07-04 17:48  limil  阅读(192)  评论(0编辑  收藏  举报