結果

問題 No.1495 パンの仕入れ
ユーザー LayCurseLayCurse
提出日時 2021-04-30 22:50:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 8,781 bytes
コンパイル時間 2,513 ms
コンパイル使用メモリ 216,212 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-07-19 02:35:28
合計ジャッジ時間 6,645 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class T> struct cLtraits_identity{
  using type = T;
}
;
template<class T> using cLtraits_try_make_signed =
  typename conditional<
    is_integral<T>::value,
    make_signed<T>,
    cLtraits_identity<T>
    >::type;
template <class S, class T> struct cLtraits_common_type{
  using tS = typename cLtraits_try_make_signed<S>::type;
  using tT = typename cLtraits_try_make_signed<T>::type;
  using type = typename common_type<tS,tT>::type;
}
;
template<class S, class T> inline auto max_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
  return (typename cLtraits_common_type<S,T>::type) a >= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline int rd_int(void){
  int x;
  rd(x);
  return x;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(__int128_t x){
  int s=0;
  int m=0;
  char f[40];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class T> inline T pow2_L(T a){
  return a*a;
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
int M;
int K;
int X[200000];
int Y[200000];
__int128_t cnt[200000];
__int128_t sm[200000];
__int128_t mn[200000];
__int128_t mx[200000];
void chk(__int128_t m){
  int i;
  __int128_t bes1;
  __int128_t bes2;
  __int128_t c1;
  __int128_t c2;
  __int128_t x;
  for(i=(0);i<(N);i++){
    if(cnt[i] == 0){
      mn[i] = 0;
      mx[i] = K;
      continue;
    }
    bes1 = sm[i] / cnt[i];
    bes2 = bes1 + 1;
    c2 = sm[i] % cnt[i];
    c1 = cnt[i] - c2;
    if(abs((long long)(c1-c2)) > m){
      if(c1 > c2){
        mn[i] = mx[i] =bes1;
      }
      else{
        mn[i] = mx[i] =bes2;
      }
    }
    else{
      __int128_t Q5VJL1cS;
      __int128_t e98WHCEY;
      __int128_t cTE1_r3A;
      Q5VJL1cS = 0;
      e98WHCEY = 1000000000000000000LL;
      while(Q5VJL1cS < e98WHCEY){
        if((Q5VJL1cS + e98WHCEY)%2==0){
          cTE1_r3A = (Q5VJL1cS + e98WHCEY) / 2;
        }
        else{
          cTE1_r3A = (Q5VJL1cS + e98WHCEY + 1) / 2;
        }
        if(c1 * (2*cTE1_r3A-1) + c2 * (2*cTE1_r3A-3) <= m){
          Q5VJL1cS = cTE1_r3A;
        }
        else{
          e98WHCEY = cTE1_r3A - 1;
        }
      }
      x =e98WHCEY;
      mx[i] = bes1 + x;
      __int128_t xr20shxY;
      __int128_t WYIGIcGE;
      __int128_t t_ynMSdg;
      xr20shxY = 0;
      WYIGIcGE = 1000000000000000000LL;
      while(xr20shxY < WYIGIcGE){
        if((xr20shxY + WYIGIcGE)%2==0){
          t_ynMSdg = (xr20shxY + WYIGIcGE) / 2;
        }
        else{
          t_ynMSdg = (xr20shxY + WYIGIcGE + 1) / 2;
        }
        if(c1 * (2*t_ynMSdg-1) + c2 * (2*t_ynMSdg+1) <= m){
          xr20shxY = t_ynMSdg;
        }
        else{
          WYIGIcGE = t_ynMSdg - 1;
        }
      }
      x =WYIGIcGE;
      mn[i] = bes1 - x;
    }
  }
  for(i=(0);i<(N);i++){
    chmax(mn[i], 0);
  }
}
int main(){
  int hCmBdyQB;
  __int128_t res;
  __int128_t m;
  __int128_t ok;
  int V9aVTaxx = rd_int();
  for(hCmBdyQB=(0);hCmBdyQB<(V9aVTaxx);hCmBdyQB++){
    int i;
    rd(N);
    rd(M);
    rd(K);
    {
      int jZyWAPpY;
      for(jZyWAPpY=(0);jZyWAPpY<(M);jZyWAPpY++){
        rd(X[jZyWAPpY]);X[jZyWAPpY] += (-1);
        rd(Y[jZyWAPpY]);
      }
    }
    for(i=(0);i<(N);i++){
      cnt[i] = sm[i] = 0;
    }
    for(i=(0);i<(M);i++){
      cnt[X[i]]++;
      sm[X[i]] += Y[i];
    }
    __int128_t BUotOFBp;
    __int128_t Q5rsz4fz;
    __int128_t GgkpftXM;
    BUotOFBp = 0;
    Q5rsz4fz = 1000000000000000000LL;
    while(BUotOFBp < Q5rsz4fz){
      if((BUotOFBp + Q5rsz4fz)%2==0){
        GgkpftXM = (BUotOFBp + Q5rsz4fz) / 2;
      }
      else{
        GgkpftXM = (BUotOFBp + Q5rsz4fz - 1) / 2;
      }
      ok = 1;
      chk(GgkpftXM);
      int Hjfu7Vx7;
      __int128_t zT28qSmp;
      if(N==0){
        zT28qSmp = 0;
      }
      else{
        zT28qSmp = mn[0];
        for(Hjfu7Vx7=(1);Hjfu7Vx7<(N);Hjfu7Vx7++){
          zT28qSmp += mn[Hjfu7Vx7];
        }
      }
      if(K <zT28qSmp){
        ok = 0;
      }
      int szDqbNYU;
      __int128_t ytthggxT;
      if(N==0){
        ytthggxT = 0;
      }
      else{
        ytthggxT = mx[0];
        for(szDqbNYU=(1);szDqbNYU<(N);szDqbNYU++){
          ytthggxT += mx[szDqbNYU];
        }
      }
      if(K >ytthggxT){
        ok = 0;
      }
      if(ok){
        Q5rsz4fz = GgkpftXM;
      }
      else{
        BUotOFBp = GgkpftXM + 1;
      }
    }
    m =Q5rsz4fz;
    res = 0;
    chk(max_L(0, m-1));
    int OC5CYxKD;
    __int128_t o3WxPXbE;
    if(N==0){
      o3WxPXbE = 0;
    }
    else{
      o3WxPXbE = mn[0];
      for(OC5CYxKD=(1);OC5CYxKD<(N);OC5CYxKD++){
        o3WxPXbE += mn[OC5CYxKD];
      }
    }
    if(K <o3WxPXbE){
      for(i=(0);i<(M);i++){
        res +=(pow2_L((mn[X[i]] - Y[i])));
      }
      int XNa8avth;
      cLtraits_try_make_signed<remove_reference<decltype((*((__int128_t*)NULL)))>::type>::type mlGkBPoR;
      if(N==0){
        mlGkBPoR = 0;
      }
      else{
        mlGkBPoR = mn[0];
        for(XNa8avth=(1);XNa8avth<(N);XNa8avth++){
          mlGkBPoR += mn[XNa8avth];
        }
      }
      res += m *max_L(0,mlGkBPoR- K);
    }
    else{
      for(i=(0);i<(M);i++){
        res +=(pow2_L((mx[X[i]] - Y[i])));
      }
      int AAsEZMFe;
      cLtraits_try_make_signed<remove_reference<decltype((*((__int128_t*)NULL)))>::type>::type xtzQOlbs;
      if(N==0){
        xtzQOlbs = 0;
      }
      else{
        xtzQOlbs = mx[0];
        for(AAsEZMFe=(1);AAsEZMFe<(N);AAsEZMFe++){
          xtzQOlbs += mx[AAsEZMFe];
        }
      }
      res += m *max_L(0, K -xtzQOlbs);
    }
    wt_L(res);
    wt_L('\n');
  }
  return 0;
}
// cLay version 20210405-1

// --- original code ---
// int N, M, K, X[2d5], Y[];
// __int128_t cnt[], sm[], mn[], mx[];
// 
// void chk(__int128_t m){
//   __int128_t bes1, bes2, c1, c2;
//   __int128_t x;
//   rep(i,N){
//     if(cnt[i] == 0) mn[i] = 0, mx[i] = K, continue;
//     bes1 = sm[i] / cnt[i];
//     bes2 = bes1 + 1;
//     c2 = sm[i] % cnt[i];
//     c1 = cnt[i] - c2;
//     if(abs((ll)(c1-c2)) > m){
//       mn[i] = mx[i] = if[c1 > c2, bes1, bes2];
//     } else {
//       x = bsearch_max[__int128_t,x,0,1d18](c1 * (2*x-1) + c2 * (2*x-3) <= m);
//       mx[i] = bes1 + x;
//       x = bsearch_max[__int128_t,x,0,1d18](c1 * (2*x-1) + c2 * (2*x+1) <= m);
//       mn[i] = bes1 - x;
//     }
//   }
//   rep(i,N) mn[i] >?= 0;
// }
// 
// {
//   __int128_t res, m, ok;
//   REP(rd_int()){
//     rd(N,M,K,(X--,Y)(M));
//     rep(i,N) cnt[i] = sm[i] = 0;
//     rep(i,M) cnt[X[i]]++, sm[X[i]] += Y[i];
// 
//     m = bsearch_min[__int128_t,m,0,1d18][
//       ok = 1;
//       chk(m);
//       if(K < sum(mn(N))) ok = 0;
//       if(K > sum(mx(N))) ok = 0;
//     ](ok);
// 
//     res = 0;
//     chk(max(0,m-1));
//     if(K < sum(mn(N))){
//       rep(i,M) res += (mn[X[i]] - Y[i]) ** 2;
//       res += m * max(0, sum(mn(N))- K);
//     } else {
//       rep(i,M) res += (mx[X[i]] - Y[i]) ** 2;
//       res += m * max(0, K - sum(mx(N)));
//     }
// 
//     wt(res);
//   }
// }
0