题解

63 条题解

  • 2
    @ 2017-10-31 16:21:13

    简单易懂的模拟(。・д・。)

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;    
    int m,n,sum=0,error,cs=1,now=1;
    int nc[105];
    int dc[1005];
    int search(int k)
    { 
        while(cs<=n)
        {
        error=0;
        for(int i=1;i<=m;++i)
        if(nc[i]!=k)
        error++;
        if(error==m)
        {
            sum++;
            if(now<=m)
            {
                nc[now]=k;
                now++;
            }
            else
            {
                for(int i=1;i<=m-1;++i)
                nc[i]=nc[i+1];
                nc[m]=k;
            }
        }
        cs++;
        search(dc[cs]);
        }
        return sum;
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        memset(nc,-1,sizeof(nc));
        memset(dc,0,sizeof(dc));
        cin>>m>>n;  
        if(n==0)
        {
            cout<<"0";
            return 0;
        }
        if(m==0)
        {
            cout<<n;
            return 0;
        }
        for(int i=1;i<=n;++i)
        cin>>dc[i];
        search(dc[1]);
        cout<<sum;
        return 0;
    }
    
  • 1
    @ 2021-09-04 15:16:15
    #include <bits/stdc++.h>
    using namespace std;
    
    int n,m,x,ans,l,r,a[1005],b[1005];
    int main()
    {
        cin>>m>>n;
        l=0;r=0;
        for(int i=1; i<=n; i++){
            cin>>x;
            if(a[x]==0){
                ans++;
                r++;
                b[r]=x;
                a[x]=1;
                if (r>m){
                    l++;
                    a[b[l]]=0;
                }
            }
        }
        cout<<ans;
        return 0;
    }
    
  • 1
    @ 2018-06-02 20:13:55

    #include <iostream>
    #include <cstring>
    using namespace std;
    int m[1500],n[1500];
    int main(int argc, char** argv)
    {
    //freopen("d1.in","r",stdin);
    //freopen("r1.out","w",stdout);
    int m1,n1,k=0,o=0;
    cin>>m1>>n1;
    for(int i=1;i<=n1;i++)
    {
    cin>>n[i];
    }
    memset(m,-1,sizeof(m));
    for(int j=1;j<=n1;j++)
    {
    int tt=1;
    for(int l=o;l<m1+o;l++)
    {
    if(n[j]==m[l])
    {
    tt=0;
    break;
    }
    }
    if(tt)
    {
    int oo=1;
    for(int ll=o;ll<m1+o;ll++)
    {
    if(m[ll]==-1)
    {
    m[ll]=n[j];
    oo=0;
    k++;
    break;
    }
    }
    if(oo)
    {
    m[m1+o]=n[j];
    o++;
    k++;
    }
    }
    }
    cout<<k;
    return 0;
    }

  • 1
    @ 2017-10-31 14:43:51

    平易近人的朴素代码(。・`ω´・)

    #include<iostream>
    #include<cstring>
     using namespace std;
     int a[1000];int b[100];
     int main()
     {  int m,n,i,k,sum=0,l=0,u=0;
    memset(a,-1,sizeof(a)); memset(b,-1,sizeof(b)); 
    cin>>m>>n;  
    for(i=0;i<n;i++)    
    cin>>a[i];    
    for(i=0;i<n;i++)  
    { for(k=0;k<m;k++)  
    { if(a[i]!=b[k])      
    l++;      }   
    if(l==k)      
    { sum++;   
    b[u]=a[i];   
     u++;      
     if(u==m)     
    u=0;      }    
    l=0;    }  
    cout<<sum;  
     return 0;}
    
  • 1
    @ 2017-10-21 10:00:27

    pascal,用循环队列实现

    var
    m,n:longint;
    ram:array[0..127]of longint;//内存
    a:longint;
    i,h,t,ans:longint;
    function search:boolean;//查找是否在内存中
    var k:longint;
    begin
    search:=false;
    k:=h;
    while k<>t do
    begin
    if ram[k]=a then exit(true);
    inc(k);if k=m then k:=0;
    end;
    if ram[t]=a then exit(true);
    end;
    begin
    readln(m,n);
    h:=0;t:=0;ans:=1;
    read(a);
    ram[t]:=a;//首次读入
    for i:=2 to n do
    begin
    read(a);
    if not(search) then
    begin
    inc(ans);
    inc(t);
    if t=m then t:=0;
    if t=h then inc(h);
    if h=m then h:=0;//调整队列
    ram[t]:=a;
    end;
    end;
    writeln(ans);
    end.

  • 1
    @ 2017-10-09 16:27:24
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iomanip>
    #include<cmath>
    #include<stack>
    #include<cstring>
    #define maxa 1000+100
    #define inf 300
    #define FOR(i,x,y) for(i=x;i<=y;++i)
    using namespace std;
    int a[maxa];
    int main()
    {
        int m,n;
        vector<int >  s;
        cin>>m>>n;
    int i;
        FOR(i,1,n)
        {
            cin>>a[i];
        }
        int ans = 0;
        FOR(i,1,n)
        {
            if(s.size()<m){
            if(count(s.begin(),s.end(),a[i])==0){
            s.push_back(a[i]);
            ans++;
            }
            }
            else if(s.size()==m)
            {
               if(count(s.begin(),s.end(),a[i])==0){
            s.erase(s.begin());
            s.push_back(a[i]);
            ans++;
            }
            }
        }
        cout<<ans<<endl;
    }
    
    
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iomanip>
    #include<cmath>
    #include<stack>
    #include<cstring>
    #define maxa 1000+100
    #define inf 300
    #define FOR(i,x,y) for(i=x;i<=y;++i)
    using namespace std;
    int a[maxa];
    int s[maxa];
    bool check(int l,int r,int t)
    {
        int i;
        FOR(i,l,r-1)
        if(s[i]==t)
            return true;
        return false;
    }
    int main()
    {
        int m,n,l,r;
         l = r = 1;
        cin>>m>>n;
    int i;
        FOR(i,1,n)
        {
            cin>>a[i];
        }
        int ans = 0;
        FOR(i,1,n)
        {
            if(r-l<m){
            if(!check(l,r,a[i])){
            s[r++] = a[i];
            ans++;
            }
            }
            else if(r-l==m)
            {
               if(!check(l,r,a[i])){
            l++;
            s[r++] = a[i];
            ans++;
            }
            }
        }
        cout<<ans<<endl;
    }
    
    
  • 1
    @ 2017-09-27 18:37:31
    //在查找上搞了个小优化
    #include<iostream>
    using namespace std;
    int a[1010],d[1010];
    int main()
    {
        int n,m,i,now=0,num,ans=0;
        cin>>m>>n;
        for(i=1;i<=n;i++)
        {
            cin>>num;
            if(d[num])
             continue;
            if(now<m)
            {
             now++;
             ans++;
             a[now]=num;
             d[num]=1;
            }
            else
            { 
             ans++;
             now++;
             d[a[now-m]]=0;
             a[now]=num;
             d[num]=1;
            
            }
        }
        cout<<ans;
        return 0;
    }
    
  • 1
    @ 2017-09-02 11:50:18

    var m,n,k1,k2,i,s,ans:longint; a,b:array [0..1001] of longint;
    begin
    readln(m,n);
    k1:=1; k2:=0;
    for i:=1 to n do
    begin
    read(s);
    if a[s]=0 then
    begin
    if k2-k1+1<m then
    begin
    inc(k2);
    b[k2]:=s;
    a[s]:=1;
    end else
    begin
    inc(k2);
    b[k2]:=s;
    inc(k1);
    a[b[k1-1]]:=0;
    a[s]:=1;
    end;
    inc(ans);
    end;
    end;
    writeln(ans);
    end.

  • 1
    @ 2017-07-13 01:18:35

    哈?竟然又碰到这种水题???

    #include<iostream>
    #include<vector>
    using namespace std;
    int main(void){
        vector<int>memory;
        int Capacity,wordsNum,word,count=0;
        cin>>Capacity>>wordsNum;
        for(int i=0;i<wordsNum;i++){
            int flag=0;
            cin>>word;
            for(const auto p:memory){
                if(p==word){
                    flag=1;
                    break;
                }
            }
            if(flag){
            }
            else{
                count++;
                memory.push_back(word);
                if(memory.size()>Capacity){
                    memory.erase(memory.begin());
                }
            }
        }
        cout<<count<<endl;
    } 
    
  • 1
    @ 2016-10-15 10:43:02
    var
      i,m,n,x,j,k:longint;
      b:boolean;
      a:array[0..1000] of longint;
    begin
      readln(m,n);fillchar(a,sizeof(a),-1);
      for i:=1 to n do
      begin
        read(x);b:=true;
        for j:=0 to m do
        if a[j]=x then b:=false;
        if b then
        begin  a[k mod m]:=x; inc(k);end;
      end;
      writeln(k);
    end.
    
  • 0
    @ 2018-12-12 22:34:36
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    queue<int> q;
    int book[1001];
    int ans=0,n,m;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>m>>n;
        for(int i=0;i<n;i++)
        {
            int cur;
            cin>>cur;
            if(book[cur]==0)
            {
                q.push(cur);
                book[cur]=1;
                ans++;
                if(q.size()>m)
                {
                    book[q.front()]=0;
                    q.pop();
                }
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    
  • 0
    @ 2018-09-27 21:10:40

    队列题
    var
    head,tail,c,m,n,i,ans:longint;
    a:array[1..100001]of boolean;
    b:array[1..100001]of longint;//定义队列
    begin
    readln(m,n);
    head:=1;tail:=1;
    fillchar(a,sizeof(a),false);
    for i:=1 to n do
    begin
    read(c);
    if not(a[c]) then//判断是否在内存中
    begin
    a[c]:=true;//进内存
    inc(ans);
    b[tail]:=c;//入队列
    inc(tail);//
    if ans>m then//满了就出队列
    begin
    a[b[head]]:=false;
    inc(head);
    end;
    end;
    end;
    write(ans);
    end.

  • 0
    @ 2018-09-27 21:10:09

    队列题
    var
    head,tail,c,m,n,i,ans:longint;
    a:array[1..100001]of boolean;
    b:array[1..100001]of longint;//定义队列
    begin
    readln(m,n);
    head:=1;tail:=1;
    fillchar(a,sizeof(a),false);
    for i:=1 to n do
    begin
    read(c);
    if not(a[c]) then//判断是否在内存中
    begin
    a[c]:=true;//进内存
    inc(ans);
    b[tail]:=c;//入队列
    inc(tail);//
    if ans>m then//满了就出队列
    begin
    a[b[head]]:=false;
    inc(head);
    end;
    end;
    end;
    write(ans);
    end.

  • 0
    @ 2018-09-27 20:55:36

    var
    head,tail,c,m,n,i,ans:longint;
    a:array[1..100001]of boolean;
    b:array[1..100001]of longint;
    begin
    readln(m,n);
    head:=1;tail:=1;
    fillchar(a,sizeof(a),false);
    for i:=1 to n do
    begin
    read(c);
    if not(a[c]) then
    begin
    a[c]:=true;
    inc(ans);
    b[tail]:=c;
    inc(tail);
    if ans>m then
    begin
    a[b[head]]:=false;
    inc(head);
    end;
    end;
    end;
    write(ans);
    end.

  • 0
    @ 2018-09-27 20:55:10

    简单的队列问题。
    var
    head,tail,c,m,n,i,ans:longint;
    a:array[1..100001]of boolean;//检查是否在内存中
    b:array[1..100001]of longint;//队列数组
    begin
    readln(m,n);
    head:=1;tail:=1;
    fillchar(a,sizeof(a),false);
    for i:=1 to n do
    begin
    read(c);
    if not(a[c]) then//判断是否在队列中
    begin
    a[c]:=true;
    inc(ans);
    b[tail]:=c;
    inc(tail);
    if ans>m then//将队列首移除队列
    begin
    a[b[head]]:=false;//移除内存
    inc(head);
    end;
    end;
    end;
    write(ans);
    end.

  • 0
    @ 2018-05-09 10:58:24

    简单的模拟,用queue当词典

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    bool hasword(int wd, queue<int> que)
    {
        int k;
        while(!que.empty())
        {
            k = que.front();
            que.pop();
            if (wd == k)
                return true;
        }
        return false;
    }
    
    void pushone(int word, queue<int> &que, int len)
    {
        if (que.size() < len)
            que.push(word);
        else // ==
        {
            que.pop();
            que.push(word);
        }
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
    
        int m, n, word;
        cin >> m >> n;
        queue<int> q;
        int res = 0;
    
        for (int i = 1; i <= n; i++)
        {
            cin >> word;
            if(!hasword(word, q))
            {
                pushone(word, q, m);
                res++;
            }
        }
    
        cout << res;
        return 0;
    }
    
  • 0
    @ 2018-03-25 15:27:49

    简单队列线性处理。用时6分钟
    时间复杂度O(1)

    #include<iostream>
    #include<queue>
    using namespace std;
    bool book[100000];
    int main()
    {
        int m,n,temp,cnt=0;
        queue <int> q;
        cin>>m>>n;
        for(int i=0;i<n;i++)
        {
            cin>>temp;
            if(book[temp]==0)
            {
                if(q.size()<m)
                {
                    q.push(temp);
                    book[temp]=1;
                    cnt++;
                }
                else
                {
                    book[q.front()]=0;
                    q.pop();
                    q.push(temp);
                    book[temp]=1;
                    cnt++;
                }
            }   
        }
        cout<<cnt;
        return 0;
    }
    
    
    
  • 0
    @ 2017-11-15 15:32:50

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    vector <int> a;
    int n,m,word,ans=0;
    cin>>m>>n;
    for(int i=0;i<n;i++){
    cin>>word;
    if(find(a.begin(),a.end(),word)==a.end()){
    a.push_back(word);
    ans++;
    if(a.size()>m)
    a.erase(a.begin());
    }
    }
    cout<<ans;
    return 0;
    }

  • 0
    @ 2017-11-15 15:31:57

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    vector <int> a;
    int n,m,word,ans=0;
    cin>>m>>n;
    for(int i=0;i<n;i++){
    cin>>word;
    if(find(a.begin(),a.end(),word)==a.end()){
    a.push_back(word);
    ans++;
    if(a.size()>m)
    a.erase(a.begin());
    }
    }
    cout<<ans;
    return 0;
    }

  • 0
    @ 2017-11-06 21:33:39

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    queue<int>q;//开一个m的队列
    bool f[1005];//定义布尔型数组,判断真假
    int main()
    {
    int m,n,x,ans=0;
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++)
    {
    scanf("%d",&x);//读入
    if(f[x]==0&&q.size()<m)//标记内存中没有的为假
    {
    f[x]=1;//标记为真
    q.push(x);//入队
    ans++;//记录查询次数
    }
    else//内存中有则为真
    if(f[x]==0)
    {
    f[q.front()]=0; //压入新元素则标记为假
    q.pop();//弹出最早入队元素
    q.push(x);//入队
    f[x]=1;//标记为真
    ans++;//记录查询次数
    }
    }
    printf("%d",ans);//输出
    return 0;//撒花
    }

信息

ID
1774
难度
4
分类
模拟 点击显示
标签
递交数
3064
已通过
1231
通过率
40%
被复制
11
上传者