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 ...
随机推荐
- KVM套件-linux基础
KVM套件 使用KVM的虚拟机,通过KVM的虚拟机克隆功能,提高效率,避免在实验过程中重装系统. 另外,在LINUX系统上使用KVM的虚拟化,可以获得更好性能,且可以提前熟悉企业级虚拟化技术. 过程: ...
- latex 生成pdf
我个人还是比较推崇传统的方法:先生成dvi,在生成pdf. 直接在winEdt中点击最下方的Windows Command Prompt, 否则从cmd进入命令行的话,还要进入tex文件夹,好麻烦. ...
- python+selenium自动化软件测试(第16章):基础实战(3)
#coding:utf-8 from time import sleep from selenium import webdriver class cloudedge_register(object) ...
- python+selenium自动化软件测试(第9章) :Logging模块
9.1 Logging模块 什么是日志记录?记录是跟踪运行时发生的事件的一种手段.该软件的开发人员将记录调用添加到其代码中,以指示某些事件已发生.事件由描述性消息描述,该消息可以可选地包含可变数据(即 ...
- python进阶学习(三)
本节通过SQLite了解数据库操作 ------------------------- 数据库支持 使用简单的纯文本只能实现有退限的功能,所需要引入数据库,完成更强大的功能,本节使用的简单数据库SQL ...
- java中接口实现多态举例
public class Test4 { public static void main(String[] args){ Instrument ss[]={new Wind(),new Piano() ...
- javascript学习笔记-3
1.对于javascript中的this关键字,表示的是当前代码所处的对象. var a={ get:function(){ this.val=12 } } console.log(a.val); a ...
- 学习js函数--自执行函数
我在写代码时候经常会在tpl的<script>里写类似的代码: $(function(){ alert("我好饿"); }); 刚开始的时候只知道写了它不需要调用,直接 ...
- ArrayBuffer和TypedArray,以及Blob的使用
前端使用TypedArray编辑二进制 ES6提供了, ArrayBuffer和TypedArray, 让前端也可以直接操作编辑二进制数据, 网页中的类型为file的input标签, 也可以通过Fil ...
- PHP初入,基础知识点分享(a标签&表格的嵌套&文字的处理)
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...