#include <cstdio> #include <vector> #include <utility> #include <deque> using namespace std; vector<int> startValues; vector<char> sequence; #define SEQ_MARKER 'D' struct Candidate { int pool; int round; int ord; Candidate(int p, int r, int o) : pool(p), round(r), ord(o) {} }; static int searchStep(const vector<pair<int, int> >& seqMins, int val) { int a = 0; int b = seqMins.size(); int t; while (b - a > 1) { t = (b + a) / 2; if (seqMins[t].first > val) { a = t; } else { b = t; } } if (seqMins[a].first <= val) t = a; else t = b; return seqMins[t].second - (val - seqMins[t].first); } static long long checkSingleSequence(int n, int m, int seq) { long long result = -1; int seqSum = 0; int seqLen = 0; vector<pair<int, int> > seqMins; deque<Candidate> candidates; int currentMin = 1; Candidate first(startValues[seq], 0, seq); for (int i = seq; i < n; i+=m) { if (startValues[i] < startValues[seq]) { first.pool = startValues[i]; first.ord = i; } } int pos = seq; while (sequence[pos] != SEQ_MARKER) { seqLen++; if (sequence[pos] == 'W') { if (seqSum < currentMin) { seqMins.push_back(make_pair(seqSum, seqLen-1)); currentMin = seqSum; } ++seqSum; ++first.pool; } else { --seqSum; --first.pool; } sequence[pos] = SEQ_MARKER; pos = (pos + n) % m; if (sequence[pos] != SEQ_MARKER) { Candidate next(100000001, seqLen, -1); for (int i = pos; i < n; i += m) { if (startValues[i] < next.pool) { next.pool = startValues[i]; next.ord = i; } } if (next.ord >= 0) { if (first.ord < 0 || next.pool <= first.pool) { first = next; candidates.erase(candidates.begin(), candidates.end()); } else { next.pool -= first.pool; while (!candidates.empty() && candidates.back().pool >= next.pool) { candidates.pop_back(); } candidates.push_back(next); } } } if (first.ord >= 0) { if (first.pool == 0) { long long cr = (seqLen - 1 - first.round) * n + first.ord + 1; if (result < 0 || result > cr) { result = cr; } if (!candidates.empty()) { first = candidates.front(); candidates.pop_front(); } else { first.ord = -1; } } } } if (seqSum < currentMin) { seqMins.push_back(make_pair(seqSum, seqLen)); } int firstPool = first.pool; while (first.ord >= 0) { if (first.pool + seqMins.rbegin()->first > 0 && seqSum >= 0) { return result; } long long rounds = seqLen; rounds += (long long)seqLen * ((first.pool + seqMins.rbegin()->first)/-seqSum); first.pool += seqSum * ((first.pool + seqMins.rbegin()->first)/-seqSum); if (first.pool + seqMins.rbegin()->first > 0) { first.pool += seqSum; rounds += seqLen; } //assert(first.pool + seqMins.rbegin()->first <= 0) long long cr = ((rounds - first.round + searchStep(seqMins, -first.pool) - 1) * n) + first.ord + 1; if (result < 0 || result > cr) { result = cr; } if (!candidates.empty()) { first = candidates.front(); first.pool += firstPool; candidates.pop_front(); } else { first.ord = -1; } } return result; } int main() { int n, m; long long result = -1; scanf("%d", &n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); startValues.push_back(a); } scanf("%d", &m); while (getchar() != '\n'); for (int i = 0; i < m; ++i) { char c = getchar(); sequence.push_back(c); } for (int i = 0; i < sequence.size() && sequence[i] != SEQ_MARKER; ++i) { long long res = checkSingleSequence(n, m, i); if (res > 0 && (result == -1 || result > res)) { result = res; } } printf("%lld\n", result); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | #include <cstdio> #include <vector> #include <utility> #include <deque> using namespace std; vector<int> startValues; vector<char> sequence; #define SEQ_MARKER 'D' struct Candidate { int pool; int round; int ord; Candidate(int p, int r, int o) : pool(p), round(r), ord(o) {} }; static int searchStep(const vector<pair<int, int> >& seqMins, int val) { int a = 0; int b = seqMins.size(); int t; while (b - a > 1) { t = (b + a) / 2; if (seqMins[t].first > val) { a = t; } else { b = t; } } if (seqMins[a].first <= val) t = a; else t = b; return seqMins[t].second - (val - seqMins[t].first); } static long long checkSingleSequence(int n, int m, int seq) { long long result = -1; int seqSum = 0; int seqLen = 0; vector<pair<int, int> > seqMins; deque<Candidate> candidates; int currentMin = 1; Candidate first(startValues[seq], 0, seq); for (int i = seq; i < n; i+=m) { if (startValues[i] < startValues[seq]) { first.pool = startValues[i]; first.ord = i; } } int pos = seq; while (sequence[pos] != SEQ_MARKER) { seqLen++; if (sequence[pos] == 'W') { if (seqSum < currentMin) { seqMins.push_back(make_pair(seqSum, seqLen-1)); currentMin = seqSum; } ++seqSum; ++first.pool; } else { --seqSum; --first.pool; } sequence[pos] = SEQ_MARKER; pos = (pos + n) % m; if (sequence[pos] != SEQ_MARKER) { Candidate next(100000001, seqLen, -1); for (int i = pos; i < n; i += m) { if (startValues[i] < next.pool) { next.pool = startValues[i]; next.ord = i; } } if (next.ord >= 0) { if (first.ord < 0 || next.pool <= first.pool) { first = next; candidates.erase(candidates.begin(), candidates.end()); } else { next.pool -= first.pool; while (!candidates.empty() && candidates.back().pool >= next.pool) { candidates.pop_back(); } candidates.push_back(next); } } } if (first.ord >= 0) { if (first.pool == 0) { long long cr = (seqLen - 1 - first.round) * n + first.ord + 1; if (result < 0 || result > cr) { result = cr; } if (!candidates.empty()) { first = candidates.front(); candidates.pop_front(); } else { first.ord = -1; } } } } if (seqSum < currentMin) { seqMins.push_back(make_pair(seqSum, seqLen)); } int firstPool = first.pool; while (first.ord >= 0) { if (first.pool + seqMins.rbegin()->first > 0 && seqSum >= 0) { return result; } long long rounds = seqLen; rounds += (long long)seqLen * ((first.pool + seqMins.rbegin()->first)/-seqSum); first.pool += seqSum * ((first.pool + seqMins.rbegin()->first)/-seqSum); if (first.pool + seqMins.rbegin()->first > 0) { first.pool += seqSum; rounds += seqLen; } //assert(first.pool + seqMins.rbegin()->first <= 0) long long cr = ((rounds - first.round + searchStep(seqMins, -first.pool) - 1) * n) + first.ord + 1; if (result < 0 || result > cr) { result = cr; } if (!candidates.empty()) { first = candidates.front(); first.pool += firstPool; candidates.pop_front(); } else { first.ord = -1; } } return result; } int main() { int n, m; long long result = -1; scanf("%d", &n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); startValues.push_back(a); } scanf("%d", &m); while (getchar() != '\n'); for (int i = 0; i < m; ++i) { char c = getchar(); sequence.push_back(c); } for (int i = 0; i < sequence.size() && sequence[i] != SEQ_MARKER; ++i) { long long res = checkSingleSequence(n, m, i); if (res > 0 && (result == -1 || result > res)) { result = res; } } printf("%lld\n", result); return 0; } |