結果

問題 No.1495 パンの仕入れ
ユーザー LayCurseLayCurse
提出日時 2021-04-30 22:52:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,288 bytes
コンパイル時間 4,626 ms
コンパイル使用メモリ 213,300 KB
実行使用メモリ 12,704 KB
最終ジャッジ日時 2023-09-26 07:30:39
合計ジャッジ時間 29,150 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,688 KB
testcase_01 AC 3 ms
9,656 KB
testcase_02 AC 1,316 ms
10,540 KB
testcase_03 AC 1,308 ms
10,548 KB
testcase_04 AC 1,311 ms
10,548 KB
testcase_05 AC 1,309 ms
10,620 KB
testcase_06 AC 1,311 ms
10,632 KB
testcase_07 AC 1,314 ms
10,544 KB
testcase_08 AC 1,315 ms
10,596 KB
testcase_09 AC 1,311 ms
10,612 KB
testcase_10 AC 1,314 ms
10,552 KB
testcase_11 AC 1,318 ms
10,552 KB
testcase_12 AC 156 ms
10,504 KB
testcase_13 AC 156 ms
10,692 KB
testcase_14 AC 155 ms
10,584 KB
testcase_15 AC 156 ms
10,576 KB
testcase_16 AC 156 ms
10,580 KB
testcase_17 AC 156 ms
10,560 KB
testcase_18 AC 155 ms
10,572 KB
testcase_19 AC 156 ms
10,576 KB
testcase_20 AC 155 ms
10,584 KB
testcase_21 AC 155 ms
10,512 KB
testcase_22 AC 1,336 ms
12,560 KB
testcase_23 AC 1,299 ms
12,544 KB
testcase_24 AC 9 ms
10,720 KB
testcase_25 AC 8 ms
10,512 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 1,302 ms
12,564 KB
testcase_29 AC 1,333 ms
12,704 KB
testcase_30 AC 152 ms
10,560 KB
testcase_31 AC 149 ms
10,588 KB
testcase_32 AC 149 ms
10,508 KB
testcase_33 AC 148 ms
10,512 KB
testcase_34 AC 148 ms
10,780 KB
testcase_35 AC 157 ms
10,508 KB
testcase_36 AC 156 ms
10,572 KB
testcase_37 AC 156 ms
10,588 KB
testcase_38 AC 155 ms
10,508 KB
testcase_39 AC 156 ms
10,540 KB
testcase_40 AC 151 ms
10,596 KB
testcase_41 AC 151 ms
10,576 KB
testcase_42 AC 151 ms
10,528 KB
testcase_43 AC 151 ms
10,520 KB
testcase_44 AC 151 ms
10,564 KB
testcase_45 AC 3 ms
9,496 KB
testcase_46 AC 10 ms
9,640 KB
testcase_47 AC 2 ms
9,492 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 chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return 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 = 100000000000000LL;
      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 = 100000000000000LL;
      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);
  }
  for(i=(0);i<(N);i++){
    chmin(mx[i], K);
  }
}
int main(){
  int APIVbQlN;
  long long res;
  long long m;
  long long ok;
  int YREPHmFM = rd_int();
  for(APIVbQlN=(0);APIVbQlN<(YREPHmFM);APIVbQlN++){
    int i;
    rd(N);
    rd(M);
    rd(K);
    {
      int ZIeRIny5;
      for(ZIeRIny5=(0);ZIeRIny5<(M);ZIeRIny5++){
        rd(X[ZIeRIny5]);X[ZIeRIny5] += (-1);
        rd(Y[ZIeRIny5]);
      }
    }
    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 GgkpftXM;
    long long gEg5UqEA;
    long long qSsg05KM;
    GgkpftXM = 0;
    gEg5UqEA = 1000000000000LL;
    while(GgkpftXM < gEg5UqEA){
      if((GgkpftXM + gEg5UqEA)%2==0){
        qSsg05KM = (GgkpftXM + gEg5UqEA) / 2;
      }
      else{
        qSsg05KM = (GgkpftXM + gEg5UqEA - 1) / 2;
      }
      ok = 1;
      chk(qSsg05KM);
      int aTqQ6rt8;
      long long X9Iss0pP;
      if(N==0){
        X9Iss0pP = 0;
      }
      else{
        X9Iss0pP = mn[0];
        for(aTqQ6rt8=(1);aTqQ6rt8<(N);aTqQ6rt8++){
          X9Iss0pP += mn[aTqQ6rt8];
        }
      }
      if(K <X9Iss0pP){
        ok = 0;
      }
      int O3U4gd88;
      long long H31bcJ8S;
      if(N==0){
        H31bcJ8S = 0;
      }
      else{
        H31bcJ8S = mx[0];
        for(O3U4gd88=(1);O3U4gd88<(N);O3U4gd88++){
          H31bcJ8S += mx[O3U4gd88];
        }
      }
      if(K >H31bcJ8S){
        ok = 0;
      }
      if(ok){
        gEg5UqEA = qSsg05KM;
      }
      else{
        GgkpftXM = qSsg05KM + 1;
      }
    }
    m =gEg5UqEA;
    res = 0;
    chk(max_L(0, m-1));
    int lQU550vz;
    long long qE8LMwYZ;
    if(N==0){
      qE8LMwYZ = 0;
    }
    else{
      qE8LMwYZ = mn[0];
      for(lQU550vz=(1);lQU550vz<(N);lQU550vz++){
        qE8LMwYZ += mn[lQU550vz];
      }
    }
    if(K <qE8LMwYZ){
      for(i=(0);i<(M);i++){
        res +=(pow2_L((mn[X[i]] - Y[i])));
      }
      int YlLMHsfa;
      cLtraits_try_make_signed<remove_reference<decltype((*((long long*)NULL)))>::type>::type sMcf5Tpe;
      if(N==0){
        sMcf5Tpe = 0;
      }
      else{
        sMcf5Tpe = mn[0];
        for(YlLMHsfa=(1);YlLMHsfa<(N);YlLMHsfa++){
          sMcf5Tpe += mn[YlLMHsfa];
        }
      }
      res += m *max_L(0,sMcf5Tpe- K);
    }
    else{
      for(i=(0);i<(M);i++){
        res +=(pow2_L((mx[X[i]] - Y[i])));
      }
      int aFgbOQYS;
      cLtraits_try_make_signed<remove_reference<decltype((*((long long*)NULL)))>::type>::type Oq6TK1Zh;
      if(N==0){
        Oq6TK1Zh = 0;
      }
      else{
        Oq6TK1Zh = mx[0];
        for(aFgbOQYS=(1);aFgbOQYS<(N);aFgbOQYS++){
          Oq6TK1Zh += mx[aFgbOQYS];
        }
      }
      res += m *max_L(0, K -Oq6TK1Zh);
    }
    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,1d14](c1 * (2*x-1) + c2 * (2*x-3) <= m);
//       mx[i] = bes1 + x;
//       x = bsearch_max[ll,x,0,1d14](c1 * (2*x-1) + c2 * (2*x+1) <= m);
//       mn[i] = bes1 - x;
//     }
//   }
//   rep(i,N) mn[i] >?= 0;
//   rep(i,N) mx[i] <?= K;
// }
// 
// {
//   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,1d12][
//       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