CF A. DZY Loves Hash
1 second
256 megabytes
standard input
standard output
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.
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 a single integer — the answer to the problem.
10 5
0
21
53
41
53
4
5 5
0
1
2
3
4
-1
//题意就是找相等的数,输出第二个的位置,可是要是最先发现的。
比如:10 5
1 2 2 2 1
输出是3而不是5,由于先找到2和2相等,假设仅仅用for循环,找到的是1和1相等输出是5.
第4个例子卡了非常久,没看懂题目。 。。。
#include <iostream>
using namespace std;
int main()
{ __int64 a[400];
int n,t,i,j,p,k;
while(scanf("%d%d",&p,&n)!=EOF)
{ memset(a,0,sizeof(a));
t=0;
for(i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
a[i]=a[i]%p;
}
k=n;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
if(a[i]==a[j])
{
k=k<(j+1)? k:(j+1);
t=1;
} }
if(t==1)
printf("%d\n",k);
if(t==0)
printf("-1\n");
}
return 0;
}
CF A. DZY Loves Hash的更多相关文章
- 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 ...
- [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 ...
- CF 447A(DZY Loves Hash-简单判重)
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- CF 444C DZY Loves Physics(图论结论题)
题目链接: 传送门 DZY Loves Chemistry time limit per test1 second memory limit per test256 megabytes Des ...
- CF 445B DZY Loves Chemistry(并查集)
题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second memory limit per test:256 megabytes D ...
- CF 444A(DZY Loves Physics-低密度脂蛋白诱导子图)
A. DZY Loves Physics time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 444B(DZY Loves FFT-时间复杂度)
B. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Cf 444C DZY Loves Colors(段树)
DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...
- CF 445A(DZY Loves Chessboard-BW填充)
A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- eclipse 重构代码自动抽取函数
1.选择重构代码段 2.重构 – 抽取方法 3.命名重构代码段抽取的方法 4.使用抽取的方法
- HTTP协议header头域
HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内 容请参考RFC2616.HTTP协议采用了请求/响应模型.客 ...
- 【IntelliJ IDEA】1.安装使用IntelliJ IDEA
IntelliJ IDEA,初次接触,被赞许的收费版IDE环境. =================================================================== ...
- 【Android】attr、style和theme
一.Attr 属性,风格样式的最小单元: Attr 的定义 在自定义 View 的时候,在 res/attrs.xml 文件中声明属性,而Android 系统的属性也是以同样的方式定义的.比如 lay ...
- MATLAB逻辑函数
%%逻辑函数 %%all:判断是否有元素非0,A是多维矩阵,all(A)是以列为单位来处理的,当前列的逻辑 %值为1,当且仅当当前列的每一个元素都非0 A=[1,2,3;0,2,1;5,0,2]; % ...
- Windows脚本\批处理命令学习笔记
1.为新建变量赋值: set 变量=值 2.输出变量的值 echo %变量% 3.关闭批处理中命令行的显示(默认是显示命令行的) 在文件開始处增加:echo off 若需又一次显示:echo on 若 ...
- java中的占位符\t\n\r\f
\t 相当于tab,缩进\n NewLine 换行 System.out.println("aaa\tbbb"); //aaa bbbSystem.out.println(&quo ...
- JAVA Eclipse的Android文件结构是怎么样的
默认res目录下面存放了界面需要的布局和图片文件,之所以图片分为hdpi,ldpi,mdpi这些,是为了不同的设备准备的(高/中/低分辨率的图片) Bin目录类似于VS的debug或者releas ...
- [Android]egit取消文件版本号控制
开发项目,多人合作开发变得越来越重要了,在此同一时候,使用git作为协同工具也是越来越多.在此.介绍一下egit取消文件版本号控制的方法. (egit即为eclipse中的git插件) 1.打开Nav ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...