1121. Damn Single (25)

本文介绍了一个算法,用于从派对中找出单身人士。输入包括已知的情侣ID和参加派对的人群ID,通过匹配来确定单身者并按ID升序输出。

1121. Damn Single (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

"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 23333
Sample 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值