forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abandonedanimal.cpp
92 lines (82 loc) · 1.87 KB
/
abandonedanimal.cpp
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
#include <bits/stdc++.h>
using namespace std;
int inf = 1 << 30;
int main() {
int n, k;
cin >> n >> k;
// Set up data structures
unordered_map<string, set<int>> m;
vector<int> items;
// Read in the first half of the data
for(int i = 0; i < k; i++) {
int store;
string item;
cin >> store >> item;
m[item].insert(store);
}
// Keep track of input
vector<string> input;
// Read in the second half
int q;
cin >> q;
for(int i = 0; i < q; i++) {
string item;
cin >> item;
input.push_back(item);
}
// Get max from lo to hi
int maxhere = 0;
for(int i = 0; i < q; i++) {
string curr = input[i];
vector<int> rem;
for(auto i : m[curr]) {
if(i < maxhere) {
rem.push_back(i);
}
}
for(auto i : rem) {
m[curr].erase(i);
}
if(m[curr].size() > 0) {
maxhere = max(*m[curr].begin(), maxhere);
}
}
// Get min from hi to lo
int minhere = inf;
for(int i = q-1; i >= 0; i--) {
string curr = input[i];
vector<int> rem;
for(auto i : m[curr]) {
if(i > minhere) {
rem.push_back(i);
}
}
for(auto i : rem) {
m[curr].erase(i);
}
if(m[curr].size() > 0) {
minhere = min(*m[curr].rbegin(), minhere);
}
}
// Find answer
bool works = true;
bool ambig = false;
for(auto i : input) {
if(m[i].size() < 1) {
works = false;
}
if(m[i].size() > 1) {
ambig = true;
}
}
// Print answer
if(!works) {
cout << "impossible" << endl;
}
else if(ambig) {
cout << "ambiguous" << endl;
}
else {
cout << "unique" << endl;
}
}