Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)F. Cyclic Cipher
来源:互联网 发布:私人订制软件 编辑:程序博客网 时间:2024/05/21 14:40
题意:
给n个序列,一个序列内没有重复元素,不同序列内可能有重复元素,每个序列长度<=40,每秒钟每个序列会循环左移一次,要求算出10100 秒内,[1,m]的答案。
一个数x在某时刻的答案为,把此时所有序列的第一个元素顺序排成一个新序列,这个新序列中最长的连续x的长度。
思路:考虑对m个答案分开求,设当前要求解的数为x,那么我们只用关心所有x的位置,可能的答案一定是连续出现x的序列,假设i,j是答案中的两个序列,长度为li,lj,对应x的位置为pi,pj,显然要让pi和pj同时出现在第一个元素,则有pi+li∗k==pj+lj∗k ,然后有pi−pj==lj∗k−li∗k ,两边同时对gcd(li,lj)取模,则有(pi−pj)=0(modgcd(li,lj))
给n个序列,一个序列内没有重复元素,不同序列内可能有重复元素,每个序列长度<=40,每秒钟每个序列会循环左移一次,要求算出
一个数x在某时刻的答案为,把此时所有序列的第一个元素顺序排成一个新序列,这个新序列中最长的连续x的长度。
思路:考虑对m个答案分开求,设当前要求解的数为x,那么我们只用关心所有x的位置,可能的答案一定是连续出现x的序列,假设i,j是答案中的两个序列,长度为li,lj,对应x的位置为pi,pj,显然要让pi和pj同时出现在第一个元素,则有
,然后可以得到一个结论,两个长度相同的序列,其pi和pj一定相同。因为序列长度<=40,所以长度相同的序列肯定是很多的,一个序列出现的数是很少的,我们就可以考虑two pointer了,过程中要保证这一段出现的x是连续的
#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <algorithm>using namespace std;typedef long long ll;const int N = 1e5+5;int n, m;struct node{ int r, len, c; node(int _a = 0, int _b = 0, int _c = 0) { r = _a, len = _b, c = _c; }};vector<node>l[N];int pos[50];int cnt[50];int gcd[50][50];int _gcd(int a, int b){ return b? _gcd(b, a%b) : a; }// (xi+a*li) == (xj+b*lj)// xi-xj == b*lj-a*li// (xi-xj)%gcd(lj,li) == 0bool add(node a) { if(cnt[a.len] && pos[a.len] != a.c) return 0; for(int i = 1; i <= 40; ++i) { if(cnt[i] && (pos[i]-a.c)%gcd[i][a.len] != 0) return 0; } pos[a.len] = a.c; cnt[a.len]++; return 1;}void del(node a) { cnt[a.len]--;}int solve(vector<node>& x) { int res = 0; memset(pos, 0, sizeof(pos)); memset(cnt, 0, sizeof(cnt)); int l = 0, r = 0; while(l < x.size()) { while(r < x.size()) { if(x[r].r-x[l].r != r-l) break; if(!add(x[r])) break; ++r; } res = max(res, r-l); if(x[r].r - x[l].r != r-l) { l = r; memset(pos, 0, sizeof(pos)); memset(cnt, 0, sizeof(cnt)); } else del(x[l++]); } return res;}int main(int argc, const char * argv[]) { for(int i = 1; i < 50; ++i) { for(int j = 1; j < 50; ++j) { gcd[i][j] = _gcd(i, j); } } scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) { int k; scanf("%d", &k); for(int x, j = 1; j <= k; ++j) { scanf("%d", &x); l[x].emplace_back(node(i, k, j)); } } for(int i = 1; i <= m; ++i) { printf("%d\n", solve(l[i])); } return 0;}
0 0
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)F. Cyclic Cipher
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) F
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) -- A. Broken Clock (贪心)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) -- B. Verse Pattern(水题)
- Codeforces Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) 部分题解
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) -- D. Generating Sets(贪心)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) [D] Destroying Array
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)D. Generating Sets(贪心)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C 并查集
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) E
- codeforeces722 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
- 5.Android数据保存策略之2--SD卡
- 【c++】二叉树的线索化
- 多线程三
- CC2538知识
- 特征点匹配——使用基础矩阵、单应性矩阵的RANSAC算法去除误匹配点对
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)F. Cyclic Cipher
- 机器学习05-支持向量机_1
- OpenMV 机器视觉模块 简介
- Android使用Java反射机制,被调用的类多出未定义的field
- DualPivotQuicKSort
- 带色彩恢复的多尺度视网膜增强算法(MSRCR)的原理、实现及应用。
- 【Day26】include require include_once require_once 的区别深度总结
- FOF管理系列之五:你适合投什么样的FOF产品?
- SpringMVC-1.认识及配置