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
Input
6
0 1 7 1 7 10
Output
2
Input
3
1 1 1
Output
-1
Input
1
0
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.
解题思路:题目的意思就是从n个数字中找出m对相同的数字(不为0),即该数出现的次数刚好为2次,此时输出m;如果该数字出现的次数超过2次,输出-1;没有的话输出0,简单水过。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct NODE{
int data,num;
}node[maxn];
int main(){
int n,x,k=,m=;bool flag0,flag1=false;
cin>>n;
for(int i=;i<maxn;++i)node[i].num=;
for(int i=;i<=n;++i){
cin>>x;flag0=false;
if(x!=){//去掉0
for(int j=;j<k;++j)
if(node[j].data==x){node[j].num++;flag0=true;break;}
if(!flag0){node[k].num++;node[k++].data=x;}
}
}
for(int i=;i<k;++i){
if(node[i].num==)m++;
if(node[i].num>){flag1=true;break;}//只要有相同数字超过2,即为-1
}
if(flag1)cout<<"-1"<<endl;
else if(m) cout<<m<<endl;
else cout<<''<<endl;
return ;
}
B - Spyke Talks的更多相关文章
- myeclispe启动后报错 Subclipse talks to Subversion via a Java API that requires access to native libraries.
myeclispe 中SVN插件常遇到的异常: Subclipse talks to Subversion via a Java API that requires access to native ...
- 100 Most Popular Machine Learning Video Talks
100 Most Popular Machine Learning Video Talks 26971 views, 1:00:45, Gaussian Process Basics, David ...
- VK Cup 2012 Qualification Round 1 E. Phone Talks —— DP
题目链接:http://codeforces.com/contest/158/problem/E E. Phone Talks time limit per test 3 seconds memory ...
- Takin Talks·上海 |开源后首场主题研讨会来了,一起解密Takin技术吧!
自 6 月 25 日全球首款生产环境全链路压测平台 Takin 正式开源,短短 13 天时间,Github 主页上 Star 数已超过 730,开发者社群也积累了 1500+粉丝.群内技术研讨氛围 ...
- Codeforces 158E Phone Talks
http://codeforces.com/contest/158/problem/E 题目大意: 麦克是个名人每天都要接n电话,每通电话给出打来的时间和持续时间,麦克可以选择接或不接,但是只能不接k ...
- (zhuan) Some Talks about Dual Learning
研究|对偶学习:一种新的机器学习范式 this blog copy from: http://www.msra.cn/zh-cn/news/blogs/2016/12/dual-learning-2 ...
- Book Contents Reviews Notes Errata Articles Talks Downloads Resources Code Formatter Cover of C# in Depth Order now (3rd edition) Implementing the Singleton Pattern in C#
原文链接地址: http://csharpindepth.com/Articles/General/Singleton.aspx#unsafe Implementing the Singleton P ...
- myeclispe2014启动后报错 Subclipse talks to Subversion via a Java API that requires access to native libraries.
解决方案: Window -> Preferences -> Team -> SVN, 将SVN接口的Client修改为如图所示
- Codeforces 158E Phone Talks:dp
题目链接:http://codeforces.com/problemset/problem/158/E 题意: 你有n个电话要接,每个电话打进来的时刻为第t[i]分钟,时长为d[i]分钟. 每一个电话 ...
随机推荐
- C++多个文本读取问题
同时使用两个 ifstream和 freopen 第二个就会失去效用,不知道错在了哪里! 1. 使用freopen打开: bool CPicToolsDlg::readTxt2Seq( std::st ...
- PS CC2018 命令大全
1.图像: 设置图像大小:图像->图像大小->设置宽高 约束比例: 解除约束比例: 2.设置大小像素图片不模糊: 双击当前图层->新建图层样式->输入名称->确定-> ...
- XML的解析方式
//解析和输出XML public void showXml() { string filepath = Application.dataPath + @"/my.xml"; if ...
- vue 强制刷新组件
<component v-if="hackReset"></component> 2 3 4 this.hackReset = false this.$ne ...
- 图的遍历---DFS
类型一:邻接表 题目一:员工的重要性 题目描述 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度 ...
- salt 根据ip修改主机名
首先定义pillar [root@web1 pillar]# cat hostname.sls ip_hostname: 10.1.1.1: web1 10.1.1.2: web2 10.1.1.3: ...
- 不用form怎么post数据
数据传输是用户交互中最重要的环节,下面收集了几个数据传输的方法,作为记录(未测试,在使用之前需要测试,如果后面我测试了,会对已测试项进行标注) 一. 网址传递 <a href=”test.php ...
- phpstorm zhuce ma
username :cf96PiPYt271u1TC License Key : 97807-12042010 00001GctOKh8f206hliugiW6iAS5bh 8AAXfEtfpdDSr ...
- AtCoder ARC 076D - Built?
传送门:http://arc076.contest.atcoder.jp/tasks/arc076_b 本题是一个图论问题——Manhattan距离最小生成树(MST). 在一个平面网格上有n个格点, ...
- Redis启动出错 noauth authentication required.
输入认证过的密码即可. 在命令行中运行: auth password