zoukankan      html  css  js  c++  java
  • Gym

    题目

    You must have heard about Agent Mahone! Dr. Ibrahim hired him to catch the cheaters in the Algorithms course. N students cheated and failed this semester and they all want to know who Mahone is in order to take revenge!

    Agent Mahone is planning to visit Amman this weekend. During his visit, there are Mplaces where he might appear. The N students are trying to cover these places with their leader Hammouri, who has been looking for Mahone since two semesters already!

    Hammouri will be commanding students to change their places according to the intel he receives. Each time he commands a student to change his position, he wants to know the number of places that are not covered by anyone.

    Can you help these desperate students and their leader Hammouri by writing an efficient program that does the job?

    Input

    The first line of input contains three integers NM and Q (2 ≤ N, M, Q ≤ 105), the number of students, the number of places, and the number of commands by Hammouri, respectively.

    Students are numbered from 1 to N. Places are numbered from 1 to M.

    The second line contains N integers, where the ith integer represents the location covered by the ith student initially.

    Each of the following Q lines represents a command and contains two integers, A and B, where A (1 ≤ AN) is the number of a student and B (1 ≤ BM) is the number of a place. The command means student number A should go and cover place number B. It is guaranteed that B is different from the place currently covered by student A.

    Changes are given in chronological order.

    Output

    After each command, print the number of uncovered places.

    Example

    Input
    4 5 4
    1 2 1 2
    1 3
    2 4
    4 5
    3 5
    Output
    2
    1
    1
    2
    题目理解:
    第一行输入n(student总人数),m(总place数),q(输入数据组数)
    第二行输入n个数字,第i个数代表第i个学生所取的place
    后面q行,每行的第一个数字是第A个学生,第二个数字是转移到的下标为B的place
    定义理解:
    stu[i]=k;//下标为i的学生的位置号为k
    Place[k]=0;//下标为k的位置学生数目为0
    place[stu[i]]=0;//下标为第i的位置号的学生所占的位置学生数为0
    sum是已占位置的数目
    m-sum是未占位置的数目

    小结:动态数组,先输入数组的大小,然后再定义
    memset(数组名,0,sizeof(数组名))可将数组置零



    Accept代码:
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        int n,m,q,k;
        cin>>n>>m>>q;
        int sum=0;//已占的位置总数 
        int place[m+1],stu[n+1];
        memset(place,0,sizeof(place));//位置人数置零 
        for(int i=1;i<=n;i++)
        {
            cin>>k;
            stu[i]=k;//下标为i的学生在第k个位置 
            if(place[k]==0) sum++;//若第k个位置的人数为0,则已坐数加1 
            place[k]++;//第k个位置的人数加1 
        }
        while(q--)
        {
            int A,B;
            cin>>A>>B;        place[stu[A]]--;//第A个学生的位置的人数减1 
            if(place[stu[A]]==0) sum--;// 第A个学生的位置的人数若为1,已占位置数减1 
            stu[A]=B;//第A个学生的位置变为B 
            if(place[B]==0) sum++;//第B个位置的人数若为0,已占位置数加1 
            place[B]++;//第B位置的人数加1 
            cout<<m-sum<<endl;
         } 
         return 0;
    }
    
    
    
    
    

     超时代码:

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int main()
    {
        int n,m,q,loc,pass,now;
        cin>>n>>m>>q;
        int arr[n][m];
        memset(arr,0,sizeof(arr));
        memset(arr[0],1,sizeof(arr[0]));
        for(int i=0;i<n;i++)
        {
            cin>>loc;
            arr[i][loc-1]=1;
        }
        for(int j=0;j<q;j++)
        { int count=0,stu;
            cin>>stu;
            for(int i=0;i<m;i++)
            arr[stu-1][i]=0;
            cin>>now;
            arr[stu-1][now-1]=1;
            for(int i=0;i<m;i++)
            {
                int sum=0;
                for(int k=0;k<n;k++)
                {
                    sum=sum+arr[k][i];
                }
                if(sum==0)
                count++;
            }
            if(j<q-1) 
            cout<<count<<endl;
            if(j==q-1)
            cout<<count;
        }
        return 0;
    }
  • 相关阅读:
    Elasticsearch5.x批量插入数据(Java)
    Elasticsearch5.x创建索引(Java)
    Kibana5.x界面简要介绍(含x-pack插件)
    CentOS6.5安装Kibana5.3.0
    Elasticsearch5.x Head插件安装
    CentOS6.5安装Elasticsearch5.3.0
    jenkins发布Spring Cloud(单机版)
    prometheus 监控docker
    gitlab迁移单个项目
    python使用pika操作rabbitmq
  • 原文地址:https://www.cnblogs.com/XuYiting/p/9272709.html
Copyright © 2011-2022 走看看