Submission #1146037

#TimeUsernameProblemLanguageResultExecution timeMemory
1146037agrim_09Watching (JOI13_watching)C++20
100 / 100
691 ms15880 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define endl '\n'; #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define py cout << "YES" << endl; #define pn cout << "NO" << endl; #define pb push_back #define int long long typedef long double lld; #define double lld typedef long long ll; typedef unsigned long long ull; const ll inf = 1e18; const ll mod = 1e9+7; const int N = 2e5; vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; // Check for queue, priorioty_queue, stack, ordered_set solutions // stack => LIFO (whatever goes in last comes out last) // queue => FIFO (whatever goes in first comes out first) // priority_queue => Dynamic queries of minimum/maximum // ordered_set => set in vector form //[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element] // To comment multiple lines : ctrl + / // To find and replace : ctrl+H void judge(){ srand(time(NULL)); #ifndef ONLINE_JUDGE freopen("1.txt","r",stdin); freopen("2.txt","w",stdout); freopen("Error.txt", "w", stderr); #define debug(x...) cerr << #x <<" = "; _print(x); cerr << endl; #else #define debug(x...) #endif } void usaco(string s) { freopen((s + ".in").c_str(),"r",stdin); freopen((s + ".out").c_str(),"w",stdout); } signed main(){ fastio; //judge(); int n,p,q; cin >> n >> p >> q; vector<int>a(n); for(auto &x : a) cin >> x; sort(a.begin(),a.end()); if((p+q)>=n){ cout << 1; return 0; } auto f1 = [&](int x){ vector<vector<int>>dp(n,vector<int>(p+1)); for(int i = n-1;i>=0;i--){ auto it = upper_bound(a.begin(),a.end(),a[i]+2*x-1); if(it==a.end()){ for(int j = 0;j<=p;j++){ dp[i][j] = 1; } } else{ for(int j = 0;j<=p;j++){ dp[i][j] = 1 + dp[it-a.begin()][j]; } } for(int j = 1;j<=p;j++){ auto it = upper_bound(a.begin(),a.end(),a[i]+x-1); if(it==a.end()){ dp[i][j] = 0; } else{ dp[i][j] = min(dp[i][j],dp[it-a.begin()][j-1]); } } } return (dp[0][p] <= q); }; auto f2 = [&](int x){ vector<vector<int>>dp(n,vector<int>(q+1)); for(int i = n-1;i>=0;i--){ auto it = upper_bound(a.begin(),a.end(),a[i]+x-1); if(it==a.end()){ for(int j = 0;j<=q;j++){ dp[i][j] = 1; } } else{ for(int j = 0;j<=q;j++){ dp[i][j] = 1 + dp[it-a.begin()][j]; } } for(int j = 1;j<=q;j++){ auto it = upper_bound(a.begin(),a.end(),a[i]+2*x-1); if(it==a.end()){ dp[i][j] = 0; } else{ dp[i][j] = min(dp[i][j],dp[it-a.begin()][j-1]); } } } return (dp[0][q] <= p); }; int low = 1, high = 1e9, ans = inf; while(low<=high){ int mid = (low+high)/2; if(p<=q){ if(f1(mid)){ ans = mid, high = mid-1; } else{ low = mid+1; } } else{ if(f2(mid)){ ans = mid, high = mid-1; } else{ low = mid+1; } } } cout << ans; }

Compilation message (stderr)

watching.cpp: In function 'void judge()':
watching.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen("1.txt","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
watching.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("2.txt","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
watching.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("Error.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
watching.cpp: In function 'void usaco(std::string)':
watching.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen((s + ".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
watching.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen((s + ".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...