Hatena::Grouptopcoder

peryaudoのTopCoder日記

2016-04-27

NegativeGraphDiv2

| 23:14

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
using ll = long long;

const ll INF = 1LL << 60LL;

class NegativeGraphDiv2 {
 public:
  long long findMin(int N, vector<int> s, vector<int> t, vector<int> weight, int charges) {
    vector<vector<pair<int, ll>>> edges(N);
    for (int i = 0; i < s.size(); ++i) {
      edges[s[i] - 1].emplace_back(t[i] - 1, (ll)weight[i]);
    }
    vector<vector<ll>> dist(N, vector<ll>(charges + 1, INF));
    dist[0][0] = 0;

    set<int> changed;
    changed.insert(0);

    while (!changed.empty()) {
      set<int> next_changed;
      for (int ch : changed) {
        for (int from = 0; from < N; ++from) {
          for (pair<int, ll> edge : edges[from]) {
            if (dist[edge.first][ch] > dist[from][ch] + edge.second) {
              dist[edge.first][ch] = dist[from][ch] + edge.second;
              next_changed.insert(ch);
            }
            if (ch + 1 <= charges && dist[edge.first][ch + 1] > dist[from][ch] - edge.second) {
              dist[edge.first][ch + 1] = dist[from][ch] - edge.second;
              next_changed.insert(ch);
              next_changed.insert(ch + 1);
            }
          }
        }
      }
      changed = next_changed;
    }
    return *min_element(dist.back().begin(), dist.back().end());
  }
};

基本的にはベルマンフォードするだけだが、負辺の含まれ方が特殊なので、更新されない頂点集合を大雑把にはぶいてベルマンフォードすると間に合うという問題。

2016-04-26

EmployManager

| 15:58

#include <iostream>
#include <vector>
#include <string>

using namespace std;
using ll = long long;

class EmployManager {
 public:
  int maximumEarnings(vector<int> value, vector<string> earning) {
    ll se = 0;
    int ans = Eval(value, earning, se);
    bool changed = true;
    while (changed) {
      changed = false;
      ll curse = se;
      for (ll i = 0; i < value.size(); ++i) {
        if (se & (1LL << i)) continue;
        const int cur = Eval(value, earning, se | (1LL << i));
        if (cur > ans) {
          changed = true;
          curse = se | (1LL << i);
          ans = cur;
        }
      }
      se = curse;
    }
    return ans;
  }

  int Eval(const vector<int>& value, const vector<string>& earning, ll se) {
    int ans = 0;
    for (ll i = 0; i < value.size(); ++i) {
      if (se & (1LL << i)) {
        ans -= value[i];
      }
    }
    for (ll i = 0; i < value.size(); ++i) {
      for (ll j = i + 1; j < value.size(); ++j) {
        int cost = earning[i][j] - '0';
        if ((se & (1LL << i)) && (se & (1LL << j))) {
          ans += cost;
        } else if (!(se & (1LL << i)) && !(se & (1LL << j))) {
          ans -= cost;
        }
      }
    }
    return ans;
  }
};

貪欲提案おじさん

(証明できて)ないです

2016-04-21

VocaloidsAndSongs

| 10:57

#include <iostream>
#include <map>
#include <tuple>

using namespace std;

class VocaloidsAndSongs {
 public:
  int count(int S, int gumi, int ia, int mayu) {
    return F(S, gumi, ia, mayu);
  }

  int F(int rem, int gumi, int ia, int mayu) {
    if (memo.count(make_tuple(rem, gumi, ia, mayu)))
      return memo[make_tuple(rem, gumi, ia, mayu)];
    int& ans = memo[make_tuple(rem, gumi, ia, mayu)];
    if (rem == 0) {
      return ans = (gumi == 0 && ia == 0 && mayu == 0 ? 1 : 0);
    }

    for (int i = 1; i < (1 << 3); ++i) {
      int ngumi = gumi - ((i & 1) ? 1 : 0);
      int nia = ia - ((i & 2) ? 1 : 0);
      int nmayu = mayu - ((i & 4) ? 1 : 0);
      if (ngumi < 0 || nia < 0 || nmayu < 0) continue;
      ans += F(rem - 1, ngumi, nia, nmayu);
      ans %= 1000000007;
    }
    return ans;
  }

  map<tuple<int, int, int, int>, int> memo;
};

結構難易度にばらつきありまんね…

2016-04-19

PowersOfTwo

| 15:31

#include <map>
#include <vector>
using namespace std;


class PowersOfTwo {
 public:
  long long count(vector<long long> powers) {
    nums = vector<int>(65);
    for (long long p : powers) {
      int dig = 0;
      while ((1LL << dig) <= p) ++dig;
      ++nums[dig];
    }

    return F(0, 0);
  }

  long long F(int dig, int carry) {
    if (dig >= 60) return 1;
    if (carry < 0) return 0;
    if (memo.count(make_pair(dig, carry))) return memo[make_pair(dig, carry)];

    const int ncarry = carry + nums[dig];
    if (ncarry == 0)  {
      memo[make_pair(dig, carry)] = F(dig + 1, 0);
    } else {
      memo[make_pair(dig, carry)] = F(dig + 1, ncarry / 2) + F(dig + 1, (ncarry - 1) / 2);
    }
    return memo[make_pair(dig, carry)];
  }

  map<pair<int, int>, long long> memo;
  vector<int> nums;
};

簡単なDPだし方向性はあってたんだけどダブルカウントとか訳わからなくなってきて自力で解けませんでした。頭の回転…………

Subsets

| 13:56

#include <vector>
#include <map>
#include <iostream>

using namespace std;
using ll = long long;

class Subsets {
 public:
  int findSubset(vector<int> numbers) {
    int ones = 0;
    map<int, int> cnts;
    for (int num : numbers) {
      if (num == 1) {
        ++ones;
      } else {
        ++cnts[num];
      }
    }

    for (auto&& cnt : cnts) {
      nums.push_back(cnt);
    }

    return F(0, 0, 1, ones) + max(0, ones - 1);
  }

  int F(int idx, int sum, int prod, int ones) {
    if (idx >= nums.size()) return 0;

    int ans = 0;
    if (ones - (prod * 2 - sum - 2) > 0)
      ans += F(idx + 1, sum, prod, ones);
    for (int i = 1; i <= nums[idx].second; ++i) {
      const ll nsum = (ll)sum + (ll)nums[idx].first * i;
      const ll nprod = (ll)prod * ipow(nums[idx].first, i);
      if (ones > nprod - nsum) {
        ans += ones - (nprod - nsum);
        ans += F(idx + 1, nsum, nprod, ones);
      }
    }
    return ans;
  }

  int ipow(int m, int e) {
    int ans = 1;
    for (int i = 0; i < e; ++i) {
      if (ans >= ans * m) return 100000;
      ans *= m;
    }
    return ans;
  }

  vector<pair<int, int>> nums;
};

やっと久々にACした………ただのオーバーフローと枝刈り不足で苦しんでました。

2016-04-16

EllysCandyGame

| 16:23

#include <vector>
#include <iostream>
#include <numeric>

using namespace std;

class EllysCandyGame {
 public:
  string getWinner(vector<int> sweets) {
    const int ans = F(true, 0, 0, sweets);
    if (ans == 1) {
      return "Elly";
    } else if (ans == 0) { 
      return "Draw";
    } else {
      return "Kris";
    }
  }

  int F(bool is_elly, int escore, int kscore, vector<int>& sweets) {
    if (accumulate(sweets.begin(), sweets.end(), 0) == 0) {
      if (escore == kscore) return 0;
      return escore < kscore ? (!is_elly ? 1 : -1) : (is_elly ? 1 : -1);
    }

    int ans = -1;
    for (int i = 0; i < sweets.size(); ++i) {
      const int cur = sweets[i];
      if (cur == 0) continue;
      sweets[i] = 0;
      if (i - 1 >= 0) sweets[i - 1] *= 2;
      if (i + 1 < sweets.size()) sweets[i + 1] *= 2;

      const int curans = -F(!is_elly, is_elly ? escore + cur : escore, !is_elly ? kscore + cur : kscore, sweets);
      ans = max(ans, curans);

      sweets[i] = cur;
      if (i - 1 >= 0) sweets[i - 1] /= 2;
      if (i + 1 < sweets.size()) sweets[i + 1] /= 2;
      if (ans == 1) break;
    }

    return ans;
  }
};

よくあるゲーム木(?)全探索。Drawがあるのでboolでは結果を持てないことに注意。

PalindromeMatrixDiv2

| 12:18

#include <iostream>
#include <string>
#include <vector>
#include <climits>
#include <cassert>

using namespace std;

inline int Inv(int x, int lim) {
  return lim - x - 1;
}

void Print(int x, int lim) {
  for (int i = 0; i < lim; ++i) {
    if (x & (1 << i)) cout<<"1"; else cout<<"0";
  }
  cout<<endl;
}

class PalindromeMatrixDiv2 {
 public:
  int minChange(vector<string> A, int rowCount, int columnCount) {
    int ans = INT_MAX;
    for (int i = 0; i < (1 << A.size()); ++i) {
      if (__builtin_popcount(i) < rowCount) continue;
      for (int j = 0; j < (1 << A[0].size()); ++j) {
        if (__builtin_popcount(j) < columnCount) continue;
        vector<string> ca(A);
        for (int k = 0; k < ca.size(); ++k) {
          for (int l = 0; l < ca[0].size(); ++l) {
            vector<int> maj(2, 0);
            ++maj[ca[k][l] - '0'];
            if (i & (1 << k)) ++maj[ca[k][Inv(l, ca[0].size())] - '0'];
            if (j & (1 << l)) ++maj[ca[Inv(k, ca.size())][l] - '0'];
            if (((j & (1<< l)) && (i & (1 << Inv(k, ca.size())))) ||
                ((i & (1 << k)) && (j & (1 << Inv(l, ca[0].size())))))
              ++maj[ca[Inv(k, ca.size())][Inv(l, ca[0].size())] - '0'];
            char filled = maj[0] < maj[1] ? '1' : '0';
            ca[k][l] = filled;
            if (i & (1 << k)) ca[k][Inv(l, ca[0].size())] = filled;
            if (j & (1 << l)) ca[Inv(k, ca.size())][l] = filled;
            if (((j & (1<< l)) && (i & (1 << Inv(k, ca.size())))) ||
                ((i & (1 << k)) && (j & (1 << Inv(l, ca[0].size())))))
              ca[Inv(k, ca.size())][Inv(l, ca[0].size())] = filled;
          }
        }

        int cur = 0;
        for (int k = 0; k < ca.size(); ++k) {
          for (int l = 0; l < ca[0].size(); ++l) {
            cur += ca[k][l] != A[k][l];
          }
        }
        ans = min(ans, cur);
      }
    }

    return ans;
  }
};

行列が十分に小さいのでpalindromeになる行と列を決め打ちして最小になるものを探す。

コードきたな…