P5886 Hello, 2020!

https://www.luogu.com.cn/problem/P5886

在这里插入图片描述

我的思路没有错,但是在两个循环中弄错了输出的对象,说明我注意力还不集中啊,总是整错

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n,m,p,c=0;cin >> n >> m >> p;
    int* a = new int[m+5];
    memset(a,0,sizeof(a));
    for(int i = 1;i <= n;i++)
    {
        int count;cin >> count;
        for(int j = 1;j <= count;j++)
        {
            int curl;cin >> curl;
            a[curl]++;//这里没有搞对
        }

    }
    for(int i = 1;i <= m;i++)
    {
        if(a[i] == p)
            c++;
    }
    cout << c << endl;
    for(int i = 1;i <= m;i++)
    {
        if(a[i] == p)
            cout << i << " ";//这里输出的全是p我也是醉了
    }
}
  • 标记所有点,递增,数组中值为p的输出

大佬

cin和scanf同时用会很慢

1e7的数据规模

scanf读入需要时间:1.1s左右
cin读入需要时间:7.0s左右
std::ios::sync_with_stdio(false); 禁用同步:1.5s左右

1e6的数据规模

scanf读入需要时间:0.12s左右
cin读入需要时间:0.7s左右
std::ios::sync_with_stdio(false); 禁用同步:0.16s左右

快读

#include<bits/stdc++.h>
using namespace std;
inline int read() {//快读
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')ch=getchar(),f=-1;
    while(isdigit(ch))x=(x*10)+(ch^48),ch=getchar();
    return x*f;
}
int a[1000001],p,n,m,i,j,s,u,tot;
int main() {
    n=read(),m=read(),p=read();
    for(i=1; i<=n; i++) {
        s=read();//读入人数
        for(j=1; j<=s; j++) {
            u=read();//读入可能的人
            a[u]++;//出现次数加++
        }
    }
    for(i=1; i<=m; i++) {
        if(a[i]==p)tot++;//判断是否出现了正好p次,总数++
    }
    printf("%d\n",tot);//输出总数
    for(i=1; i<=m; i++) {
        if(a[i]==p)printf("%d ",i);//再扫一遍,输出可能的人
    }
    return 0;
}
发布了372 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104295541