A. Spyke Talks

来源:互联网 发布:菜鸟网络校园加盟条件 编辑:程序博客网 时间:2024/04/24 03:51

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
60 1 7 1 7 10
output
2
input
31 1 1
output
-1
input
10
output
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.



解题说明:此题只需要统计出现的非0数字的个数,如果为2就加1,超过2就输出-1


#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;int main(){   int n,i,k,temp,j;int a[1003][2];int count;memset(a,0,sizeof(a));scanf("%d",&n);k=0;for(i=0;i<n;i++){scanf("%d",&temp);for(j=0;j<k;j++){if(temp==a[j][0]){a[j][1]++;break;}}if(j==k){a[k][0]=temp;a[k][1]=1;k++;}}count=0;for(i=0;i<k;i++){if(a[i][1]==2&&a[i][0]!=0){count++;}if(a[i][1]>=3&&a[i][0]!=0){count=-1;break;}}printf("%d\n",count);return 0;}


原创粉丝点击