Submission #1182736

#TimeUsernameProblemLanguageResultExecution timeMemory
1182736DedibeatRoom Temperature (JOI24_ho_t1)C++20
85 / 100
40 ms33860 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

#define F first
#define S second 
#define all(x) (x).begin(), (x).end()

template<typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p)
{
    return os << "(" << p.F << "," << p.S << ")";
}
template<typename T>
void print(const T &v, int lim = 1e9)
{
    for(auto x : v) 
        if(lim-- > 0) cout << x << " ";
    cout << "\n";
}

const int mxN = 5e5;
int N, T;
pair<ll, int> stk[4 * mxN + 5];
int reg[mxN + 5];
int a[mxN + 5];
int brek = 0;
int id;
void push(ll f, int s)
{
    stk[brek].first = f;
    stk[brek].second = s;
    brek++;
}
signed main()
{
    ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    cin >> N >> T;
    for(int i = 0; i < N; i++) cin >> a[i];
    auto check = [&](int mid)
    {
        brek = 0;
        int cnt = 0;
        for(int i = 0; i < N; i++)
        {
            for(int j = -1; j <= 1; j++)
            {
                ll y = (ll)a[i] % T + (ll)j * T;
                push(y - mid, -i - 1);
                push(y + mid, i + 1);
            }
        }
        int mx_cnt = 0;
        sort(stk, stk + brek);
        for(int i = 0; i < brek; i++)
        {
            id = stk[i].second;
        //    cout << l << " " << abs(id) - 1 << "\n";
            if(id > 0)
            {
                if(id - 1 < 0 && id - 1 >= N) for(;;);
                if(reg[id - 1] == 1) cnt--;
                reg[id - 1]--;
            }
            else 
            {
                if(-1 - id < 0 && -1 - id >= N) for(;;);
                if(reg[-1 - id] == 0) cnt++;
                reg[-1 - id]++;
            }
          //  print(reg);
         //   cout << cnt << "\n";
            if(mx_cnt < cnt) mx_cnt = cnt;
        }
        return mx_cnt == N;
    };
    int l = -1, r = 1e9;
    //check(1);
    //return 0;
    while(l + 1 < r)
    {
        int mid = (l + r) / 2;
        if(check(mid)) r = mid;
        else l = mid;
    }

    cout << r << "\n";
 
    
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...