提出 #38172961


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) ;
#endif

// clang-format off
#define rep(i, n) for (int i = 0; (i) < (int)(n); (i)++)

template<class T> bool chmax(T &a, const T &b) {if(a<b) {a=b; return true;} return false; }
template<class T> bool chmin(T &a, const T &b) {if(a>b) {a=b; return true;} return false; }

template<class T> istream& operator>>(istream& is, vector<T>& vec){ rep(i, vec.size()) is >> vec[i]; return is;}
template<class T> ostream& operator<<(ostream& os, vector<T>& vec){ rep(i, vec.size()) os << vec[i] << (i+1==(int)vec.size() ? "" : " "); return os;}
// clang-format on

using ll = long long;
using ull = unsigned long long;

// valより大きい最小の要素を返す
// @note 条件を満たす要素がない場合はendイテレータを返す
template <class T>
typename set<T>::const_iterator find_min_greater(const set<T>& val_set, const T& val) {
   auto it = val_set.upper_bound(val);
   return it;
}

using P = pair<ll, ll>;

P next_pos(char c, int x, int y) {
   if (c == 'L') x--;
   if (c == 'R') x++;
   if (c == 'U') y++;
   if (c == 'D') y--;
   return P(x, y);
};

pair<int, int> move(string S) {
   int x = 0, y = 0;

   for (auto c : S) {
      tie(x, y) = next_pos(c, x, y);
   }

   return {x, y};
}

string normalize(string S) {
   auto [X, Y] = move(S);

   if (X == 0 && Y == 0) {
      set<P> p_set;
      int x = 0, y = 0;

      S += 'X';

      for (auto c : S) {
         if (c == 'X') break;

         p_set.emplace(x, y);
         tie(x, y) = next_pos(c, x, y);
      }

      cout << p_set.size() << endl;
      exit(0);
   }

   if (X < 0) {
      // L,R flip
      rep(i, S.size()) {
         auto c = S[i];
         if (c == 'L') S[i] = 'R';
         if (c == 'R') S[i] = 'L';
      }

      X *= -1;
   }

   if (Y < 0) {
      // U, D flip
      rep(i, S.size()) {
         auto c = S[i];
         if (c == 'U') S[i] = 'D';
         if (c == 'D') S[i] = 'U';
      }

      Y *= -1;
   }

   if (X == 0) {
      // X, Y flip
      rep(i, S.size()) {
         auto c = S[i];
         if (c == 'R') S[i] = 'U';
         if (c == 'L') S[i] = 'D';
         if (c == 'U') S[i] = 'R';
         if (c == 'D') S[i] = 'L';
      }
   }

   return S;
}

int main() {
   string oS;
   ll K;
   cin >> oS >> K;

   string S = normalize(oS);
   ll X, Y;
   tie(X, Y) = move(S);

   // X>0, Y>=0
   assert(X > 0 && Y >= 0);

   vector<P> pl;
   ll x = 0, y = 0;
   pl.emplace_back(0, 0);

   for (auto c : S) {
      tie(x, y) = next_pos(c, x, y);
      pl.emplace_back(x, y);
   }

   map<ll, ll> min_val;

   auto calc_group = [&](ll x, ll y) -> pair<ll, ll> {
      ll c = X * y - Y * x;
      ll group = ((x % X) + X) % X;
      return {c, group};
   };

   map<pair<ll, ll>, set<ll>> line_info;

   for (auto [x, y] : pl) {
      auto gi = calc_group(x, y);

      line_info[gi].insert(x);
   }

   ll ans = 0;
   set<P> visited;

   for (auto [x, y] : pl) {
      if (visited.count({x, y})) continue;
      visited.emplace(x, y);

      auto gi = calc_group(x, y);
      ll sub_ans = 0;

      auto it = find_min_greater(line_info[gi], x);

      if (it == line_info[gi].end()) {
         // debug(x, y, k, K);
         sub_ans = K;
      } else {
         auto diff_k = (*it - x) / X;
         // debug(x, *it, X, diff_k);
         sub_ans = min(K, diff_k);
         // debug(x, y, k, diff_k);
      }

      ans += sub_ans;
   }

   cout << ans << endl;

   return 0;
}

提出情報

提出日時
問題 F - Cleaning Robot
ユーザ starpentagon
言語 C++ (GCC 9.2.1)
得点 500
コード長 3719 Byte
結果 AC
実行時間 210 ms
メモリ 50344 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 99
セット名 テストケース
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, 064.txt, 065.txt, 066.txt, 067.txt, 068.txt, 069.txt, 070.txt, 071.txt, 072.txt, 073.txt, 074.txt, 075.txt, 076.txt, 077.txt, 078.txt, 079.txt, 080.txt, 081.txt, 082.txt, 083.txt, 084.txt, 085.txt, 086.txt, 087.txt, 088.txt, 089.txt, 090.txt, 091.txt, 092.txt, 093.txt, 094.txt, 095.txt, example0.txt, example1.txt, example2.txt
ケース名 結果 実行時間 メモリ
000.txt AC 7 ms 3516 KiB
001.txt AC 210 ms 50344 KiB
002.txt AC 2 ms 3588 KiB
003.txt AC 3 ms 3492 KiB
004.txt AC 2 ms 3488 KiB
005.txt AC 2 ms 3620 KiB
006.txt AC 2 ms 3624 KiB
007.txt AC 2 ms 3584 KiB
008.txt AC 44 ms 10112 KiB
009.txt AC 46 ms 10152 KiB
010.txt AC 48 ms 10176 KiB
011.txt AC 43 ms 10124 KiB
012.txt AC 43 ms 10260 KiB
013.txt AC 45 ms 10176 KiB
014.txt AC 18 ms 4008 KiB
015.txt AC 18 ms 4060 KiB
016.txt AC 17 ms 4132 KiB
017.txt AC 19 ms 4000 KiB
018.txt AC 21 ms 4040 KiB
019.txt AC 17 ms 4104 KiB
020.txt AC 53 ms 16444 KiB
021.txt AC 51 ms 16516 KiB
022.txt AC 57 ms 16480 KiB
023.txt AC 51 ms 16512 KiB
024.txt AC 51 ms 16416 KiB
025.txt AC 51 ms 16436 KiB
026.txt AC 31 ms 6524 KiB
027.txt AC 32 ms 6624 KiB
028.txt AC 33 ms 6596 KiB
029.txt AC 37 ms 6388 KiB
030.txt AC 38 ms 6692 KiB
031.txt AC 33 ms 6744 KiB
032.txt AC 62 ms 11904 KiB
033.txt AC 57 ms 11004 KiB
034.txt AC 62 ms 11476 KiB
035.txt AC 60 ms 11548 KiB
036.txt AC 66 ms 12048 KiB
037.txt AC 60 ms 11004 KiB
038.txt AC 185 ms 39556 KiB
039.txt AC 184 ms 39496 KiB
040.txt AC 183 ms 39400 KiB
041.txt AC 185 ms 39444 KiB
042.txt AC 185 ms 39556 KiB
043.txt AC 187 ms 39560 KiB
044.txt AC 67 ms 12068 KiB
045.txt AC 59 ms 10524 KiB
046.txt AC 65 ms 11900 KiB
047.txt AC 70 ms 12372 KiB
048.txt AC 129 ms 39188 KiB
049.txt AC 135 ms 39356 KiB
050.txt AC 133 ms 39268 KiB
051.txt AC 137 ms 39184 KiB
052.txt AC 147 ms 28408 KiB
053.txt AC 149 ms 28536 KiB
054.txt AC 148 ms 28744 KiB
055.txt AC 147 ms 28472 KiB
056.txt AC 148 ms 28752 KiB
057.txt AC 146 ms 28748 KiB
058.txt AC 147 ms 28440 KiB
059.txt AC 148 ms 28352 KiB
060.txt AC 149 ms 28448 KiB
061.txt AC 175 ms 33900 KiB
062.txt AC 180 ms 33948 KiB
063.txt AC 179 ms 33908 KiB
064.txt AC 174 ms 33952 KiB
065.txt AC 177 ms 33912 KiB
066.txt AC 175 ms 34080 KiB
067.txt AC 174 ms 33908 KiB
068.txt AC 175 ms 33976 KiB
069.txt AC 178 ms 33992 KiB
070.txt AC 108 ms 27060 KiB
071.txt AC 168 ms 33852 KiB
072.txt AC 57 ms 11160 KiB
073.txt AC 41 ms 8608 KiB
074.txt AC 24 ms 5732 KiB
075.txt AC 33 ms 7124 KiB
076.txt AC 14 ms 4716 KiB
077.txt AC 8 ms 4192 KiB
078.txt AC 62 ms 11452 KiB
079.txt AC 8 ms 3872 KiB
080.txt AC 34 ms 7748 KiB
081.txt AC 43 ms 9196 KiB
082.txt AC 36 ms 7980 KiB
083.txt AC 20 ms 6428 KiB
084.txt AC 34 ms 7760 KiB
085.txt AC 45 ms 9628 KiB
086.txt AC 12 ms 4464 KiB
087.txt AC 50 ms 10380 KiB
088.txt AC 4 ms 3904 KiB
089.txt AC 40 ms 8504 KiB
090.txt AC 29 ms 7136 KiB
091.txt AC 51 ms 10644 KiB
092.txt AC 18 ms 5808 KiB
093.txt AC 28 ms 6960 KiB
094.txt AC 47 ms 9744 KiB
095.txt AC 46 ms 9648 KiB
example0.txt AC 2 ms 3588 KiB
example1.txt AC 2 ms 3412 KiB
example2.txt AC 2 ms 3596 KiB