Dancing Lessons

Time Limit: 5000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 45C
64-bit integer IO format: %I64d      Java class name: (Any)

 

There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.

 

Input

The first line contains an integer n (1 ≤ n ≤ 2·105) — the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the dancing skill. People are specified from left to right in the order in which they lined up.

 

Output

Print the resulting number of couples k. Then print k lines containing two numerals each — the numbers of people forming the couple. The people are numbered with integers from1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.

 

Sample Input

Input
  1. 4
    BGBG
    4 2 4 3
Output
  1. 2
    3 4
    1 2
Input
  1. 4
    BBGG
    4 6 1 5
Output
  1. 2
    2 3
    1 4
Input
  1. 4
    BGBB
    1 1 2 3
Output
  1. 1
    1 2

Source

 
 
解题:双向链表+优先队列
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #define LL long long
  13. #define INF 0x3f3f3f3f
  14. using namespace std;
  15. const int maxn = ;
  16. struct pl{
  17. int skill,pre,next;
  18. char sex;
  19. };
  20. struct pp {
  21. int x,y,df;
  22. friend bool operator<(const pp &a,const pp &b) {
  23. return (a.df > b.df || a.df == b.df && a.x > b.x);
  24. }
  25. };
  26. priority_queue<pp>q;
  27. bool vis[maxn];
  28. pl p[maxn];
  29. char str[maxn];
  30. int ans[maxn][];
  31. int main(){
  32. int n,i,j,df,index,next,head;
  33. int pre,tot,temp,u;
  34. while(~scanf("%d",&n)){
  35. scanf("%s",str+);
  36. for(i = ; i <= n; i++){
  37. p[i].next = i+;
  38. p[i].pre = i-;
  39. p[i].sex = str[i];
  40. scanf("%d",&p[i].skill);
  41. }
  42. p[i-].next = -;
  43. tot = head = ;
  44. p[].next = ;
  45. memset(vis,false,sizeof(vis));
  46. while(!q.empty()) q.pop();
  47. for(i = ; i < n; i++){
  48. if(str[i] != str[i+])
  49. q.push((pp){i,i+,abs(p[i].skill-p[i+].skill)});
  50. }
  51. tot = ;
  52. while(!q.empty()){
  53. pp cur = q.top();
  54. q.pop();
  55. if(vis[cur.x] || vis[cur.y]) continue;
  56. ans[tot][] = cur.x;
  57. ans[tot++][] = cur.y;
  58. vis[cur.x] = true;
  59. vis[cur.y] = true;
  60. int u = p[cur.x].pre;
  61. int v = p[p[p[u].next].next].next;
  62. p[u].next = v;
  63. p[v].pre = u;
  64. if(u && v != - && str[u] != str[v]){
  65. q.push((pp){u,v,abs(p[u].skill-p[v].skill)});
  66. }
  67. }
  68. printf("%d\n",tot);
  69. for(i = ; i < tot; i++)
  70. printf("%d %d\n",ans[i][],ans[i][]);
  71. }
  72. return ;
  73. }

xtu summer individual 4 C - Dancing Lessons的更多相关文章

  1. codeforces 45C C. Dancing Lessons STL

    C. Dancing Lessons   There are n people taking dancing lessons. Every person is characterized by his ...

  2. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  3. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  4. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  5. xtu summer individual 1 A - An interesting mobile game

    An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...

  6. xtu summer individual 2 D - Colliders

    Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  7. xtu summer individual 1 C - Design the city

    C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu D ...

  8. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  9. xtu summer individual 1 D - Round Numbers

    D - Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u D ...

随机推荐

  1. DFS水题 URAL 1152 False Mirrors

    题目传送门 /* 题意:一个圈,每个点有怪兽,每一次射击能消灭它左右和自己,剩余的每只怪兽攻击 搜索水题:sum记录剩余的攻击总和,tot记录承受的伤害,当伤害超过ans时,结束,算是剪枝吧 回溯写挫 ...

  2. 第02课 操作系统及Linux 系统介绍

    1.操作系统介绍 操作系统(Operating System,简称OS),是计算机系统中必不可少的基础系统软件,它是应用程序运行以及用户操作必备的基础环境支撑,是计算机系统的核心. 操作系统的作用是管 ...

  3. 在Android 源码中添加系统服务

    Android系统本身提供了很多系统服务,如WindowManagerService,PowerManagerService等.下面描述一下添加一个系统服务的具体步骤. 1.定义自定义系统服务接口 撰 ...

  4. 转--oracle查看允许的最大连接数和当前连接数等信息

    两个参数间的关系:sessions=1.1*processes+5 目前总结的语句,在查看数据的连接情况很有用,写完程序一边测试代码一边查看数据库连接的释放情况有助于分析优化出一个健壮的系统程序来. ...

  5. CZ-python01-06

    练习代码 练习代码 # Python注释 # 注释不是越多越好,对于一目了然的代码,不需要添加注释 # 对于复杂的操作,应该在操作开始填上若干行注释 # 对于不是一目了然的代码,应在其行尾添加注释(为 ...

  6. Hadoop体系结构管理

    一.查看Zookeeper信息 [hadoop@weekend01 ~]$zkServer.sh status JMX enabled by default Using config: /hadoop ...

  7. 第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法

    第一章 熟悉Objective -C   编写高质量iOS与OS  X代码的52 个有效方法   第一条: 了解Objective-C 语言的起源 关键区别在于 :使用消息结构的语言,其运行时所应执行 ...

  8. 6 Specialzed layers 特殊层 第二部分 读书笔记

    CAGradientLayer   CAGradientLayer is used to generate a smooth gradient between two or more colors. ...

  9. 搜索模板elasticsearch

    搜索: like 对中文分词效率与支持都不太友好elasticsearch 实时的(效率高).分布式(可扩展)的搜索和分析引擎,基于Lucene全文搜索引擎工具包,算法基于倒排索引算法(eg:一篇文章 ...

  10. TensorFlow 安装 Win10 Python+GPU

    前叙:有灵魂的程序都是每一个程序员的最终目标.TensorFlow了解下? 打算花几个月学机器学习,TensorFlow是很好的选择,折腾了会环境,略有心得分享下. 环境:win10 Python:3 ...