标签:failed logic 一个人 std sizeof ges 计数 nts any
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 M places 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 N, M 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?≤?A?≤?N) is the number of a student and B (1?≤?B?≤?M) 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
4 5 4
1 2 1 2
1 3
2 4
4 5
3 5
2
1
1
2
题意:第一行输入三个数,N、M、Q,N代表有N个人,M代表M个地方,Q代表Q个地方,第二个行输入N个整数,第一个数表示第一个人去的地方,第二个数表示第二个人去的地方,以此类推。接下来的Q行代表Q个指令,每行两个数u和v,表示第u个人去第v个地方,每执行完一个指令输出没有任何人的地方个数。
题解:用两个数组,第一个数组a[i]表示第i个人所去的地方,b[j]表示第j个地方的人数,定义一个ans变量对没人的地方进行计数,每执行一次指令,对a[i],b[j],ans进行更新即可。
具体分析详见代码:
#include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<stack> #include<queue> #define ll long long using namespace std; int main(){ ll n,m,q; int u,v; cin>>n>>m>>q; int ans=m; ll a[n+1]; memset(a,0,sizeof(a)); ll b[m+1]; memset(b,0,sizeof(b)); for(int i=1;i<=n;i++){ cin>>a[i]; b[a[i]]++; if(b[a[i]]==1) ans--; } while(q--){ cin>>u>>v; b[a[u]]--; if(b[a[u]]==0) ans++; a[u]=v; b[v]++; if(b[v]==1) ans--; cout<<ans<<endl; } return 0; }
标签:failed logic 一个人 std sizeof ges 计数 nts any
原文地址:https://www.cnblogs.com/zjl192628928/p/9273159.html