51 Nod 1282 时钟 (循环中的最小表示+哈希)
题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
有N个时钟,每个时钟有M个指针,P个刻度。时钟是圆形的,P个刻度均分整个圆。每个时钟每个指针指向整数刻度,并且每个时钟自身指针指向的数字都不同。你可以任意旋转时钟的表盘,但是你不能转指针。问最后有多少对时钟可以变成相同的状态。
例如:N = 5,M = 2,P = 4,5个时钟的数据如下{1, 2} {2, 4} {4, 3} {2, 3} {1, 3}

经过旋转后。 其中(1, 3), (1, 4), (2, 5) 和 (3, 4)是相同的。

给出所有时钟的数据,求有多少对时钟是相同的。
Input
第1行:3个数N, M, P中间用空格分隔,其中N为时钟的数量,M为表针的数量,P为刻度的数量(1 <= M, N <= 500, 1 <= P <= 10^9, M <= P)。
第2 - N + 1行:每行M个数,对应一个时钟,M个指针的位置。
Output
输出有多少对时钟是相同的。
Input示例
5 2 4
1 2
2 4
4 3
2 3
1 3
Output示例
4
#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
int n,m,p;
int a[505];
int b[505];
int c[505];
unordered_map<string,int>mp;
int getmin(int *s,int len){
int n=len;
int i=0,j=1,k=0,t;
while(i<n && j<n && k<n){
t=s[(i+k)%n]-s[(j+k)%n];
if (!t) k++;
else{
if (t>0) i+=k+1;
else j+=k+1;
if (i==j) j++;
k=0;
}
}
return i<j?i:j;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
scanf("%d%d%d",&n,&m,&p);
string ss;
while(n--)
{
for(int i=0;i<m;++i)scanf("%d",&c[i]);
sort(c,c+m);
for(int i=0;i<m-1;++i)
{
a[i]=c[i+1]-c[i];
}
a[m-1]=c[0]+p-c[m-1];
int pos=getmin(a,m);
int pp=0;
for(int i=pos;i<m;i++)
b[pp++]=a[i];
for(int i=0;i<pos;i++)b[pp++]=a[i];
/*
for(int i=0;i<m;i++)cout<<a[i]<<" ";
cout<<endl;
for(int i=0;i<m;i++)cout<<b[i]<<" ";
cout<<endl;*/
ss="";
for(int i=0;i<m;i++)
ss+=std::to_string(b[i]);
// cout<<ss<<endl;
mp[ss]++;
}
ll ans=0;
unordered_map<string,int>::iterator it=mp.begin();
int tmp;
while(it!=mp.end())
{
tmp=it->second;
if(tmp<=1){it++;continue;}
else ans+=(tmp*(tmp-1))/2,it++;
}
cout<<ans<<endl;
return 0;
}
51 Nod 1282 时钟 (循环中的最小表示+哈希)的更多相关文章
- 51Nod 1282 时钟 —— 最小表示法 + 字符串哈希
题目链接:https://vjudge.net/problem/51Nod-1282 1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难 ...
- 1282 时钟(最小表示法+hash)
1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N个时钟,每个时钟有M个指针,P个刻度.时钟是圆形的,P个刻度均分整 ...
- 51 nod 1406 与查询
1406 与查询 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有n个整数.输出他之中和x相与之后结果为x的有多少个.x从0 ...
- FPGA异步时钟设计中的同步策略
1 引言 基于FPGA的数字系统设计中大都推荐采用同步时序的设计,也就是单时钟系统.但是实际的工程中,纯粹单时钟系统设计的情况很少,特别是设计模块与外围芯片的通信中,跨时钟域的情况经常不可避免. ...
- 51 nod 1439 互质对(Moblus容斥)
1439 互质对 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有n个数字,a[1],a[2],…,a[n].有一个集合,刚开 ...
- 51 nod 1427 文明 (并查集 + 树的直径)
1427 文明 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 安德鲁在玩一个叫“文明”的游戏.大妈正在帮助他. 这个游 ...
- 51 nod 1421 最大MOD值
1421 最大MOD值 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以 ...
- 51 nod 1681 公共祖先 (主席树+dfs序)
1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...
- 51 nod 1456 小K的技术(强连通 + 并查集)
1456 小K的技术 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 苏塞克王国是世界上创新技术的领先国家,在王国中有n个城市 ...
随机推荐
- MySQL8在CentOS7上的安装
Install_CentOS7_MySQL8_binary.sh #!/bin/bash MySQL_Package=mysql-8.0.16-linux-glibc2.12-x86_64.tar.x ...
- WebMvcConfigurationSupport跨域和fastjson全局替换
@Configuration public class WarnWebMvcConfigurationSupport extends WebMvcConfigurationSupport { /** ...
- Struts框架的使用初步
Struts框架的使用初步: A:Apache下载struts.2.1.8.rar包. B:解压空工程,进入apps目录. C:将struts2的基本jar包拷到工程的lib目录中. D:配置web. ...
- O044、一张图秒懂 Nova 16种操作
参考https://www.cnblogs.com/CloudMan6/p/5565757.html
- ubuntu下安装vue/cli提示No command 'vue' found
通过官方指令 npm install -g @vue/cli 安装vue脚手架提示: No command 'vue' found, did you mean: Command 'vpe' from ...
- 修改this的指向
call var a={ name:'xuux', fn:function(a,b){ console.log(a+b); console.log(this);//{name: "xuux& ...
- vue防重复点击(指令实现)
快速点击按钮会重复多次调用接口,防止出现这样的情况 全局定义,方便调用 新建plugins.js export default { install (Vue) { // 防重复点击(指令实现) Vue ...
- django 使用mysql数据库
一 修改settings里面的配置文件 import pymysql # 一定要添加这两行!通过pip install pymysql! 或者pycharm 里面安装 pymysql.install_ ...
- vbox的四种网络模式
一.NAT模式 特点: 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间不能ping通 3.虚拟机可以ping通主机(此时ping虚拟机的网关,即是ping主机) 4.主机不能ping通虚拟机 ...
- C语言的宏macro的使用
C's Macro Introduction 1.The Connect Macros: ## 这是一个预处理连接符,这个操作符主要用来将两个符号连接成为一个完整的宏符号.通过下面的代码,可以看到其具 ...