正难则反
Codeforces Round 860 Div. 2

题意:
一共m天,每天都会有人买彩票,第i天买了彩票的人,在之后就不会再买彩票了,求一个买票顺序
思路:
(第i天买了彩票的人,在之后就不会再买彩票了)所以反向思考,从最后一天开始模拟,如果顺序存在,那么最后一天的所有人在之前绝对是并未购买彩票的人(有一个是最后一天买彩票的,但是没有影响),用set处理天数从后往前并未购买彩票的人,第一次出现的数字当作是这轮买彩票的人,加入set(代表这天之前没人买它)
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define endl "\n" const int N = 5e3; void solve() { int n, m; cin >> m; set<int>s; vector<vector<int>>a(m); map<int, int>vis; for (int i = 0; i < m; i++) { cin >> n; a[i].resize(n); for (int j = 0; j < n; j++) { cin >> a[i][j]; } } vector<int>ans; for (int i = m - 1; i >= 0; i--) { int now = -1; for (auto x : a[i]) { if (!vis[x]) { s.insert(x); vis[x] = 1; now = x; } } if (now == -1) { cout << -1 << endl; return; } ans.push_back(now); } for (int i = ans.size() - 1; i >= 0; i--) { cout << ans[i] << " "; } cout << endl; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T = 1; cin >> T; while (T--) { solve(); } return 0; }
浙公网安备 33010602011771号