Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long a,b,c,d,k,N,Q;
  5. vector<long long> tab;
  6.  
  7. inline long long check(long long val){
  8.     long long jeden = tab[val];
  9.     val = (long long)k-(long long)val;
  10.     long long dwa = a*(val*val*val) + b*(val*val) + c*val + d;
  11.     if(val > N) dwa = 1e18;
  12.     if(val == 0ll) dwa = 0ll;
  13.     return max(dwa,jeden);
  14. }
  15.  
  16. inline void putUI(unsigned long long n) {
  17.     if(n==0){
  18.         putc_unlocked(48,stdout);
  19.         return;
  20.     }
  21.  
  22.      char tab[20];
  23.     long long p = 0;
  24.     while(n != 0) {
  25.         tab[p++] = (n % 10) + 48;
  26.         n /= 10ll;
  27.     }
  28.    
  29.      while(p--)
  30.         putc_unlocked(tab[p], stdout);
  31. }
  32.  
  33. inline void readINT(long long *n) {
  34.    register char c = 0,
  35.     znak_liczby = 1;
  36.    while (c < 33) c = getc_unlocked(stdin);
  37.    
  38.    if(c==45) { znak_liczby = -1;  c = getc_unlocked(stdin); }
  39.    (*n) = 0;
  40.    
  41.     while (c>32) {(*n)=(*n)*10 + c-48; c=getc_unlocked(stdin);}
  42.  
  43.    (*n)*=znak_liczby;
  44. }
  45.  
  46. int main(){
  47.     readINT(&N); readINT(&Q);
  48.     long long x;
  49.     for(int i = 0; i < N; i++){
  50.         readINT(&x);
  51.         tab.push_back(x);
  52.     }
  53.  
  54.     tab.push_back(0);
  55.     sort(tab.begin(),tab.end());
  56.  
  57.     for(int i = 0; i <= N/2; i++)
  58.         tab.push_back(1e18 + i + 1);   
  59.  
  60.     while(Q--){
  61.         readINT(&a); readINT(&b); readINT(&c); readINT(&d); readINT(&k);
  62.         int l = 0, r = k;
  63.         while(l + 1 < r){
  64.             int mid = (l+r)/2;
  65.             long long x = check(mid);
  66.             if(check(mid-2) >= x && x >= check(mid+2)){
  67.                 l = mid;
  68.             } else {
  69.                 r = mid;
  70.             }
  71.         }
  72.         long long res = check(max(0,((int)l-2)));
  73.         for(int i = max(0,(int)l-1); i <= min((int)k,(int)l+2); i++)
  74.             res = min(res,check(i));
  75.         putUI(res);
  76.         putc_unlocked('\n', stdout);
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement