Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 1e6+10;
- int lt[N];
- int rt[N];
- int hsh[N];
- int main(){
- int t;
- cin >> t;
- assert(1<=t && t <= 10);
- while(t--){
- int n, m;
- cin >> n >> m;
- assert(n >= 1 && n <= 1000000);
- assert(m >= 2 && m <= 1000000);
- for(int i = 0; i <= n; ++i){
- hsh[i] = 0;
- }
- int x;
- for(int i = 0; i < m; ++i){
- cin >> x;
- hsh[x]++;
- }
- stack <int> s;
- for(int i = 0; i <= n; ++i){
- while(!s.empty() && hsh[s.top()] > hsh[i]){
- rt[s.top()] = i;
- s.pop();
- }
- s.push(i);
- }
- while(!s.empty()){
- rt[s.top()] = n+1;
- s.pop();
- }
- for(int i = n; i >= 0; --i){
- while(!s.empty() && hsh[s.top()] > hsh[i]){
- lt[s.top()] = i;
- s.pop();
- }
- s.push(i);
- }
- while(!s.empty()){
- lt[s.top()] = -1;
- s.pop();
- }
- long long int ans = 0;
- for(int i = 0; i <= n; ++i){
- // cout << lt[i] << " " << rt[i] << endl;
- ans = max(hsh[i] * 1LL * (rt[i] - lt[i] - 1), ans);
- }
- cout << ans << endl;
- }
- }
Add Comment
Please, Sign In to add comment