結果

問題 No.1597 Matrix Sort
ユーザー tnakao0123
提出日時 2021-07-12 12:57:13
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 924 ms / 1,500 ms
コード長 1,303 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 93,828 KB
実行使用メモリ 120,960 KB
最終ジャッジ日時 2024-07-02 03:25:32
合計ジャッジ時間 17,933 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1597.cc:  No.1597 Matrix Sort - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_P = 10000000;

/* typedef */

typedef long long ll;

/* global variables */

int acs[MAX_P], bcs[MAX_P], bcss[MAX_P + 1];

/* subroutines */

void readcs(int n, int cs[]) {
  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    cs[ai]++;
  }
}

ll check(int p, int x) {
  ll sum = 0;
  for (int a = 0; a < p; a++) {
    // (a+b)%p<x -> a+b<x or p<=a+b<x+p -> b<x-a or p-a<=b<x+p-a
    int bc = bcss[max(x - a, 0)] + bcss[min(x + p - a, p)] - bcss[p - a];
    sum += (ll)acs[a] * bc;
  }
  return sum;
}

/* main */

int main() {
  int n, p;
  ll k;
  scanf("%d%lld%d", &n, &k, &p);

  readcs(n, acs);
  readcs(n, bcs);

  for (int i = 0; i < p; i++) bcss[i + 1] = bcss[i] + bcs[i];

  int x0 = 0, x1 = p;
  while (x0 + 1 < x1) {
    int x = (x0 + x1) / 2;
    if (check(p, x) >= k) x1 = x;
    else x0 = x;
  }

  printf("%d\n", x0);
  return 0;
}
0