vector<int> maxSlidingWindow(vector<int>& nums, int k) { MaxQueue mq; vector<int > ans; if(nums.size() == 0 || k <= 0) return ans; for(int i = 0; i < nums.size(); ++i){ if(i+1 >= k){ ans.push_back(mq.getMax()); mq.pop(); } mq.push(nums[i]); } return ans; }
bool isValid(long long g) { // cout << "G: " << g << endl; MaxQueue Q; long long l = max(d - g, 1ll), r = d + g; queue<State> wait; wait.push(State{0ll, 0ll}); long long best = 0; for (State p: points) { // cout << "===" << i << endl; long long L = p.x - r, R = p.x - l; while (wait.size() != 0 && wait.front().x <= R) { Q.push(wait.front()); wait.pop(); } while (Q.size() != 0 && Q.front().x < L) Q.pop(); if (Q.size() != 0) { // cout << "Push: " << Q.max() + p.s << endl; wait.push(State{p.x, Q.max() + p.s}); best = max(best, Q.max() + p.s); } } return best >= k; }
// Test queue with concurrency. TEST(ThreadSafePriorityQueue, Concurrent) { #define MIN 0 #define MAX 9 #define LEN 11 typedef ThreadSafePriorityQueue<U32, F32, true> MinQueue; typedef ThreadSafePriorityQueue<U32, F32, false> MaxQueue; struct ProducerThread : public Thread { MinQueue& minQueue; MaxQueue& maxQueue; ProducerThread(MinQueue& min, MaxQueue& max) : minQueue(min), maxQueue(max) {} virtual void run(void*) { U32 indices[LEN] = { 2, 7, 4, 6, 1, 5, 3, 8, 6, 9, 0}; F32 priorities[LEN] = {0.2, 0.7, 0.4, 0.6, 0.1, 0.5, 0.3, 0.8, 0.6, 0.9, 0}; for(U32 i = 0; i < LEN; i++) { minQueue.insert(priorities[i], indices[i]); maxQueue.insert(priorities[i], indices[i]); } } }; MinQueue minQueue; MaxQueue maxQueue; ProducerThread producers[] = { ProducerThread(minQueue, maxQueue), ProducerThread(minQueue, maxQueue), ProducerThread(minQueue, maxQueue) }; const U32 len = sizeof(producers) / sizeof(ProducerThread); for(U32 i = 0; i < len; i++) producers[i].start(); for(U32 i = 0; i < len; i++) producers[i].join(); U32 index = MIN; for(U32 i = 0; i < LEN * len; i++) { U32 popped; EXPECT_TRUE(minQueue.takeNext(popped)) << "Failed to pop element from minQueue"; EXPECT_LE(index, popped) << "Element from minQueue was not in sort order"; index = popped; } index = MAX; for(U32 i = 0; i < LEN * len; i++) { U32 popped; EXPECT_TRUE(maxQueue.takeNext(popped)) << "Failed to pop element from maxQueue"; EXPECT_GE(index, popped) << "Element from maxQueue was not in sort order"; index = popped; } #undef MIN #undef MAX #undef LEN }
int solve(){ MaxQueue Q; for (int i = 1; i <= K; i++){ if (d[i] == 1){ for (int y = 1; y <= M; y++){ Q.clear(); for (int x = N, ii = 1; x >= 1; x--, ii++){ while(Q.size() > t[i] - s[i] + 1) Q.pop(); if (grid[x][y] == 'x'){Q.clear(); continue;} dp[i][x][y] = dp[i - 1][x][y]; if (Q.size()) dp[i][x][y] = max(dp[i][x][y], Q.max() + ii); if (dp[i - 1][x][y]) Q.push(dp[i - 1][x][y] - ii); else Q.push(-0x1f1f1f1f); } } }else if (d[i] == 2){ for (int y = 1; y <= M; y++){ Q.clear(); for (int x = 1, ii = 1; x <= N; x++, ii++){ while(Q.size() > t[i] - s[i] + 1) Q.pop(); if (grid[x][y] == 'x'){Q.clear(); continue;} dp[i][x][y] = dp[i - 1][x][y]; if (Q.size()) dp[i][x][y] = max(dp[i][x][y], Q.max() + ii); if (dp[i - 1][x][y]) Q.push(dp[i - 1][x][y] - ii); else Q.push(-0x1f1f1f1f); } } }else if (d[i] == 3){ for (int x = 1; x <= N; x++){ Q.clear(); for (int y = M, ii = 1; y >= 1; y--, ii++){ while(Q.size() > t[i] - s[i] + 1) Q.pop(); if (grid[x][y] == 'x'){Q.clear(); continue;} dp[i][x][y] = dp[i - 1][x][y]; if (Q.size()) dp[i][x][y] = max(dp[i][x][y], Q.max() + ii); if (dp[i - 1][x][y]) Q.push(dp[i - 1][x][y] - ii); else Q.push(-0x1f1f1f1f); } } }else if (d[i] == 4){ for (int x = 1; x <= N; x++){ Q.clear(); for (int y = 1, ii = 1; y <= M; y++, ii++){ //cout << Q.size() << ", " << t[i] << ", " << s[i] << "=>" << (Q.size() ? Q.max(): 0) << endl; while(Q.size() > t[i] - s[i] + 1) Q.pop(); if (grid[x][y] == 'x'){Q.clear(); continue;} dp[i][x][y] = dp[i - 1][x][y]; if (Q.size()) dp[i][x][y] = max(dp[i][x][y], Q.max() + ii); if (dp[i - 1][x][y]) Q.push(dp[i - 1][x][y] - ii); else Q.push(-0x1f1f1f1f); } } } } int ans = 0; for (int x = 1; x <= N; x++){ for (int y = 1; y <= M; y++){ ans = max(ans, dp[K][x][y]); } } return ans - 1; }