题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12588

在判断 ‘+’ 的时候使用了 重叠度 的概念,跟一般的 “先去掉这个位置,看剩下的位置能不能符合条件” 方法不太一样。

代码如下:

  1. #include <algorithm>
  2. #include <numeric>
  3. #include <iterator>
  4. #include <functional>
  5.  
  6. #include <iostream>
  7. #include <sstream>
  8.  
  9. #include <string>
  10. #include <vector>
  11. #include <queue>
  12. #include <stack>
  13. #include <set>
  14. #include <map>
  15.  
  16. #include <cstdio>
  17. #include <cstdlib>
  18. #include <cctype>
  19. #include <climits>
  20. #include <cmath>
  21. #include <cstring>
  22.  
  23. using namespace std;
  24.  
  25. /************** Program Begin *********************/
  26. const int MAX = 50;
  27. int flag[MAX];
  28. class SurveillanceSystem {
  29. public:
  30. string getContainerInfo(string containers, vector <int> reports, int L) {
  31. string res(containers.size(), '-');
  32. vector < vector<int> > pos( containers.size()+1 );
  33. // pos[c]表示能够监视到c个'X'的所有的位置的起始处
  34. vector <int> rep_count( L+1 ); // rep_count[c]表示,reports中c出现的次数
  35.  
  36. for (int i = 0; i <= containers.size()-L; i++) {
  37. int c = count(&containers[i], &containers[i]+L, 'X');
  38. // 计算各个位置能监视到的'X'
  39. pos[c].push_back(i);
  40. }
  41. for (int i = 0; i < reports.size(); i++) {
  42. ++rep_count[ reports[i] ];
  43. }
  44.  
  45. for (int i = 0; i <= L; i++) {
  46. int rc = rep_count[i];
  47. if (rc == 0) {
  48. continue;
  49. }
  50.  
  51. // 表示i在reports中出现rc次,而满足可以监视到i个'X'的段保存在pos[i]中,
  52. // 一共有pos[i].size个这样的段,也就是要从中选出rc个段,使用每个位置的
  53. // 重叠度来判断该位置是否一定会被监视到。位置的重叠度表示该位置在
  54. // pos[i].size个段中出现的次数。如果其出现次数大于pos[i].size-rc,则
  55. // 这个位置一定会被监视到。因为如果这个位置没有被监视的话,那么找不符合
  56. // 条件的rc个段。
  57. memset(flag, 0, sizeof(flag));
  58. for (int j = 0; j < pos[i].size(); j++) {
  59. for (int k = 0; k < L; k++) {
  60. if ('-' == res[ pos[i][j] + k ]) {
  61. // 在段中出现的位置置为'?'
  62. res[ pos[i][j] + k ] = '?';
  63. }
  64. ++flag[ pos[i][j]+k ]; // 该位置重叠度加1
  65. }
  66. }
  67. for (int j = 0; j < containers.size(); j++) {
  68. if (flag[j] > pos[i].size() - rc) {
  69. //判断重叠度,判断该位置是否必定被监视
  70. res[j] = '+';
  71. }
  72. }
  73. }
  74.  
  75. return res;
  76. }
  77. };
  78.  
  79. /************** Program End ************************/

SRM 581 D2 L2:SurveillanceSystem,重叠度的更多相关文章

  1. SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12707 算法决定一切,这道题目有很多方法解,个人认为这里 ve ...

  2. SRM 581 D2 L3:TreeUnionDiv2,Floyd算法

    题目来源:http://community.topcoder.com//stat?c=problem_statement&pm=12587&rd=15501 这道题目开始以为是要在无向 ...

  3. SRM 620 DIV1 L2

    题意:有n个等长的string(设string的长度为m),string中的字符从'A'到'Z',容许对m列执行稳定的排序操作,问说是否能通过这m种操作将这n个string调整成对应的顺序. 题解: ...

  4. SRM 585 DIV1 L2

    记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include & ...

  5. SRM 588 D2 L3:GameInDarknessDiv2,DFS

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12710 采用DFS搜索,第一次写的时候忘了加访问标志,结果状态 ...

  6. Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1

    据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...

  7. 高效求幂取余 算法,复杂度 log(n)

    做TopCoder SRM 576 D2 L3 题目时,程序有个地方需要对一个数大量求幂并取余,导致程序运行时间很长,看了Editoral之后,发现一个超级高效的求幂并取余的算法,之前做System ...

  8. 利用opencv作透明重叠人群密度热度图

    在作热度图的时候我们经常需要将热度图调整透明度后叠加在原图上达到更好的展示效果.比如检测人气密度的热度图: (来自sensetime) 一般作图的时候会第一时间想到matplotlib,因为可以很方便 ...

  9. py_faster_rcnn识别出来的结果好多红框重叠

    py_faster_rcnn识别出来的结果好多红框重叠, 可以通过调节demo.py中的NMS_THRESH的值进行限制. NMS_THRESH表示非极大值抑制,这个值越小表示要求的红框重叠度越小,0 ...

随机推荐

  1. SQL Server sp_configure 控制内存使用

    背景知识: sp_configure   显示或更改当前服务器的全局配置设置(使用 sp_configure 可以显示或更改服务器级别的设置.) 查看 全局配置值 方法 1.execute sp_co ...

  2. 常用的IO流

    常用的IO流 •根据处理数据类型的不同分为:字节流和字符流 •根据数据流向不同分为:输入流和输出流 字节流:字节流以字节(8bit)为单位,能处理所有类型的数据(如图片.avi等). 字节输入流:In ...

  3. 经典CSS颜色混合模式

    转自:http://www.webhek.com/css-blend-mode/ 注意:只有使用最新版的谷歌浏览器.火狐浏览器,才能正确的显示本文中的演示. Photoshop里最没有用处的一种功能— ...

  4. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  5. MS SQL 当记录不存在时插入insert INTO not exists

    INSERT INTO dbo.[T_DabaoTemp]  ([PType]           ,[pID]           ,[NewVersion]           ,[ParentC ...

  6. last reboot

    last reboot是一个linux命令,查看上次重启时间等相关信息

  7. cassandra命令

    压力测试:cassandra-stress [command] -node [nodes] -mode thrift user=[user] password=[password] example: ...

  8. 每个人应该知道的NVelocity用法

    NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定义的对象.从而使得界面设 ...

  9. NSRangeFromString(<#NSString * _Nonnull aString#>) 和rangeOfString

    NSRangeFromString NSString *str1 = @"abcdef"; NSString *str2 = @"1-105"; NSStrin ...

  10. c# 另存为excel

    去网上找了一下  看了一个比较简单的新建excel然后另存为. 要引用Microsoft.Office.Interop.Excel命名空间,如果没有的话 ,百度比我懂. 直接付代码: Microsof ...