题解

63 条题解

  • 0
    @ 2017-10-31 16:21:48

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

    #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;
    }
    
  • 0
    @ 2017-10-02 13:51:23
    #include<iostream>
    using namespace std;
    int main()
    {
        int m,n,ans=0,word,js=0;
        bool p;
        cin>>m>>n;
        int b[m+1];
        for (int i=1;i<=m;i++) b[i]=-1;//初值设为-1 
        for (int i=1;i<=n;i++)
        {
            cin>>word; p=true;
              for (int k=1;k<=m;k++)
              {
                if (word==b[k]) p=false;
              }
            if (p) {
                ans++; js++; 
                if (js>m)
                {
                  for (int k=2;k<=m;k++) 
                   {
                      b[k-1]=b[k];
                   } 
                b[m]=word;  js--;   
                } else 
                if (js<=m) b[js]=word;
            }       
        }
        cout<<ans<<endl;
        return 0;
    }**H2O**
    
  • 0
    @ 2017-03-10 20:11:20

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>

    using namespace std;

    int main()
    {
    int m,n,word,ans=0,s=1,num=0,de=1;
    int a[1100]={0};
    int c[1100];
    cin>>m>>n;
    for(int i=1;i<=n;i++)
    {
    cin>>word;
    if(a[word]==0)
    {
    a[word]=1;
    ans++;
    c[s]=word;
    s++;
    num++;
    if(num>m)
    {
    a[c[de]]=0;
    de++;
    }
    }
    }
    cout<<ans;
    return 0;
    }

  • 0
    @ 2016-12-10 23:40:59
    import java.util.Scanner;
    
    
    public class Main {
        public static void main(String[] args){
            Scanner cin = new Scanner(System.in);
            
            int[] dic = new int[101];
            
            int m = cin.nextInt() , n = cin.nextInt() ,ans = 0 ,size = 0;
            
            for(int  i = 0 ; i < n ; i ++){
                int x = cin.nextInt();
                boolean tmp = false;
                for(int j = 0 ; j < size ; j ++){
                    if( dic[j] == x ){
                        tmp = true;
                        break;
                    }
                }
                if( tmp){
                    continue;
                }
                else{
                    ans++;
                    if(size < m){
                        dic[size++] = x;
                        continue;
                    }
                    else if( size == m){
                        for(int j = 1 ; j < m ; j ++){
                            dic[j-1] = dic[j];
                        }
                        dic[m-1] = x;
                    }
                }
            }
            System.out.println(ans);
            cin.close();
        }
    }
    

    评测结果
    编译成功

    测试数据 #0: Accepted, time = 78 ms, mem = 43008 KiB, score = 10
    测试数据 #1: Accepted, time = 93 ms, mem = 43004 KiB, score = 10
    测试数据 #2: Accepted, time = 93 ms, mem = 43012 KiB, score = 10
    测试数据 #3: Accepted, time = 109 ms, mem = 43008 KiB, score = 10
    测试数据 #4: Accepted, time = 109 ms, mem = 43016 KiB, score = 10
    测试数据 #5: Accepted, time = 109 ms, mem = 43000 KiB, score = 10
    测试数据 #6: Accepted, time = 125 ms, mem = 43008 KiB, score = 10
    测试数据 #7: Accepted, time = 109 ms, mem = 43016 KiB, score = 10
    测试数据 #8: Accepted, time = 109 ms, mem = 43004 KiB, score = 10
    测试数据 #9: Accepted, time = 125 ms, mem = 43008 KiB, score = 10
    Accepted, time = 1059 ms, mem = 43016 KiB, score = 100
    ###AC啦啦啦

  • 0
    @ 2016-11-08 20:28:06

    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=1024;
    bool instack[maxn];
    int a,n,m,tot=0,ans=0;
    int s[maxn];

    void push(int x)
    {
    if(s[0]==m)
    {
    tot++;
    if(tot>m) tot-=m;
    instack[s[tot]]=false;
    s[tot]=x;
    instack[x]=true;
    }
    else
    {
    s[++s[0]]=x;
    instack[x]=true;
    }
    }

    int main()
    {
    freopen("translate.in","r",stdin);
    freopen("translate.out","w",stdout);
    memset(instack,false,sizeof(instack));
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++)
    {
    scanf("%d",&a);
    if(!instack[a])
    {
    push(a);
    ans++;
    }
    }
    printf("%d",ans);
    }

  • 0
    @ 2016-11-08 20:27:32

    测试数据 #0: Accepted, time = 0 ms, mem = 512 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 508 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 516 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 512 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 512 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 516 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 512 KiB, score = 10
    测试数据 #7: Accepted, time = 15 ms, mem = 508 KiB, score = 10
    测试数据 #8: Accepted, time = 15 ms, mem = 508 KiB, score = 10
    测试数据 #9: Accepted, time = 15 ms, mem = 512 KiB, score = 10
    Accepted, time = 45 ms, mem = 516 KiB, score = 100

  • 0
    @ 2016-11-08 20:19:33

    、、、c++
    #include<cstdio>
    #define maxn 5000
    int ans=0,n,s,m,head=1,count=0,tail=1;
    int a[maxn],fifo[maxn];
    int main(){
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++){
    scanf("%d",&s);
    if(a[s]==0){
    ans++;
    fifo[tail]=s;
    tail++;
    a[s]=1;
    count++;
    if(count>m){
    a[fifo[head]]=0;
    head++;
    }

    }
    }
    printf("%d",ans);
    return 0;
    }
    、、、
    水题。。就是桶+队列

  • 0
    @ 2016-10-11 20:07:53

    #include<cstdlib>
    #include<cstdio>
    #include<vector>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<stack>
    #include<ctime>
    #include<cmath>
    #include<map>
    #include<cctype>
    #define f(x,y) for(int x=1;x<=y;x++)
    using namespace std;
    inline int read() {
    register int x = 0;
    register char c = getchar() ;
    register int f = 1;
    while (isspace(c)) c = getchar() ;
    if(c == '-') f = -1,c = getchar();
    do x = x * 10 + c - '0', c = getchar();
    while (isdigit(c)) ;
    return x * f;
    }
    int st[1005];
    int pos = 1 ;
    int m,n;
    int ans ;
    int now = 1;

    void ser(int x) {
    for (int i = 1 ; i <= pos ; i++)
    if (st[i] == x) return ;
    if (pos < m) st[pos ++] = x ,ans ++;
    else st[now] = x, now = now % m + 1,ans ++;
    }
    int main() {
    memset(st,-1,sizeof(st));
    m = read (), n = read ();
    f(i,n) ser (read ());
    printf ("%d", ans);
    return 0;
    }

  • 0
    @ 2016-08-21 20:24:50

    水题var
    m,n,i,j,k,num:longint;
    a,b:array[1..10000] of longint;
    t:boolean;
    begin
    num:=0;
    readln(m,n);
    for i:=1 to m do begin
    b[i]:=1000000;
    end;
    for i:=1 to n do begin
    read(a[i]);
    end;
    readln;
    for i:=1 to n do begin
    t:=false;
    for j:=1 to m do begin
    if a[i]=b[j] then begin
    t:=true;
    break;
    end;
    end;
    if t=false then begin
    inc(num);
    for k:=2 to m do begin
    b[k-1]:=b[k];
    end;
    b[m]:=a[i];
    end;
    end;
    writeln(num);
    end.

  • 0
    @ 2016-07-27 21:53:21

    var
    a:array[1..2000]of longint;
    f:array[1..200]of longint;
    m,n,i,j,sum,p:longint;
    t:boolean;
    begin
    for i:=1 to 200 do f[i]:=-1;
    readln(m,n);
    for i:=1 to n do read(a[i]);
    sum:=0;
    p:=1;
    for i:=1 to n do
    begin
    t:=false;
    for j:=1 to m do
    begin
    if f[j]=a[i] then
    begin
    t:=true;
    break;
    end;
    end;
    if t=false then
    begin
    inc(sum);
    f[((p-1) mod m)+1]:=a[i]; {防止变为0}
    inc(p);
    end;
    end;
    writeln(sum);
    end.

  • 0
    @ 2016-07-14 19:20:54

    #include<iostream>
    #include<cstdio>
    #include<queue>
    using namespace std;
    int M,N,i;
    int k;
    int in[1010]={0};
    int flag=0;
    int main(){
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    queue<int>q;
    cin>>M>>N;
    for(i=1;i<=N;i++){
    cin>>k;
    if(in[k]==0){
    in[k]=1;
    q.push(k);
    if(q.size()>M){
    k=q.front();
    in[k]=0;
    q.pop();
    }
    flag++;
    }

    }
    cout<<flag;
    return 0;
    }

  • 0
    @ 2016-07-12 16:36:03

    #include <cstdio>
    #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(q.size()>max){
    temp=q.front();
    word[temp]=0;
    q.pop();
    }
    flag++;
    }
    }
    cout<<flag;
    return 0;
    }

  • 0
    @ 2016-02-12 23:31:45

    看清是非负整数,数组不能初始化为0
    Pascal AC
    var m,n,i,j,k,t,s,l:longint;
    a:array[1..100]of longint;
    begin
    readln(m,n);
    for i:=1 to m do a[i]:=-1;
    for i:=1 to n do
    begin
    read(k);
    t:=0;
    for j:=1 to m do
    if a[j]=k then t:=1;
    if t=0 then
    begin
    l:=0;
    for j:=1 to m do
    if a[j]=-1 then
    begin
    a[j]:=k;
    l:=1;
    break;
    end;
    if l=0 then
    begin
    for j:=1 to m-1 do
    a[j]:=a[j+1];
    a[m]:=k;
    end;
    inc(s);
    end;
    end;
    write(s);
    end.

  • 0
    @ 2015-11-04 00:58:52

    #include <iostream>
    #include <vector>
    #include <cstdio>

    using namespace std;

    int M,N;
    int bo_ol[1001];
    vector<int>word;

    int main()
    {
    scanf("%d %d",&M,&N);
    int book[N];
    int i,j=0;
    for(i=0;i<N;++i)
    {
    scanf("%d",&book[i]);
    }
    for(i=0;i<N;++i)
    {
    if(bo_ol[ book[i] ]!=1)
    {
    bo_ol[ book[i] ]=1;
    if(word.size()>=M)
    {
    bo_ol[ word[0] ]=0;
    word.erase( word.begin() );
    }
    word.push_back( book[i] );
    j++;
    }
    }
    cout<<j;
    return 0;
    }

  • 0
    @ 2015-10-30 21:54:51

    //类似队列
    var m,n,i,x,ans,head,tail:longint;
    q:array[0..1001] of longint;
    flag:array[0..1001] of boolean;
    begin
    fillchar(flag,sizeof(flag),false);
    fillchar(q,sizeof(q),0);
    head:=0;
    tail:=0;
    readln(m,n);
    for i:=1 to n do
    begin
    read(x);
    if flag[x]=true then continue;
    if not flag[x] then
    begin
    inc(tail);
    q[tail]:=x;
    flag[x]:=true;
    inc(ans);
    if tail-head>m then
    begin
    inc(head);
    flag[q[head]]:=false;
    end;
    end;
    end;
    writeln(ans);
    end.

  • 0
    @ 2015-10-28 21:14:56

    我说呢,我明明想到了0的情况赋-1,为什么还是错8号点。原来!!原来我把循环加到readln上面然后1 to m...

    还不如1 to 1000呢= =

    不过这个错误其实挺好排的。算了,反正水题。

    这题很简单,按照题目要求循环模拟即可,循环类似约瑟夫。主要是0的问题

    ###pascal code
    program P1774;
    var m,n,i,ans,head,num:longint;
    a:array[1..1000] of longint;
    have:array[0..1000] of boolean;
    procedure next(var x:longint);
    begin
    inc(x); if x>m then dec(x,m);
    end;

    begin
    fillchar(have,sizeof(have),false);
    read(m,n); head:=0; for i:=1 to m do a[i]:=-1;
    for i:=1 to n do
    begin
    read(num);
    if have[num]=false then
    begin
    inc(ans); next(head);
    if a[head]=-1 then
    begin
    a[head]:=num; have[num]:=true;
    end
    else
    begin
    have[a[head]]:=false; a[head]:=num; have[num]:=true;
    end;
    end;
    end;
    write(ans);
    end.

  • 0
    @ 2015-10-24 10:45:50

    想到有0的情况却没有完善对0的处理
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>

    using namespace std;

    int m, n;
    int h[1005], p[1005];

    int main(int argc, const char *argv[]) {
    scanf("%d %d", &m, &n);
    int ans = 0, now = 0;
    memset(h, -1, sizeof (h));
    memset(p, -1, sizeof (p));
    if (m == 0) {
    printf("%d\n", n);
    return 0;
    }
    for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    if (h[x] != -1)
    continue;
    if (p[now] != -1)
    h[p[now]] = -1;
    h[x] = now;
    p[now] = x;
    ++ans;
    ++now;
    now %= m;
    }
    printf("%d\n", ans);
    return 0;
    }

  • 0
    @ 2015-10-17 21:34:42

    #include<cstdio>
    #include<cstring>
    int m[105],n[1005];

    int main()
    {
    int M,N,ok;
    scanf("%d%d%d",&M,&N,&n[0]);
    int first=0,last=1;
    for(int j=-1;j<N;j++){
    int f;
    scanf("%d",&f);
    ok=1;
    for(int i=first;i<last;i++){
    if(f==n[i]){ok=0;break;}
    }
    if(ok&&(last-first)==M){
    n[first]=0;
    first++;
    n[last]=f;
    last++;
    }else if(ok&&(last-first)<M){
    n[last]=f;
    last++;
    }
    }
    printf("%d",last);
    return 0;
    }

  • 0
    @ 2015-10-03 16:13:42

    #include<stdio.h>
    int n,m,v[1005],ans,num,sub;
    int main()
    { int i,k;
    scanf("%d%d",&n,&m);
    for(i=0,k;i<m;i++)
    {scanf("%d",&k);
    if(v[k]<=sub)
    v[k]=++num,sub+=(num-sub-1==n),ans++;}
    printf("%d",ans);
    return 0;}

    • @ 2015-10-26 19:38:23

      看不懂。。。恕我无能。。为什么v[k]=++num,sub+=(num-sub-1==n),ans++;}

信息

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