ferinの競プロ帳

競プロについてのメモ

SRM659 div1 easy ApplesAndOrangesEasy

考えたこと

  • 問題文を理解するのに時間がかかる
  • i番目をりんごにできるならりんごにせずに後ろに回した方がいい場合はなさそう
  • 貪欲に前からりんごにできるならりんごにするみたいなのを書く
  • O(K)で区間のりんごの数を数えられるので合計O(NK)で解けそう

区間のりんごの数を求めるところで区間をスライドしていく感じで書いたらindexバグらせて実装遅くなってしまった…
実装する内容自体は簡単だったし反省

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
const int INF = (1LL<<30);
const ll LLINF = (1LL<<60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
//#define int ll

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

class ApplesAndOrangesEasy {
   public:
   int maximumApples(int N, int K, vector <int> info)
  {
    VI dp(N, 0);
    for(int i: info) dp[i-1] = 1;

    REP(i, N) {
      int now = 0;
      bool flag = true;
      REP(j, K) if(i-j >= 0) {
        now += dp[i-j];
        if(now >= K/2) flag = false;
      }
      FOR(j, 1, K) if(i+j < N) {
        if(i+j-K >= 0) now -= dp[i+j-K];
        now += dp[i+j];
        if(now >= K/2) flag = false;
      }
      if(flag) dp[i] = 1;
    }

    int ret = 0;
    REP(i, N) ret += dp[i];
    return ret;
  }
};