Description
有一家旅馆,共有M个房间,编号为1到M。 某天,来了N个客人(编号为1到N)住宿,每个人当然是住在这M个房间中的一个。 问题:求出每个房间中的客人的编号?
Input
多组数据。 每组数据的第一行输入N M (1 <= N <= 1000000, 1 <= M <= 10000) 第二行有N个数,其中第i个数表示第i个客人所住的房间的编号。
1 <= N <= 1000000
Output
输出M行,第k行表示房间k中的客人的编号(从小到大)。 如果k房间中没有客人,输出 -1
7 4 1 2 3 3 2 1 1
room 1: 1 6 7 room 2: 2 5 room 3: 3 4 room 4: -1
解题思路:由于每个房间里面的人数不确定,所以如果开数组room[10000][1000000]的话太浪费空间。所以要用不定长数组vector。注意输入输出就行了。
#include<iostream>
#include<vector>
using namespace std;
const int maxn=100005;
vector<int>v[maxn];
int main ()
{
int N,M,room;
while(~scanf("%d%d",&N,&M)){
for(int i=1;i<N+1;i++){
scanf("%d",&room);
v[room].push_back(i);
}
for(int i=1;i<=M;i++){
printf("room %d:",i);
if(v[i].empty())
printf(" -1\n");
else{
for(vector<int>::iterator it=v[i].begin();it!=v[i].end();it++)
printf(" %d",*it);
printf("\n");
}
}
for(int i=0;i<=M;i++)
v[i].clear();
}
return 0;
}