D - DZY Loves Hash CodeForces - 447A
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table.
For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p.
Operation a mod b denotes taking a remainder after division a by b. However, each bucket can contain no more than one element. If DZY wants to
insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the i-th insertion,
you should output i. If no conflict happens, just output -1.
Input
The first line contains two integers, p and n (2 ≤ p, n ≤ 300). Then n lines follow. The i-th of them contains an integer xi (0 ≤ xi ≤ 109).
Output
Output a single integer — the answer to the problem.
Example
10 5
0
21
53
41
53
4
5 5
0
1
2
3
4 题意:把n个数字放到0~p-1个位置,要求x放在x%p的位置;如果已经放过 输出x的位置,否则输出-1;
(吐槽一下这个题,一直wa就是找不到原因,最后发现原来是没有数组清零,真是太坑了;)
代码:
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std; int main(){
int n,p,k,flag=;
//char c[301][20];
char m[]; unsigned long long x;
cin>>p>>n;
for(int i = ;i<p;i++)
{
m[i]=;
}//数组一定要清零,
for(int i=;i<n;i++){
cin>>x;
k=x%p;
if(m[k]==)
m[k]=;
else if(!flag)
flag=i+;
}
if(flag)cout<<flag<<endl;
else cout<<-<<endl; }
-1
D - DZY Loves Hash CodeForces - 447A的更多相关文章
- [CodeForces - 447A] A - DZY Loves Hash
A - DZY Loves Hash DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert ...
- Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- CF A. DZY Loves Hash
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...
- CF447A DZY Loves Hash 模拟
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...
- DZY Loves Colors CodeForces - 444C (线段树势能分析)
大意:有$n$个格子, 初始$i$位置的颜色为$i$, 美丽值为0, 有两种操作 将区间$[l,r]$内的元素全部改为$x$, 每个元素的美丽值增加$|x-y|$, $y$为未改动时的值 询问区间$[ ...
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...
- CF 447A(DZY Loves Hash-简单判重)
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力
D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...
随机推荐
- 利用CoreAnimation实现一个时间的进度条
(个人原创,转载请注明出处 http://www.cnblogs.com/pretty-guy/p/7460334.html) 在iOS中实现进度条通常都是通过不停的设置progress来完成的,这样 ...
- 【渗透笔记】友情检测朋友公司并拿下shell过程
一朋友在一个百货公司上班,由于无聊危险漫步就友情检测了他们公司的网站,开始我们的检测之旅吧. 打开网站,发现网站挺不错,不愧是大卖场,页面做的挺花,但是安全性怎么样呢?来试试吧.先用工具扫了下后台,结 ...
- 注册WinEdt 7
1. 先用crack算出注册码(crack在这里下载:http://download.csdn.net/detail/setoy/4384553) 放到winedt安装目录下,用管理员权限运行,点pa ...
- js 在遍历时只会显示最后一个遍历到的结果
在做项目时遇到了一个关于遍历的问题, 前提是:在ul中有n个li每个li从后台获取的数据中有一个sign的字段,当sign等于0时(li未被点击过)li会显示一个红点,当sign不等于0时(li已被点 ...
- Java并发编程-阻塞队列
Java concurrent 包中BlockingQueue接口有ArrayBlockingqueue.LinkedBlockingQueue.PriorityBlockingQueue.Synch ...
- CSS3笔记之第二天
1.CSS盒模型 指定元素的宽度和高度width,height 2.CSS边框 border-style:solid; border-width:5px; border-top-width:15px; ...
- 短视频服务大PK,阿里云、腾讯云、又拍云、七牛云、金山云5强横向对比
继直播后,短视频又再次爆发,在这个风口,国内的各大云厂商也都相继推出了自己的一站式短视频解决方案.上周由于公司短视频功能开发的需要,对比了阿里云.腾讯云.又拍云.七牛云.金山云5家的短视频服务. 先介 ...
- 大数据平台搭建-spark集群安装
版本要求 java 版本:1.8.*(1.8.0_60) 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downl ...
- vue系列之动态路由【原创】
开题 最近用vue来构建了一个小项目,由于项目是以iframe的形式嵌套在别的项目中的,所以对于登录的验证就比较的麻烦,索性后端大佬们基于现在的问题提出了解决的方案,在看到他们的解决方案之前,我先画了 ...
- java 读写锁详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt124 在java多线程中,为了提高效率有些共享资源允许同时进行多个读的操作, ...