-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyoperationmaximumscore.cpp
More file actions
109 lines (62 loc) · 2.54 KB
/
applyoperationmaximumscore.cpp
File metadata and controls
109 lines (62 loc) · 2.54 KB
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
class Solution {
public:
const int MOD = 1e9 + 7;
int maximumScore(vector<int>& nums, int k) {
int n = nums.size();
vector<int> primeScores(n);
for (int index = 0; index < n; index++) {
int num = nums[index];
for (int factor = 2; factor <= sqrt(num); factor++) {
if (num % factor == 0) {
primeScores[index]++;
while (num % factor == 0) num /= factor;
}
}
if (num >= 2) primeScores[index]++;
}
vector<int> nextDominant(n, n);
vector<int> prevDominant(n, -1);
stack<int> decreasingPrimeScoreStack;
for (int index = 0; index < n; index++) {
while (!decreasingPrimeScoreStack.empty() &&
primeScores[decreasingPrimeScoreStack.top()] <
primeScores[index]) {
int topIndex = decreasingPrimeScoreStack.top();
decreasingPrimeScoreStack.pop();
nextDominant[topIndex] = index;
}
if (!decreasingPrimeScoreStack.empty())
prevDominant[index] = decreasingPrimeScoreStack.top();
decreasingPrimeScoreStack.push(index);
}
vector<long long> numOfSubarrays(n);
for (int index = 0; index < n; index++) {
numOfSubarrays[index] = (long long)(nextDominant[index] - index) *
(index - prevDominant[index]);
}
priority_queue<pair<int, int>> processingQueue;
for (int index = 0; index < n; index++)
processingQueue.push({nums[index], index});
long long score = 1;
while (k > 0) {
auto [num, index] = processingQueue.top();
processingQueue.pop();
long long operations = min((long long)k, numOfSubarrays[index]);
score = (score * power(num, operations)) % MOD;
k -= operations;
}
return score;
}
private:
long long power(long long base, long long exponent) {
long long res = 1;
while (exponent > 0) {
if (exponent % 2 == 1) {
res = ((res * base) % MOD);
}
base = (base * base) % MOD;
exponent /= 2;
}
return res;
}
};