宁波工程学院 OJ [1292] Jokes 一堆数中对每个数与前k个重复的数字的个数

  • [1292] Jokes

  • 时间限制: 1000 ms 内存限制: 6000 K
  • 问题描述
  • Mr. Cai loves to tell some jokes to TT.
    As Mr. Cai don't have too much jokes, he may tell one joke again and again.

    Fortunately, TT's memory is not that good. That means she can only remember recently M 'new jokes'. Even if she remembered one joke, she won't deepen impression for this joke.
    Mr. Cai tell one joke per day. And during N days, can you calculate out how many 'new jokes' TT has heard?

  • 输入
  • This problem contains several cases.
    The first line of each case is two integers N and M. (1 <= M, N <= 1 000 000)
    Then follows a line with N integers, each integer is between 0 and 1 000 000.
  • 输出
  • For each case, you should output the number of 'new jokes' that TT has heard during those days.
  • 样例输入
  • 9 5
    1 2 3 4 5 1 6 1 2
    
  • 样例输出
  • 8
  • 题意: 输入n m 表示有n个数  对每个数看其前面m个数中是否有重复的数   问不重复的有多少个

下面是ac代码  同学的 

bool vis[1000005];
上面的定义很牛逼哦vis函数定义  写成  bool   比写成int 要节省很多很多的空间  改成char也能节省很多的空间


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
bool vis[1000005];
int n,m;
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,temp;
		int x,cnt=0;
		queue<int>q;
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
		{
			scanf("%d",&x);
			if(cnt<m&&vis[x]==0) //开始的时候少了vis[x]==0的条件,错了4次。。
			{
				cnt++;
				vis[x]=1;
				q.push(x);
			}
			else
			{
				if(vis[x]==0)
				{
					temp=q.front();
					q.pop();
					vis[temp]=0;
					q.push(x);
					vis[x]=1;
					cnt++;
				}
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}

另外 我的有个疑问  为什么加上

		que.clear();
		sset.clear();
我的tle代码就变成了mle 了?

下面是我 的TLE代码

#include<stdio.h> 
#include<set> 
#include<vector> 
using namespace std;
		multiset<int>sset;
        multiset<int>::iterator it;
        vector<int>que;
int getval()
{     
    int ret(0);     
    char c;     
    while((c=getchar())==' '||c=='\n'||c=='\r');     
        ret=c-'0';     
    while((c=getchar())!=' '&&c!='\n'&&c!='\r')                 
            ret=ret*10+c-'0';     
        return ret;
}
int main()
{
	int i,k,j,n,m,num,sum,ans,h;
	while(scanf("%d %d",&k,&h)!=EOF)
	{
	//	que.clear();
	//	sset.clear();
       h--;
		ans=k;
		for(i=1;i<=k;i++)
		{
			num=getval();
			if(sset.find(num)!=sset.end())
				ans--;
			sset.insert(num);
			que.push_back(num);
			if(i>h)
			{
			     num=que[i-h];
				 it=sset.find(num);
				 sset.erase(it);
			}
		}
		printf("%d\n",ans);
	

	}
	return 0;
}



你可能感兴趣的:(宁波工程学院 OJ [1292] Jokes 一堆数中对每个数与前k个重复的数字的个数)