63 条题解
-
0陈泓瑞 LV 7 @ 2015-10-03 12:24:30
队列
#include <iostream>
#include <queue>
using namespace std;int main(){
int max,len;
cin>>max>>len;
int word[1001]={0};
queue <int> q;
int flag=0;
int temp;
for(int i=0;i<len;i++){
cin>>temp;
if(word[temp]==0){
word[temp]=1;
q.push(temp);
if(int(q.size())>max){
temp=q.front();
word[temp]=0;
q.pop();
}
flag++;
}
}
cout<<flag;
return 0;
} -
02015-10-02 20:49:44@
/*
Author : Slience_K
Belong : C++
Pro : NOIP 2010 - 1*/
#include <cstdio>
#include <algorithm>
using namespace std;
int n , m , tot , x , ans , dic[ 105 ];
void Check(){
for( int i = 1 ; i <= tot ; i++ )
if ( dic[ i ] == x ) return;
ans++;
if ( tot < m ) dic[ ++tot ] = x;
else{
for( int i = 1 ; i < m ; i++ )
dic[ i ] = dic[ i + 1 ];
dic[ m ] = x;
}
}
int main(){
scanf( "%d%d" , &m , &n );
for( int i = 1 ; i <= n ; i++ ){
scanf( "%d" , &x );
Check();
}
printf( "%d" , ans );
return 0;
} -
02015-09-27 07:48:25@
不明白为什么要用队列什么的。。明明乱搞一下就好了qwq
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int q[1005],v[1005];
int main(){
int n,m;
scanf("%d%d",&n,&m);
int ans=0;
while(m--){
int x;
scanf("%d",&x);
if(v[x]) continue;
ans++;
v[x]=1;
q[++q[0]]=x;
if(q[0]>n) v[q[q[0]-n]]=0;
}
printf("%d\n",ans);
return 0;
} -
02015-08-19 18:37:27@
#include<cstdio>
#include<queue>
#include<set>
using namespace std;queue<int>q;
set<int>s;int main()
{
int n, m, num, ans = 0;
scanf("%d%d", &m, &n);
for(int i=1; i<=n; i++){
scanf("%d", &num);
if(s.count(num))
continue;
else{
ans++;
q.push(num);
s.insert(num);
if(s.size() > m){
s.erase(q.front());
q.pop();
}
}
}
printf("%d", ans);
return 0;
}
STL -
02015-06-26 16:44:27@
库容器版本
#include<cstdio>
#include<set>
#include<queue>
using namespace std;set<int> S;
queue<int> Q;
int m,n,t,ans;int main()
{
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
{
scanf("%d",&t);
if(!S.count(t))
{
Q.push(t);
if(S.size()==m)
{
S.erase(Q.front());
Q.pop();
}
S.insert(t);
ans++;
}
}
printf("%d",ans);
return 0;
} -
02015-05-09 16:48:25@
队列的应用。
范围很小,所以暴力查找+暴力维护左右端点就行了,不用循环队列。Pascal Code
var
q:array[-1000..2000] of longint;
s,e,i,j,w,ans,m,n:longint;
flag:boolean;
function size:longint;
begin
exit(e-s+1);
end;
begin
readln(m,n);
s:=1;
e:=0;
ans:=0;
fillchar(q,sizeof(q),0);
for i:=1 to n do
begin
read(w);
flag:=false;
for j:=s to e do
if q[j]=w then flag:=true;
if not flag //内存中没有
then begin
ans:=ans+1;
e:=e+1;
q[e]:=w;
if size>m then s:=s+1; //出队列
end;
end;
writeln(ans);
end. -
02014-11-25 10:38:21@
#include<iostream>
using namespace std;
main()
{
bool b;
int a[101],c=0,i,j,k,l=1,m,n;
cin>>n>>m;
for(i=1;i<=n;i++)
a[i]=-1;
for(i=1;i<=m;i++)
{
b=1;
cin>>j;
for(k=1;k<=n;k++)
if(a[k]==j)
{
b=0;
break;
}
if(b)
{
c++;
a[l++]=j;
if(l==n+1)
l=1;
}
}
cout<<c;
} -
02014-11-04 20:04:14@
那要写那么麻烦啊
那要写那么麻烦啊#include<iostream>
using namespace std;
int n,m;
int v[1005];
int ans;
int num,sub;int main()
{
cin>>n>>m;
for(int i=0,k;i<m;i++)
{
cin>>k;
if(v[k]<=sub) v[k]=++num,sub+=(num-sub-1==n),ans++;
}
cout<<ans;
return 0;
}这不就是了
-
02014-11-03 11:02:37@
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1001],b[1001];
int main()
{
int n,m,t,s=1,sum=0;
cin>>m>>n;
for(int i=0;i<=1000;i++) a[i]=-1;
for(int i=1;i<=n;i++)
{
cin>>t;
if(b[t]==1) continue;
if(s>m) s=s-m;
if(a[s]!=-1) b[a[s]]=0;
a[s]=t;
b[t]=1;
s++;
sum++;
}
cout<<sum<<endl;
return 0;
} -
02014-11-03 11:02:13@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <deque>
using namespace std;
int N=0,M=0;
int num[1000+5];
deque<int> q;
int main()
{
int tot=0;
scanf("%d%d",&M,&N);
for(int i=0;i!=N;++i)
cin>>num[i];
for(int i=0;i!=N;++i)
{
int flag=0;
for(int j=0;j!=q.size();++j)
if(num[i]==q[j])
{
flag=1;
break;
}
if(flag==0)
{
tot++;
if(q.size()+1>M)
{
q.pop_front();
q.push_back(num[i]);
}
else
q.push_back(num[i]);
}
}
cout<<tot<<endl;
return 0;
} -
02014-11-03 11:01:29@
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1001],b[1001];
int main()
{
int n,m,t,s=1,sum=0;
cin>>m>>n;
for(int i=0;i<=1000;i++) a[i]=-1;
for(int i=1;i<=n;i++)
{
cin>>t;
if(b[t]==1) continue;
if(s>m) s=s-m;
if(a[s]!=-1) b[a[s]]=0;
a[s]=t;
b[t]=1;
s++;
sum++;
}
cout<<sum<<endl;
return 0;
} -
02014-10-30 15:21:36@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <deque>
using namespace std;
int N=0,M=0;
int num[1000+5];
deque<int> q;
int main()
{
int tot=0;
scanf("%d%d",&M,&N);
for(int i=0;i!=N;++i)
cin>>num[i];
for(int i=0;i!=N;++i)
{
int flag=0;
for(int j=0;j!=q.size();++j)
if(num[i]==q[j])
{
flag=1;
break;
}
if(flag==0)
{
tot++;
if(q.size()+1>M)
{
q.pop_front();
q.push_back(num[i]);
}
else
q.push_back(num[i]);
}
}
cout<<tot<<endl;
return 0;
} -
02014-08-28 16:53:52@
大水题 一次AC!
#include <iostream>using namespace std;
bool findw(int word,int memory[],int front,int tail)
{
for(int i = front; i < tail; ++i)
{
if(memory[i] == word)
return true;
}
return false;
}
int main()
{
int m,n;std::cin>>m>>n;
int memory[10000] = {0};
int front = 0;
int tail = 0;int word[n];
for(int i = 0; i < n; ++i)
std::cin>>word[i];
int cnt = 0;
for(int i = 0; i < n; ++i)
{
if(!findw(word[i],memory,front,tail))
{
cnt++;
if(tail - front == m)
{
front++;
memory[tail++] = word[i];
}
else
memory[tail++] = word[i];
}
}
std::cout<<cnt;
return 0;
} -
02014-08-09 23:44:12@
第一眼看到想用队列做,不过觉得太没有技术含量,所以就拿链表做了。其实这题也可以开个0~1000的数组 arr , 其中 arr [ i ] 表示单词 i 的存储次序,0表示不存在。输入单词 k 后,查找 arr [ k ] 是否为0,若不为0,则 arr [ k ]=当前次序。随后再从 arr[ 0 ]一直搜到 arr [ 1000 ] ,若大于最大内存限制则置0。不过说到底还是链表好,省空间省时间嘛。 (下面附上代码)
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
int main(){
struct node *head,*current;
int memorySize,length;
int word=0,count=0,index=0,flag=0;head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
scanf("%d %d",&memorySize,&length);for(;length>0;length--){
scanf("%d",&word);
index=flag=1;
current=head;
while(current->next!=NULL){
current=current->next;
if(current->data==word){
flag=0;
break;
}
index++;
}
if(flag){
count++;
current->next=(struct node *)malloc(sizeof(struct node));
current->next->data=word;
current->next->next=NULL;
if(count==1)
head->next=current->next;
if(index>memorySize){
current=head->next->next;
free(head->next);
head->next=current;
}
}
}
printf("%d\n",count);
return 0;
} -
02014-08-02 08:09:28@
#include <cstdio>
using namespace std;
int q[10004];
bool inq[1003];
int ans=0,m,n;
int main()
{
scanf("%d%d",&m,&n);
int l=0,r=0;
for (int i=1,x;i<=n;i++)
{
scanf("%d",&x);
if (inq[x]) continue;
ans++;
if (r-l<m) q[++r]=x,inq[x]=true;
else
{
++l,inq[q[l]]=false;
q[++r]=x,inq[x]=true;
}
}
printf("%d\n",ans);
return 0;
} -
02014-07-19 15:36:11@
#include<cstdio>
#include<deque>
#include<algorithm>
#define IOFileName "translate"
using namespace std;
int n,m,ans=0,ipt;
deque<int>d;
int main(){
//freopen(IOFileName".in","r",stdin);
//freopen(IOFileName".out","w",stdout);
scanf("%d %d\n",&n,&m);
for(int i=0;i<m;i++){
scanf("%d ",&ipt);
if(find(d.begin(),d.end(),ipt)==d.end()){
d.push_back(ipt);
ans++;
if((int)d.size()>n){
d.pop_front();
}
}
}
printf("%d\n",ans);
}
90MS+264KB -
02014-01-01 12:01:50@
Vijos 题解:http://hi.baidu.com/umule/item/2c997f8ed9600fdae596e017
有疑问请留言 共同进步 -
02013-11-03 12:37:19@
#include <cstdio>
#include <queue>
#include <cstring>using namespace std;
#define MAXN 1001
bool f[MAXN];
queue<int>Q;
int n,m,ans=0,x;int main() {
scanf("%d%d",&m,&n);
memset(f,false,sizeof(f));
while (!Q.empty()) Q.pop();
while (n--) {
scanf("%d",&x);
if (!f[x]) {
ans++,f[x]=true;
if (Q.size()>=m) {
f[Q.front()]=false;
Q.pop();
}
Q.push(x);
}
}
printf("%d\n",ans);
return 0;
} -
02013-11-03 12:36:13@
水题。。。
#include <cstdio>
#include <queue>
#include <cstring>using namespace std;
#define MAXN 1001
bool f[MAXN];
queue<int>Q;
int n,m,ans=0,x;int main() {
scanf("%d%d",&m,&n);
memset(f,false,sizeof(f));
while (!Q.empty()) Q.pop();
while (n--) {
scanf("%d",&x);
if (!f[x]) {
ans++,f[x]=true;
if (Q.size()>=m) {
f[Q.front()]=false;
Q.pop();
}
Q.push(x);
}
}
printf("%d\n",ans);
return 0;
} -
02013-10-08 14:58:10@
开始犯了些sb 的错误- =
AC:
var
tu,ch:array[0..1001] of integer;
m,n,i,j,l,r,t,b:longint;begin
readln(m,n);
for i:=1 to n do begin
read(j);
if ch[j]=1 then continue else begin
inc(t);inc(r); tu[t]:=j; ch[j]:=1;
if t=m+b+1 then begin
inc(b); ch[tu[b]]:=0;
end;
end;
end; writeln(r);
end.