結果

問題 No.5 数字のブロック
ユーザー rrrrikirrrriki
提出日時 2023-12-02 12:35:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 9,401 bytes
コンパイル時間 4,399 ms
コンパイル使用メモリ 282,912 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-02 12:35:36
合計ジャッジ時間 6,343 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 3 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 3 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 3 ms
6,676 KB
testcase_19 AC 3 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 3 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *    author:  rrrriki
 *    created: 02.12.2023 12:16:18
 */
#define USE_ACL
#if !__INCLUDE_LEVEL__
#include __FILE__

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int L, N;
  cin >> L >> N;
  vector<ll> W(N);
  for (int i = 0; i < N; i++) {
    cin >> W[i];
  }
  sort(all(W));
  ll ans = 0;
  ll tmp = 0;
  for (int i = 0; i < N; i++) {
    if (tmp + W[i] <= L) {
      tmp += W[i];
      ans++;
    } else {
      break;
    }
  }
  cout << ans << "\n";
  return 0;
}

#else

// clang-format off
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define YES cout << "Yes\n"
#define NO cout << "No\n"
using namespace std;
#ifdef USE_ACL
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
//using mint = modint1000000007;
#endif
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 42
#endif
using ll = long long;
using vl = vector<ll>;
// コンテナの全出力 outC(A);
template <class T> void out_c(T &A, string gap=" ") {auto itr = A.begin(); if (itr != A.end()) {cout << *itr; itr++;} while (itr != A.end()) {cout << gap << *itr; itr++;}cout << "\n"; return;}
template <class T> void out_c_pairs(T &A, string gap_inside=" ", string gap_outside = " ") {auto itr = A.begin();if (itr != A.end()) {cout << itr->first << gap_inside << itr->second;itr++;}while (itr != A.end()) {cout << gap_outside << itr->first << gap_inside << itr->second;itr++;}cout << "\n";return;}
ll _pow(ll x, ll n) {if (n == 0) return 1; ll val = _pow(x, n / 2); val *= val; if (n & 1) val *= x; return val;} // べき乗
// マンハッタン距離
template <class T> T mnht(T a, T b, T c, T d) {return abs(a - c) + abs(b - d);}
// ランレングス圧縮
void rle(string s, vector<pair<char, int>> &vec){int cnt = 1; for(int i = 1; i < (int)s.size(); i++) {if(s[i] != s[i-1]){vec.emplace_back(s[i-1], cnt); cnt = 0;} cnt++;} vec.emplace_back(s.back(), cnt);}
// 素数
bool is_prime(ll x){for (ll i=2; i*i<=x; i++){if(x%i==0)return false;}return true;}
map<ll,int> prime_factor(ll n) {map<ll,int> ret; for(ll i=2; i*i <= n; i++) {while(n%i == 0) {ret[i]++; n /= i;}} if(n != 1) ret[n]=1;return ret;}
// 組み合わせ全探索
template <typename T> bool next_combination(const T first, const T last, int k) {
    const T subset = first + k;
    // empty container | k = 0 | k == n 
    if (first == last || first == subset || last == subset) {
        return false;
    }
    T src = subset;
    while (first != src) {
        src--;
        if (*src < *(last - 1)) {
            T dest = subset;
            while (*src >= *dest) {
                dest++;
            }
            iter_swap(src, dest);
            rotate(src + 1, dest + 1, last);
            rotate(subset, subset + (last - dest) - 1, last);
            return true;
        }
    }
    // restore
    rotate(first, subset, last);
    return false;
}
// グラフ

/**
 * @brief Edgeクラスはグラフのエッジ(辺)を表します。
 *
 * @tparam T エッジの重みの型(デフォルトはint)
 */
template <typename T = int> struct Edge {
  int from, to;  // エッジの始点と終点
  T cost;        // エッジの重み
  int idx;       // エッジのインデックス(オプション)

  // デフォルトコンストラクタ
  Edge() = default;

  // エッジをコストに基づいて比較するための演算子オーバーロード
  bool operator<(const Edge &other) const { return cost < other.cost; }
  bool operator>(const Edge& other) const { return cost > other.cost; }
  friend std::ostream& operator<<(std::ostream& os, const Edge& edge) { os << edge.to; return os; }
  
  // コンストラクタ
  Edge(int from, int to, T cost = 1, int idx = -1)
      : from(from), to(to), cost(cost), idx(idx) {}

  // エッジの終点をintとして取得するためのキャスト演算子
  operator int() const { return to; }
};

/**
 * @brief Graphクラスはグラフのデータ構造を表します。
 *
 * @tparam T エッジの重みの型(デフォルトはint)
 */
template <typename T = int> struct Graph {
  vector<vector<Edge<T>>> g;  // 各ノードから出ているエッジのリスト
  int es;                     // エッジの数

  // デフォルトコンストラクタ
  Graph() = default;

  // ノード数nを指定するコンストラクタ
  explicit Graph(int n) : g(n), es(0) {}

  // グラフのサイズ(ノードの数)を返す
  size_t size() const { return g.size(); }

  // 有向エッジを追加する関数
  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  // 無向エッジを追加する関数
  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  // エッジを読み込む関数
  // M: エッジの数, padding: インデックスのオフセット, weighted: 重み付きかどうか, directed: 有向かどうか
  void read(int M, int padding = -1, bool weighted = false,
            bool directed = false) {
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if (weighted) cin >> c;
      if (directed)
        add_directed_edge(a, b, c);
      else
        add_edge(a, b, c);
    }
  }

  // 演算子オーバーロード:インデックスによるエッジのリストへのアクセス
  inline vector<Edge<T>> &operator[](const int &k) { return g[k]; }

  // 演算子オーバーロード(const版):インデックスによるエッジのリストへのアクセス
  inline const vector<Edge<T>> &operator[](const int &k) const { return g[k]; }
};

// Edgesテンプレートの別名定義
template <typename T = int> using Edges = vector<Edge<T>>;

/**
 * @brief NQueenクラスはN-Queen問題を解くためのクラスです。
 *
 */
struct NQueen {
 public:
  explicit NQueen(int n) : N(n) { assert(n > 0);}

  bool add_queen(int x, int y) {  // クイーンを置く
    if (row.count(x) || col.count(y) || diag1.count(x + y) || diag2.count(x - y)) return false;
    assert(x < N && y < N);
    assert(x >= 0 && y >= 0);
    queens.emplace(x, y);
    row.insert(x);        // x が同じ行にある
    col.insert(y);        // y が同じ列にある
    diag1.insert(x + y);  // x + y が同じ斜めの利き筋にある
    diag2.insert(x - y);  // x - y が同じ斜めの利き筋にある
    return true;
  }

  bool remove_queen(int x, int y) {  // クイーンを取り除く
    if (!row.count(x) || !col.count(y) || !diag1.count(x + y) || !diag2.count(x - y)) return false;
    assert(x < N && y < N);
    assert(x >= 0 && y >= 0);
    queens.erase({x, y});
    if (added_queens.count({x, y})) added_queens.erase({x, y});
    row.erase(x);
    col.erase(y);
    diag1.erase(x + y);
    diag2.erase(x - y);
    return true;
  }
  // x, y にクイーンを置けるかどうか
  bool is_valid(int x, int y) { return !row.count(x) && !col.count(y) && !diag1.count(x + y) && !diag2.count(x - y);}
  // クイーンが全てのマスを利き筋に置いているかどうか
  bool is_valid() { return (int)row.size() == N;}
  bool solve(int x = 0) {  // x行目以降のクイーンを置く
    if (x == N) return true;
    if (is_valid()) return true;
    if (row.count(x)) return solve(x + 1);
    for (int y = 0; y < N; y++) {
      if (is_valid(x, y)) {
        add_queen(x, y);
        added_queens.emplace(x, y);
        if (solve(x + 1)) return true;
        added_queens.erase({x, y});
        remove_queen(x, y);
      }
    }
    return false;
  }
  int size() { return N; }  // チェス盤のサイズを返す
  friend std::ostream& operator<<(std::ostream& os, const NQueen& nqueen) {  // クイーンの位置を出力する
    for (auto [x, y] : nqueen.queens) os << x << " " << y << "\n";
    return os;
  }
  // クイーンの位置をvector<pair<int,int>>に出力する
  vector<pair<int, int> > get_queens() { return vector<pair<int, int> >(queens.begin(), queens.end());}
  // 追加したクイーンの位置をvector<pair<int,int>>に出力する
  vector<pair<int, int> > get_added_queens() { return vector<pair<int, int> >(added_queens.begin(), added_queens.end());}
  void print(char c = 'Q', char d = '.') {  // 盤面を出力する
    vector<vector<char> > board(N, vector<char>(N, d));
    for (auto [x, y] : queens) {
      board[x][y] = c;
    }
    for (auto& row : board) {
      for (auto& c : row) {
        cout << c;
      }
      cout << "\n";
    }
  }
 private:
  int N;  // チェス盤のサイズ
  unordered_set<int> row, col, diag1, diag2;  // それぞれの行、列、斜めの利き筋にクイーンがあるかどうか
  set<pair<int, int> > queens;        // クイーンの位置
  set<pair<int, int> > added_queens;  // 追加したクイーンの位置
};
// clang-format on

#endif

/*
 *******  神龜雖壽  *******
 *******  猶有竟時  *******
 *******  騰蛇乘霧  *******
 *******  終爲土灰  *******
 *******  老驥伏櫪  *******
 *******  志在千里  *******
 *******  烈士暮年  *******
 *******  壯心不已  *******
 *******  盈縮之期  *******
 *******  不但在天  *******
 *******  養怡之福  *******
 *******  可得永年  *******
 *******  幸甚至哉  *******
 *******  歌以詠志  *******
 */
0