1121. Damn Single (25)
"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID's which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (<=10000) followed by M ID's of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.
Output Specification:
First print in a line the total number of lonely guests. Then in the next line, print their ID's in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.
Sample Input:3 11111 22222 33333 44444 55555 66666 7 55555 44444 10000 88888 22222 11111 23333Sample Output:
5 10000 23333 44444 55555 88888
注意点:
1、因为有人用0-99999表示,包含0在内,所以数组的记录值最好不要出现0,所以memset只能考虑-1
2、最后用set计数的话,第二个测试点会超时,所以干脆用两次for循环
笔记:
1、memset(cp,-1,sizeof(cp));//-1或0 ,sizeof()抄前面
ac代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <map>
#include <set>
#define M 100010
using namespace std;
int cp[M];//存储对象
int pre[M];//存储是否到场
int book[M];//记录是否和对象同时到场。若有对象,则和对象一起-1,所以若最后为-3,则说明此人和他对象均到场
int main()
{
//freopen("in.txt","r",stdin);
memset(cp,-1,sizeof(cp));//头文件是cstring . sizeof(前)
memset(pre,0,sizeof(pre));
memset(book,-1,sizeof(book));
int N;
scanf("%d",&N);
while(N--)
{
int p1,p2;
scanf("%d %d",&p1,&p2);
cp[p1]=p2;
cp[p2]=p1;
}
int K;
cin>>K;
while(K--)
{
int p3;
scanf("%d",&p3);;
if(cp[p3]==-1)
{
pre[p3]=1;
}
else
{
int p4=cp[p3];
pre[p3]=1;
book[p3]--;
book[p4]--;
}
}
int cou=0;
for(int j=0;j<M;j++)
{
if(pre[j]==1&&book[j]!=-3)
cou++;
}
printf("%d\n",cou);
int flag=0;
for(int i=0;i<M;i++)
{
if(pre[i]==1&&book[i]!=-3)
{
if(flag==0)
{
printf("%05d",i);
flag=1;
}
else
{
printf(" %05d",i);
}
}
}
/*
set<int> st;
for(int i=0;i<M;i++)
{
if(pre[i]==1&&book[i]!=-3)
st.insert(i);
}
printf("%d\n",st.size());
set<int>::iterator it=st.begin();
printf("%05d",*it);
it++;
for(it;it!=st.end();it++)
printf(" %05d",*it);
*/
return 0;
}
本文介绍了一个算法,用于从派对中找出单身人士。输入包括已知的情侣ID和参加派对的人群ID,通过匹配来确定单身者并按ID升序输出。
1831

被折叠的 条评论
为什么被折叠?



