結果

問題 No.1495 パンの仕入れ
ユーザー LayCurseLayCurse
提出日時 2021-04-30 22:47:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,112 bytes
コンパイル時間 2,817 ms
コンパイル使用メモリ 213,540 KB
実行使用メモリ 12,808 KB
最終ジャッジ日時 2023-09-26 07:22:26
合計ジャッジ時間 20,134 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,552 KB
testcase_01 AC 3 ms
9,624 KB
testcase_02 AC 815 ms
10,544 KB
testcase_03 AC 813 ms
10,620 KB
testcase_04 AC 816 ms
10,544 KB
testcase_05 AC 817 ms
10,532 KB
testcase_06 AC 816 ms
10,624 KB
testcase_07 AC 819 ms
10,592 KB
testcase_08 AC 819 ms
10,748 KB
testcase_09 AC 816 ms
10,688 KB
testcase_10 AC 819 ms
10,608 KB
testcase_11 AC 815 ms
10,528 KB
testcase_12 AC 106 ms
10,736 KB
testcase_13 AC 106 ms
10,508 KB
testcase_14 AC 107 ms
10,500 KB
testcase_15 AC 107 ms
10,508 KB
testcase_16 AC 108 ms
10,512 KB
testcase_17 AC 107 ms
10,580 KB
testcase_18 AC 107 ms
10,596 KB
testcase_19 AC 107 ms
10,556 KB
testcase_20 AC 106 ms
10,504 KB
testcase_21 AC 106 ms
10,772 KB
testcase_22 AC 816 ms
12,680 KB
testcase_23 AC 815 ms
12,808 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 844 ms
12,544 KB
testcase_29 AC 822 ms
12,620 KB
testcase_30 AC 95 ms
10,508 KB
testcase_31 AC 95 ms
10,592 KB
testcase_32 AC 97 ms
10,604 KB
testcase_33 AC 95 ms
10,668 KB
testcase_34 AC 95 ms
10,508 KB
testcase_35 AC 107 ms
10,512 KB
testcase_36 AC 109 ms
10,492 KB
testcase_37 AC 107 ms
10,596 KB
testcase_38 AC 107 ms
10,552 KB
testcase_39 AC 107 ms
10,776 KB
testcase_40 AC 98 ms
10,492 KB
testcase_41 AC 98 ms
10,504 KB
testcase_42 AC 97 ms
10,504 KB
testcase_43 AC 97 ms
10,584 KB
testcase_44 AC 97 ms
10,496 KB
testcase_45 AC 3 ms
9,476 KB
testcase_46 AC 7 ms
9,496 KB
testcase_47 AC 3 ms
9,556 KB
権限があれば一括ダウンロードができます

ソースコード

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(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');
  }
}
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];
long long cnt[200000];
long long sm[200000];
long long mn[200000];
long long mx[200000];
void chk(long long m){
  int i;
  long long bes1;
  long long bes2;
  long long c1;
  long long c2;
  long long 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(c1-c2) > m){
      if(c1 > c2){
        mn[i] = mx[i] =bes1;
      }
      else{
        mn[i] = mx[i] =bes2;
      }
    }
    else{
      long long Q5VJL1cS;
      long long e98WHCEY;
      long long cTE1_r3A;
      Q5VJL1cS = 0;
      e98WHCEY = 100000000000LL;
      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;
      long long xr20shxY;
      long long WYIGIcGE;
      long long t_ynMSdg;
      xr20shxY = 0;
      WYIGIcGE = 100000000000LL;
      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;
  long long res;
  long long m;
  long long 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];
    }
    long long BUotOFBp;
    long long Q5rsz4fz;
    long long GgkpftXM;
    BUotOFBp = 0;
    Q5rsz4fz = 10000000000LL;
    while(BUotOFBp < Q5rsz4fz){
      if((BUotOFBp + Q5rsz4fz)%2==0){
        GgkpftXM = (BUotOFBp + Q5rsz4fz) / 2;
      }
      else{
        GgkpftXM = (BUotOFBp + Q5rsz4fz - 1) / 2;
      }
      ok = 1;
      chk(GgkpftXM);
      int Hjfu7Vx7;
      long long 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;
      long long 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;
    long long 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((*((long long*)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((*((long long*)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[];
// ll cnt[], sm[], mn[], mx[];
// 
// void chk(ll m){
//   ll bes1, bes2, c1, c2;
//   ll 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(c1-c2) > m){
//       mn[i] = mx[i] = if[c1 > c2, bes1, bes2];
//     } else {
//       x = bsearch_max[ll,x,0,1d11](c1 * (2*x-1) + c2 * (2*x-3) <= m);
//       mx[i] = bes1 + x;
//       x = bsearch_max[ll,x,0,1d11](c1 * (2*x-1) + c2 * (2*x+1) <= m);
//       mn[i] = bes1 - x;
//     }
//   }
//   rep(i,N) mn[i] >?= 0;
// }
// 
// {
//   ll 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[ll,m,0,1d10][
//       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