CodeForces-1798#B 题解

正文

开个数组 \(last_k\) 统计 \(a_{i,j}\) 最后买彩票的时间,再开一排桶 \(day_t\) 记录该天最后买彩票的有哪些人(即:有 \(p\) 满足 \(last_p=t\) 的集合)。

\(last_k\) 放入 \(day_t\) 中,判断 \(day_t\) 中是否存在空桶,若有则无解(因为没有人在当天是最后买彩票的)。

因为本题是 SPJ,对于每个桶 \(day_t\),输出其中任意一个元素都是正确的,因此我们开桶的时候完全没必要用 vector,每个桶只需要存一个元素,用 C++ 内置数组完全够用。

因为懒,我将 \(last_k\)unordered_map 取代。

#include<iostream>
#include<cstring>
#include<unordered_map>
using namespace std;
const int N=5e4+7;
int t,m,n,a,day[N];
unordered_map<int,int> last;
int main(){
  ios::sync_with_stdio(false),cin.tie(0);
  cin>>t;
  while(t--){
    last.clear();
    memset(day,0,sizeof day);
    cin>>m;
    for(int i=1;i<=m;i++){
      cin>>n;
      for(int j=1;j<=n;j++)
        cin>>a,last[a]=i;
    }
    int M=m;//统计还有多少桶是空的
    for(auto &i: last){//将last放入桶中
      if(!day[i.second]) M--;
      day[i.second]=i.first;
    }
    if(M) cout<<-1;
    else for(int i=1;i<=m;i++) cout<<day[i]<<' ';
    cout<<'\n';
  }
}

AC 记录

后附

日志

v1.0 on 2023.08.14: 发布

posted @ 2023-08-14 22:02  IOIAK_wanguan  阅读(38)  评论(0)    收藏  举报