题目链接

  • 题意:

    给一个长度为n的字符串,每次删除字母同样切连续的串,假设有多个,删除最左边的、最长的串。每次删除输出串的字母,每一个字母的下标(1-n)

    N (1 ≤ N ≤
    106),串仅仅包含red (‘R’),
    green (‘G’) or blue (‘B’)
  • 分析:

    题目比較麻烦,分析一下须要的任务:

    1、每次找到最长串——优先队列

    2、删除最长串后,须要合并两側的串(假设字母同样)——加Hash的链表,set(超时)

    3、每次删除须要知道这一个区间的下标都是谁——加Hash的链表,set(超时)
C++通过,G++超时……
  1. const int MAXN = 1100000;
  2.  
  3. struct Node
  4. {
  5. int pos, len;
  6. char val;
  7. Node *nxt, *pre;
  8. Node (int p = 0, int n = 0, char v = 0) : pos(p), len(n), val(v) {}
  9. bool operator< (const Node& rhs) const
  10. {
  11. return pos < rhs.pos;
  12. }
  13. void erase()
  14. {
  15. pre->nxt = nxt;
  16. nxt->pre = pre;
  17. }
  18. } nd[MAXN], pt, fst, lst;
  19. int tot;
  20.  
  21. struct HeapNode
  22. {
  23. int val, pos;
  24. HeapNode (int v = 0, int p = 0) : val(v), pos(p) {}
  25. bool operator< (const HeapNode& rhs) const
  26. {
  27. if (val != rhs.val)
  28. return val < rhs.val;
  29. return pos > rhs.pos;
  30. }
  31. } vt;
  32.  
  33. priority_queue<HeapNode> q;
  34. char ipt[MAXN];
  35. bool vis[MAXN];
  36. Node* to[MAXN], *pit, *pl, *pr;
  37. int nxt[MAXN], pre[MAXN];
  38.  
  39. void init(int n)
  40. {
  41. REP(i, n)
  42. {
  43. nxt[i] = i + 1;
  44. if (i)
  45. pre[i] = i - 1;
  46. }
  47. }
  48. void erase(int l, int r)
  49. {
  50. int p = pre[l], n = nxt[r];
  51. nxt[p] = n;
  52. pre[n] = p;
  53. }
  54.  
  55. int main()
  56. {
  57. while (~RS(ipt))
  58. {
  59. fst.val = -1; fst.nxt = &lst; fst.pre = NULL;
  60. lst.val = -2; lst.pre = &fst; lst.nxt = NULL;
  61. CLR(vis, false);
  62. while (!q.empty())
  63. q.pop();
  64. tot = 0;
  65. int len = strlen(ipt);
  66. init(len + 2);
  67.  
  68. nd[tot++] = Node(1, 1, ipt[0]);
  69. FF(i, 1, len)
  70. {
  71. if (ipt[i] == nd[tot - 1].val)
  72. nd[tot - 1].len++;
  73. else
  74. {
  75. nd[tot].pos = i + 1;
  76. nd[tot].len = 1;
  77. nd[tot].val = ipt[i];
  78. tot++;
  79. }
  80. }
  81. fst.nxt = &nd[0]; nd[0].pre = &fst;
  82. lst.pre = &nd[tot - 1]; nd[tot - 1].nxt = &lst;
  83. REP(i, tot)
  84. {
  85. if (i != 0)
  86. nd[i].pre = &nd[i - 1];
  87. if (i != tot - 1)
  88. nd[i].nxt = &nd[i + 1];
  89. to[nd[i].pos] = &nd[i];
  90. q.push(HeapNode(nd[i].len, nd[i].pos));
  91. }
  92. while (!q.empty())
  93. {
  94. vt = q.top();
  95. q.pop();
  96. if (vt.val == 1)
  97. break;
  98. if (vis[vt.pos])
  99. continue;
  100. pt.pos = vt.pos;
  101. pit = to[vt.pos];
  102.  
  103. int idx = vt.pos;
  104. printf("%c", ipt[vt.pos - 1]);
  105. REP(i, vt.val)
  106. {
  107. printf(" %d", idx);
  108. erase(idx, idx);
  109. idx = nxt[pre[idx]];
  110. }
  111. puts("");
  112.  
  113. pl = pit->pre; pr = pit->nxt;
  114. if (pl->val == pr->val)
  115. {
  116. pl->len += pr->len;
  117.  
  118. vis[pr->pos] = true;
  119. pr->erase();
  120.  
  121. q.push(HeapNode(pl->len, pl->pos));
  122. }
  123. vis[vt.pos] = true;
  124. pit->erase();
  125. }
  126. }
  127. return 0;
  128. }

A Game with Colored Balls的更多相关文章

  1. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  2. codeforces 553A . Kyoya and Colored Balls 组合数学

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  3. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

    C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  4. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))

    C. Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k diffe ...

  6. 554C - Kyoya and Colored Balls

    554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...

  7. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  8. Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  9. codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)

    题目链接: A. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes i ...

  10. codeforces 553 A Kyoya and Colored Balls

    这个题.比赛的时候一直在往dp的方向想,可是总有一个组合数学的部分没办法求, 纯粹组合数学撸,也想不到办法-- 事实上,非常显然.. 从后往前推,把第k种颜色放在最后一个,剩下的k球.还有C(剩余的位 ...

随机推荐

  1. A canvas fillText and strokeText example

    A canvas fillText and strokeText example A canvas fillText and strokeText example

  2. distinct() 去重复

    distinct 是对整个结果集进行数据重复抑制,而不是针对每一个列. select distinct FDepartment from T_Employee

  3. hdu 2438 Turn the corner(几何+三分)

    Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...

  4. mysql的主从复制配置

    怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作:  1.1.版本一致  1.2.初始化表,并在后台启动mysql  1.3.修改root的密码 2.修 ...

  5. CSS3初步

    一.CSS与CSS3的区别 非常简单,CSS代表"Casading Style Sheets",就是样式表,是一种替代并为网站添加样式的标记性语言.现在所使用的CSS基本是在199 ...

  6. hibernate Restrictions用法

    QBC常用限定方法 Restrictions.eq --> equal,等于. Restrictions.allEq --> 参数为Map对象,使用key/value进行多个等于的比对,相 ...

  7. 第一个MyBatis程序

    最近研究了一些MyBatis技术,虽然工作中还未用到,但是觉得了解一下也是不错的.这里记录了第一个简单的Demo程序,防止自己忘记. 第一步需要配置Mybatis-config.xml文件.注意:这里 ...

  8. ibatis错误汇总

    1) 错误:The prefix "context" for element "context:property-placeholder" is not bou ...

  9. 安装Php时候报错信息:virtual memory exhausted: Cannot allocate memory (不能分配内存)

    原因是fileinfo这个函数在编译时非常消耗内存,而系统内存又不够了,所以才会出现此问题. 网上找了方法: 1,关闭其他占用大内存的进程. 2,在编译是添加参数 --disable-fileinfo

  10. Java单例模式的线程安全问题

    单例模式有两种书写模式:饿汉式和懒汉式. 1.饿汉式 class Single{ private final static Single s = new Single(); private Singl ...