結果

問題 No.942 プレゼント配り
ユーザー 👑 emthrmemthrm
提出日時 2019-12-05 00:54:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 3,405 bytes
コンパイル時間 1,518 ms
コンパイル使用メモリ 135,312 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 07:55:32
合計ジャッジ時間 3,311 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 13 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 15 ms
4,376 KB
testcase_05 AC 13 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 12 ms
4,380 KB
testcase_09 AC 13 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 11 ms
4,376 KB
testcase_17 AC 11 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
int main() {
  int n, k; cin >> n >> k;
  if (k == 1) {
    cout << "Yes\n";
    FOR(i, 1, n + 1) cout << i << (i == n ? '\n' : ' ');
    return 0;
  }
  if (k == 2) {
    if (n % 4 == 2) { // 総和は奇数
      cout << "No\n";
      return 0;
    }
    vector<vector<int> > ans(k);
    int now = 1;
    bool uekara = true;
    while (now <= n) {
      if (uekara) {
        ans[0].emplace_back(now++);
        ans[1].emplace_back(now++);
      } else {
        ans[1].emplace_back(now++);
        ans[0].emplace_back(now++);
      }
      uekara = !uekara;
    }
    cout << "Yes\n";
    REP(i, k) REP(j, n / k) cout << ans[i][j] << (j + 1 == n / k ? '\n' : ' ');
    return 0;
  }
  if (n / k == 2) {
    vector<vector<int> > ans(k);
    FOR(i, 1, k + 1) ans[i - 1].emplace_back(i);
    FOR(i, k + 1, n + 1) ans[k - 1 - (i - (k + 1))].emplace_back(i);
    cout << "Yes\n";
    REP(i, k) cout << ans[i][0] << ' ' << ans[i][1] << '\n';
    return 0;
  }
  if (n == k) {
    cout << "No\n";
    return 0;
  }
  if (k % 2 == 0) {
    if ((n / k) % 2 == 1) {
      // k = 2a, n = kb とすると ab(2ab+1) は k の倍数でない
      cout << "No\n";
      return 0;
    }
    vector<vector<int> > ans(k);
    for (int i = 1; i <= n;) {
      REP(j, k) ans[j].emplace_back(i++);
      for (int j = k - 1; j >= 0; --j) ans[j].emplace_back(i++);
    }
    cout << "Yes\n";
    REP(i, k) REP(j, n / k) cout << ans[i][j] << (j + 1 == n / k ? '\n' : ' ');
    return 0;
  }
  vector<vector<int> > ans(k);
  for (int i = 1; i <= n;) {
    if (i + k * 3 - 1 == n) {
      vector<pair<int, int> > tmp(k);
      REP(j, k) tmp[j] = {0, j};
      REP(j, k) {
        ans[j].emplace_back(i++);
        tmp[j].first += ans[j].back();
      }
      FOR(j, k / 2 + 1, k) {
        ans[j].emplace_back(i++);
        tmp[j].first += ans[j].back();
      }
      REP(j, k / 2 + 1) {
        ans[j].emplace_back(i++);
        tmp[j].first += ans[j].back();
      }
      sort(tmp.rbegin(), tmp.rend());
      REP(j, k) ans[tmp[j].second].emplace_back(i++);
    } else {
      REP(j, k) ans[j].emplace_back(i++);
      for (int j = k - 1; j >= 0; --j) ans[j].emplace_back(i++);
    }
  }
  cout << "Yes\n";
  REP(i, k) REP(j, n / k) cout << ans[i][j] << (j + 1 == n / k ? '\n' : ' ');
  return 0;
}
0