考虑将两个单词变成有序,我们可以得到一个或者两个旋转次数的区间。

然后考虑将两组单词变成有序,比如[l,mid]和[mid+1,r],对于mid和mid+1这两个单词我们可以求出使他们有序的旋转次数的区间。

然后将这个区间与[l,mid]的区间以及[mid+1,r]的区间求交,就可以得到使[l,r]有序的旋转次数的区间。

上面这个过程我们可以用分治来进行,区间求交可以用扫描线法。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int> words[];
  5. int cnt[];
  6. int n, c;
  7. int tot = ;
  8.  
  9. void calc(int a, int b)
  10. {
  11. int idx = ;
  12. while (idx < words[a].size() && idx < words[b].size())
  13. {
  14. if (words[a][idx] != words[b][idx])
  15. break;
  16. idx++;
  17. }
  18. if (idx < words[a].size() && idx < words[b].size())
  19. {
  20. if (words[a][idx] < words[b][idx])
  21. {
  22. cnt[]++;
  23. cnt[c - words[b][idx] + ]--;
  24. cnt[c + - words[a][idx]]++;
  25. cnt[c]--;
  26. tot++;
  27. }
  28. else
  29. {
  30. cnt[c + - words[a][idx]]++;
  31. cnt[c - words[b][idx] + ]--;
  32. tot++;
  33. }
  34. }
  35. else if (idx == words[a].size() && idx != words[b].size())
  36. {
  37. cnt[]++;
  38. cnt[c]--;
  39. tot++;
  40. }
  41. else if (idx != words[a].size() && idx == words[b].size())
  42. tot++;
  43. else
  44. {
  45. cnt[]++;
  46. cnt[c]--;
  47. tot++;
  48. }
  49. }
  50.  
  51. void divideConquer(int l, int r)
  52. {
  53. if (l == r)
  54. return;
  55. int mid = (l + r) >> ;
  56. divideConquer(l, mid);
  57. divideConquer(mid + , r);
  58. calc(mid, mid + );
  59. }
  60.  
  61. int main()
  62. {
  63. scanf("%d%d", &n, &c);
  64. for (int i = ; i < n; i++)
  65. {
  66. int l, w;
  67. scanf("%d", &l);
  68. while (l--)
  69. {
  70. scanf("%d", &w);
  71. words[i].push_back(w);
  72. }
  73. }
  74. divideConquer(, n - );
  75. bool ok = false;
  76. int sum = ;
  77. for (int i = ; i < c; i++)
  78. {
  79. sum += cnt[i];
  80. if (sum == tot)
  81. {
  82. ok = true;
  83. printf("%d", i);
  84. break;
  85. }
  86. }
  87. if (!ok)
  88. printf("-1");
  89. return ;
  90. }

Codeforces731D 80-th Level Archeology的更多相关文章

  1. 80-th Level Archeology

    80-th Level Archeology time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. Codeforces Round #376 (Div. 2) D. 80-th Level Archeology —— 差分法 + 线段扫描法

    题目链接:http://codeforces.com/contest/731/problem/D D. 80-th Level Archeology time limit per test 2 sec ...

  3. CodeForces 731D 80-th Level Archeology

    区间并. 对于上下两个数字,如果不一样,那么可以计算出哪一段范围内可以保证字典序,并且后面所有位置都无需再考虑.对所有范围求交集就是答案了. 求交集写起来有点烦,直接对不可取的范围求并即可. #pra ...

  4. 【codeforces 731D】80-th Level Archeology

    [题目链接]:http://codeforces.com/contest/731/problem/D [题意] 给你n个象形文; 每个象形文由l[i]个数字组成; 你可以把所有的组成象形文的数字同时增 ...

  5. html5-表单

    例子: text,number,email 的输入框 <!-- required:必填项 --> <!-- autofocus:获得焦点 --> <!-- placeho ...

  6. 纯CSS3制作学生入学档案表单样式代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. nc命令总结

    1.远程拷贝文件从server1拷贝文件到server2上.需要先在server2上,用nc激活监听,server2上运行: 引用 [root@hatest2 tmp]# nc -l 1234 > ...

  8. [Canvas]Bombman v1.04

    Bombman是我仿造红白机上经典游戏爆破小人,用Canvas制作的一款网页版单机游戏, 自我感觉还是有一定的可玩性. 本游戏的胜利条件是用雷消灭所有怪物,但被怪物即使是擦边碰到或是炸弹火焰炸到就算失 ...

  9. [Canvas]Bombman v1.00

    爆破小人Canvas版,请点此下载,并用浏览器打开试玩. 图例: 源码: <!DOCTYPE html> <html lang="utf-8"> <m ...

随机推荐

  1. v-if v-else-if v-else

    1.代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  2. hdu 2795(单点改动)

    题意:有h×w大的公告板.有n条公告要写入,每条公告高度都是1,宽度是wi,每次从最上最左的空位写,假设有空位输出第几行.假设没有足够空位输出-1. 题解:注意h最大1e9.但事实上是看n的大小.由于 ...

  3. [Pyhton]weakref 弱引用

    文档中的解释: https://docs.python.org/2/library/weakref.html wiki 中的解释: 在计算机程序设计中,弱引用.与强引用相对.是指不能确保其引用的对象不 ...

  4. bash_profile打不开怎么办,用nano .bash_profile打开

    I’ve spent years curating a collection of Mac bash aliases and shortcuts to make my life easier. My ...

  5. C系列语言终极校对宝典【第一、第二部分】

    第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...

  6. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  7. 利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)

    前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...

  8. JVM垃圾回收算法 及 垃圾收集器

    摘自<深入理解Java虚拟机> 一.什么是: GC算法是 方法论,那么垃圾收集器就是具体的 实现. 二.四种 垃圾回收算法 1.标记-清除算法:最基础的收集算法:不足有两点:1标记和清除两 ...

  9. 解决ubuntu没有/var/log/messages的问题

    1:root身份打开 /etc/rsyslog.d/50-default.conf 2:把注释#去掉 #*.=info;*.=notice;*.=warn;\ # auth,authpriv.none ...

  10. 编译android内核和文件系统,已经安装jdk,提示build/core/config.mk:268: *** Error: could not find jdk tools.jar

    1:确保安装jdk,如果没有安装请移布:http://www.cnblogs.com/jiuyueguang/p/3156621.html 2:如果已经安装了jdk,还是提示此错误, 解决方法 请确保 ...