QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#415931 | #7942. $K$ Subsequences | pkien01 | WA | 1ms | 3420kb | C++23 | 3.7kb | 2024-05-21 12:48:12 | 2024-05-21 12:48:15 |
Judging History
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>
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 = 0;
max_key_freq = 0;
}
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 = 0;
max_key = T();
max_key_freq = 0;
} else {
while (max_key_freq > 0) {
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: 3420kb
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)