LoginSignup
0
0

More than 5 years have passed since last update.

abc 115 D 再帰

Last updated at Posted at 2018-12-09

C

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<math.h>
#include<queue>
#include<deque>
#include<stack>
#include<cstdio>
#include<utility>
#include<set>
#include<list>
#include<cmath>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int INF = 1e9;
int main() {
    int N, K;
    cin >> N >> K;
    vector<int>V;
    int h;
    for (int i = 0; i < N; ++i) {
        cin >> h;
        V.push_back(h);
    }
    sort(V.begin(), V.end());
    int M = INF;
    for (int i = 0; i < N - K + 1; ++i) {
        M = min(M,V[i + K-1] - V[i]);
    }
    cout << M << endl;
    return 0;
}

114と比べてかなり易しくなったと思う

D
とある世界では、今日はクリスマスです。
高羽氏のパーティで、彼は多次元バーガーを作ることにしました。レベル Lバーガー (Lは 0以上の整数) とは次のようなものです。
レベル 0バーガーとは、パティ
1枚である。
レベルLバーガー (L≥1)
とは、バン 1枚、レベルL−1バーガー、パティ 1枚、レベルL−1バーガー、バン 1枚、をこの順に下から積み重ねたものである。
例えば、パティを P、バンを B で表すと、レベル
1バーガーは BPPPB (を 90度回転したもの)、レベル 2バーガーは BBPPPBPBPPPBB といった見た目になります。
高羽氏が作るのはレベルNバーガーです。ダックスフンドのルンルンは、このバーガーの下から X層を食べます (パティまたはバン 1枚を 1
層とします)。ルンルンはパティを何枚食べるでしょうか?

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<math.h>
#include<queue>
#include<deque>
#include<stack>
#include<cstdio>
#include<utility>
#include<set>
#include<list>
#include<cmath>
#include<stdio.h>
#include<cstdio>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int INF = 1e9;
ll mod = 1e9 + 7;
ll Len[55], Pat[55];//バーガーの厚さとパティの総数
ll rec(ll level, ll layer) {
    if (level == 0)return 1;
    if (layer == 1)return 0;
    else if (layer <= Len[level - 1] + 1) {
        return rec(level - 1, layer - 1);
    }
    else if (layer <= Len[level - 1] + 2) {
        return Pat[level - 1] + 1;
    }
    else if (layer <= 2 * Len[level - 1] + 2) {
        return Pat[level - 1] + 1 + rec(level - 1, layer - Len[level - 1] - 2);
    }
    else {
        return 2 * Pat[level - 1] + 1;
    }
}
//レベルNバーガーの下からX層に含まれるパティの総数をrec(N,X)とする
signed main() {
    ll N, X;
    cin >> N >> X;
    Len[0] = Pat[0] = 1;
    for (int i = 1; i <= N; ++i) {
        Len[i] = Len[i - 1] * 2 + 3;
        Pat[i] = Pat[i - 1] * 2 + 1;
    }
    cout << rec(N, X) << endl;
    return 0;
}

初のd問題。再帰。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0