QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#415945#7942. $K$ Subsequencespkien01WA 1ms3708kbC++233.8kb2024-05-21 12:56:462024-05-21 12:56:48

Judging History

你现在查看的是最新测评结果

  • [2024-05-21 12:56:48]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3708kb
  • [2024-05-21 12:56:46]
  • 提交

answer

/*input
5
3 2
1 -1 1
4 2
-1 1 1 -1
7 3
1 1 1 1 1 1 1
10 3
1 1 1 1 -1 -1 1 1 1 1
12 4
1 1 1 1 -1 -1 -1 -1 1 1 1 1
*/

#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;

template<typename T>
class AllOne {
    unordered_map<T, int> key_to_freq;
    unordered_map<int, unordered_set<T>> freq_to_keys;
    T min_key, max_key; 
    int min_key_freq, max_key_freq;
public:
    AllOne() {
        min_key_freq = INT_MAX;
        max_key_freq = INT_MIN;
    }
    
    void inc(T key) {
        auto key_it = key_to_freq.find(key);
        if (key_it != key_to_freq.end()) freq_to_keys[key_it->second].erase(key);
        int next_freq = ++key_to_freq[key];
        freq_to_keys[next_freq].insert(key);
        if (next_freq > max_key_freq) {
            max_key_freq = next_freq;
            max_key = key;
        }

        if (next_freq < min_key_freq) {
            min_key_freq = next_freq;
            min_key = key;
        }
        else {
            while (min_key_freq <= max_key_freq) {
                auto freq_it = freq_to_keys.find(min_key_freq);
                if (freq_it != freq_to_keys.end() && !freq_it->second.empty()) {
                    min_key = *freq_to_keys[min_key_freq].begin();
                    break;
                }
                min_key_freq++;
            }
        }

        // cout << key << ": " << max_key_freq << " " << min_key_freq << endl; 
    }
    
    void dec(T key) {
        auto key_it = key_to_freq.find(key);
        freq_to_keys[key_it->second].erase(key);

        int next_freq = --key_to_freq[key];
        if (next_freq == 0) {
            key_to_freq.erase(key);
            if (key_to_freq.empty()) {
                min_key = T();
                min_key_freq = INT_MAX;
                max_key = T();
                max_key_freq = INT_MIN;
            } else {
                while (true) {
                    auto freq_it = freq_to_keys.find(max_key_freq);
                    if (freq_it != freq_to_keys.end() && !freq_it->second.empty()) {
                        max_key = *freq_to_keys[max_key_freq].begin();
                        break;
                    }
                    max_key_freq--;
                } 
                while (min_key_freq <= max_key_freq) {
                    auto freq_it = freq_to_keys.find(min_key_freq);
                    if (freq_it != freq_to_keys.end() && !freq_it->second.empty()) {
                        min_key = *freq_to_keys[min_key_freq].begin();
                        break;
                    }
                    min_key_freq++;
                } 
            }
        }
        else {
            freq_to_keys[next_freq].insert(key); 
            if (next_freq < min_key_freq) {
                min_key_freq = next_freq;
                min_key = key;
            }
            while (max_key_freq >= min_key_freq) {
                auto freq_it = freq_to_keys.find(max_key_freq);
                if (freq_it != freq_to_keys.end() && !freq_it->second.empty()) {
                    max_key = *freq_to_keys[max_key_freq].begin();
                    break;
                }
                max_key_freq--;
            }
        }
    }
    
    T getMaxKey() const {
        return max_key;
    }
    
    T getMinKey() const {
        return min_key;
    }
};


int main() {
	int t; cin >>t;
	while (t--) {
		int n, k; cin >> n >> k;
		AllOne<int> st;
		for (int i = 0; i < k; i++) st.inc(i);
		for (int i = 0; i < n; i++) {
			int elem; cin >> elem;
			if (elem == 1) {
				int mn = st.getMinKey();
				st.inc(mn);
				cout << mn + 1 << " ";
			} else {
				auto mx = st.getMaxKey();
				st.dec(mx);
				cout << mx + 1 << " ";
			}
		}
		cout << endl;
	}	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3708kb

input:

5
3 2
1 -1 1
4 2
-1 1 1 -1
7 3
1 1 1 1 1 1 1
10 3
1 1 1 1 -1 -1 1 1 1 1
12 4
1 1 1 1 -1 -1 -1 -1 1 1 1 1

output:

2 2 1 
1 2 2 2 
3 2 1 1 2 3 3 
3 2 1 1 1 1 1 1 2 3 
4 3 2 1 4 1 2 3 4 3 2 1 

result:

wrong answer Jury found better answer than participant (test case 2)