B - Spyke Talks
Posted 霜雪千年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B - Spyke Talks相关的知识,希望对你有一定的参考价值。
Problem description
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.
One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn‘t talking via Spyke at that moment.
Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.
Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren‘t permitted — that is, one call connects exactly two people.
Input
The first line contains integer n (1?≤?n?≤?103) — the number of secretaries in Polycarpus‘s corporation. The next line contains n space-separated integers: id1,?id2,?...,?idn (0?≤?idi?≤?109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise.
Consider the secretaries indexed from 1 to n in some way.
Output
Print a single integer — the number of pairs of chatting secretaries, or -1 if Polycarpus‘s got a mistake in his records and the described situation could not have taken place.
Examples
6
0 1 7 1 7 10
2
3
1 1 1
-1
1
0
0
Note
In the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.
In the second test sample the described situation is impossible as conferences aren‘t allowed.
解题思路:题目的意思就是从n个数字中找出m对相同的数字(不为0),即该数出现的次数刚好为2次,此时输出m;如果该数字出现的次数超过2次,输出-1;没有的话输出0,简单水过。
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1005; 4 struct NODE{ 5 int data,num; 6 }node[maxn]; 7 int main(){ 8 int n,x,k=0,m=0;bool flag0,flag1=false; 9 cin>>n; 10 for(int i=0;i<maxn;++i)node[i].num=0; 11 for(int i=1;i<=n;++i){ 12 cin>>x;flag0=false; 13 if(x!=0){//去掉0 14 for(int j=0;j<k;++j) 15 if(node[j].data==x){node[j].num++;flag0=true;break;} 16 if(!flag0){node[k].num++;node[k++].data=x;} 17 } 18 } 19 for(int i=0;i<k;++i){ 20 if(node[i].num==2)m++; 21 if(node[i].num>2){flag1=true;break;}//只要有相同数字超过2,即为-1 22 } 23 if(flag1)cout<<"-1"<<endl; 24 else if(m) cout<<m<<endl; 25 else cout<<‘0‘<<endl; 26 return 0; 27 }
以上是关于B - Spyke Talks的主要内容,如果未能解决你的问题,请参考以下文章
WSJMicrosoft’s Talks to Buy TikTok Raise Ire in China
X-TALKS Shenzhen - AI + Blockchain + Made in China 2025