CF 45C Dancing Lessons(优先队列)
来源:互联网 发布:linux du显示行号 编辑:程序博客网 时间:2024/06/03 23:48
http://codeforces.com/problemset/problem/45/C
Description
There are n people taking dancing lessons. Every person is characterized by his/her dancing skillai. 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 ofai 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 containsn symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line containsn 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
4BGBG4 2 4 3
23 41 2
4BBGG4 6 1 5
22 31 4
4BGBB1 1 2 3
11 2
题目大意: 有n个人在上舞蹈课,每个人都有相应的舞蹈技能。记录男女
在一起直到结尾,然后选出男女差值最小的一对,如果有多对,
就从左往右输出,选出后剩下的成员紧密排在一起,直到不能
找到男女搭配。问:输出组合对数,并且每队成员最初时的位
置(标号)。
分析:维护一个优先队列,按差值为关键字,如果差值一样则按从左往右排序。
然后用vis记录是否别访问过,然后再合并继续输出满足条件的,直到不
能搭配。
时间复杂度:o(n) 空间复杂度:o(n)
#include<iostream>#include<cstring>#include<queue>#include<cstdio>#include<cmath>using namespace std;const int maxn=300005;int L[maxn],R[maxn],skill[maxn];char s[maxn];int n;bool vis[maxn];struct node{ int u; int v; int c;};bool operator <(const node a,const node b){ if(a.c^b.c)return a.c>b.c; else return a.u>b.u;}priority_queue<node>q;int main(){ while(cin>>n) { while(!q.empty()) q.pop(); cin>>s+1; node z; int num=0; for(int i=1;i<=n;i++) { L[i]=i-1; R[i]=i+1; if(s[i]=='B'){ num +=1; } } num=min(num,n-num); for(int i=1;i<=n;i++) cin>>skill[i]; for(int i=1;i<n;i++) { if(s[i] != s[i+1]) { z.u=i; z.v=i+1; z.c=abs(skill[i]-skill[i+1]); q.push(z); } } printf("%d\n",num); memset(vis,0,sizeof(vis)); while(num--) { while(!q.empty()) { z=q.top(); q.pop(); if(!vis[z.u] && !vis[z.v]) { printf("%d %d\n",z.u,z.v); vis[z.u]=1; vis[z.v]=1; break; } } int b=L[z.u]; int t=R[z.v]; R[b]=t; L[t]=b; z.u=b; z.v=t; z.c=abs(skill[b]-skill[t]); if(b>0 && t<=n && s[b]!=s[t]) q.push(z); } } return 0;}
- CF 45C Dancing Lessons(优先队列)
- CF 45C Dancing Lessons(优先队列)
- codeforces 45c dancing lessons(优先队列)
- Codeforces 45C. Dancing Lessons (SBT)
- CodeForces 45C Dancing Lessons
- CodeForces 45CDancing Lessons 优先队列
- CF45-C. Dancing Lessons(STL或线段树)
- cf - 140 C. New Year Snowmen(贪心+优先队列)
- cf 721 d(优先队列+贪心)
- 【CF】C. Glass Carving(二分 + 树状数组 + 优先队列 + 数组计数)
- 【CF】【318div2A】【优先队列】
- cf gym101061J Cola(优先队列)
- 优先队列(C++/Java)
- CF 255 DIV2 D DZY Loves Modification(优先队列)
- 【CF 732E】Sockets(优先队列+贪心)
- CF 754D 贪心 + 优先队列
- CF 19C 后缀数组O(1)判Repeats ,优先队列模拟操作+map离散
- 优先队列的精简实现(c++)
- LDF文件过大,如何收缩
- uva 111 History Grading
- Web系统大规模并发——电商秒杀与抢购
- Algorithms—120.Triangle
- poj1870--Bee Breeding(模拟)
- CF 45C Dancing Lessons(优先队列)
- 二叉树
- BPM那此事儿——实施BPM方法
- NSCache
- Hibernate 持久化状态、HQL语句大全(转)
- Android Message和obtainMessage的区别
- RabbitMq、ActiveMq、ZeroMq、kafka之间的比较,资料汇总
- [leetcode] 87.Scramble String
- 移动端CSS Reset