abc157_c - Guess The Number
18 Dec 2020 — Tags: implementation,greedy — URLImplement statement and handle edge cases. Fill every unknown space with a $0$ if it’s not the first digit ($1$ in that case).
Time complexity: $O(m + n)$
Memory complexity: $O(n)$
Click to show code.
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
ios::sync_with_stdio(false), cin.tie(NULL);
int n, m;
cin >> n >> m;
vi s(n, -1);
bool flag = true;
while (m--)
{
int i, c;
cin >> i >> c, i--;
if (s[i] != -1 and s[i] != c)
flag = false;
if (i == 0 and c == 0 and n != 1)
flag = false;
s[i] = c;
}
if (flag)
{
for (int i = 0; i < n; ++i)
{
if (s[i] == -1)
s[i] = (i == 0 and n != 1);
cout << s[i];
}
cout << endl;
}
else
{
cout << -1 << endl;
}
return 0;
}