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 ...
随机推荐
- 实现NFS共享wordpress
author:JevonWei 版权声明:原创作品 两台Web服务器,一台mysql服务器,一台NFS服务器,一台DNS服务器 拓扑结构 拓扑环境 web1 192.168.198.110 web2 ...
- 边看MHA源码边学Perl语言之一开篇
边看MHA源码边学Perl语言之一开篇 自我简介 先简单介绍一下自己,到目前为此我已经做了7年左右的JAVA和3年左右php开发与管理,做java时主要开发物流行业的相关软件,对台湾快递,国际快递,国 ...
- el表达式里面fn的用法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp31 头部加入标签库 <%@ taglib prefix=" ...
- Python中os和shutil模块实用方法集…
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- java 多线程(0) Java线程
线程 线程是系统调度的基本单元,每当创建一个进程时,会有许多的线程,也叫轻量级进程,在一个进程中拥有多个线程,各自都有自己的计数器,堆和局部变量属性,并且能够分享内存变量. 为什么要使用多线程 1. ...
- 第二次项目冲刺(Beta阶段)--第七天
一.站立式会议照片 二.项目燃尽图 三.项目进展 codingnet:https://git.coding.net/tuoxie/chachong-beta.git 1.对项目进行全面的测试 2.继续 ...
- 团队作业4——第一次项目冲刺(Alpha版本)第六天and第七天
团队作业4--第一次项目冲刺(Alpha版本)第六天and第七天 第一次项目冲刺(Alpha版本)第六天 一.Daily Scrum Meeting照片 二.燃尽图 1.解释说明横纵坐标代表的含义 ...
- 201521145048《Java程序设计管理》第一周学习总结
1. 本周学习总结 学习并了解Java的发展与历史 在网上视频中学习Java 了解并区分JVM JRE JDK 将java与已学语言做比较,发现相同处 2. 书面作业 Q1.为什么java程序可以跨平 ...
- Java课程设计 购物车系统(个人博客) 201521123052 蓝锦明
1. 团队课程设计博客链接 课程设计团队博客 2. 个人负责模块或任务说明 (1)制作图形菜单引导界面 (2)定义各获取和输出类函数 3. 自己的代码提交记录截图 4. 自己负责模块或任务详细说明 i ...
- 多线程面试题系列(14):读者写者问题继 读写锁SRWLock
在第十一篇文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法--读写锁SRWLock来解决这一问题.读写锁在对资源进行保护的同时,还 ...