题解 | #小球投盒#
小球投盒
https://www.nowcoder.com/practice/e0e8a6f2ba7747b5a9f8a8dc6fa3e9f1
读题意可以发现有以下几种情况是可以全部投一遍的:
- 操作1,执行操作1
n
次不同的位置,最后一定是满了的。 - 操作2,可以发现执行了两次不同操作他们的并集就覆盖了全部的盒子
- 操作 1 和 操作 2 一起,如果在 i 位置同时执行过了操作 1 和 操作 2,那么也全部投满了。
统计不同的次数,可以用 set
来统计。
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; set<int> op1, op2; for (int i = 1; i <= m; i++) { int op, x; cin >> op >> x; if (op == 1) { op1.insert(x); } else { op2.insert(x); } if (op1.size() == n || op2.size() > 1 || (op1.count(x) && op2.count(x))) { cout << i << endl; return 0; } } cout << -1 << '\n'; }