舞蹈课 (C++堆的优先级与重载)
2012-10-25 16:02
197 查看
第三题:舞蹈课(dancingLessons)
时间限制:1秒内存限制:256MB
输入:dancingLessons.in
输出:dancingLessons.out
问题描述
有n个人参加一个舞蹈课。每个人的舞蹈技术由整数来决定。在舞蹈课的开始,他们从左到右站成一排。当这一排中至少有一对相邻的异性时,舞蹈技术相差最小的那一对会出列并开始跳舞。如果相差最小的不止一对,那么最左边的那一对出列。一对异性出列之后,队伍中的空白按原顺序补上(即:若队伍为ABCD,那么BC出列之后队伍变为AD)。舞蹈技术相差最小即是的绝对值最小。你的任务是,模拟以上过程,确定跳舞的配对及顺序。
输入
第一行为正整数:队伍中的人数。下一行包含n个字符B或者G,B代表男,G代表女。下一行为n个整数。所有信息按照从左到右的顺序给出。在50%的数据中,。输出
第一行:出列的总对数k。接下来输出k行,每行是两个整数。按跳舞顺序输出,两个整数代表这一对舞伴的编号(按输入顺序从左往右1至n编号)。请先输出较小的整数,再输出较大的整数。样例输入
4BGBG
4 2 4 3
样例输出
23 4
1 2
样例输入
4BGBB
1 1 2 3
样例输出
11 2
堆的操作
--堆默认为小根堆(重载Operator<) 小的放前面
注意此处用greater - 大根堆
#include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<cstdlib> #include<iostream> #include<functional> #include<algorithm> using namespace std; #define MAXN (200000 + 10) #define MAXAI (10000000 + 10) int n; char s[MAXN]; int a[MAXN]; bool b[MAXN]; struct pair2 { int x,y,w; }ans[MAXN]; bool operator>(const pair2 a,const pair2 b) { if (a.w==b.w) return a.x>b.x; return a.w>b.w; } void cout_pair(const pair2 a) { printf("%d %d\n",a.x,a.y); } priority_queue <pair2, vector<pair2>, greater<pair2> > q; void push(int x,int y) { pair2 now; now.x=x; now.y=y; now.w=abs(a[x]-a[y]); q.push(now); // cout<<"add "<<now.x<<' '<<now.y<<' '<<now.w<<'\n'; } int main() { freopen("dancingLessons.in","r",stdin); freopen("dancingLessons.out","w",stdout); scanf("%d\n%s",&n,s); for (int i=1;i<=n;i++) scanf("%d",&a[i]); memset(b,0,sizeof(b)); for (int i=1;i<n;i++) if (s[i-1]!=s[i]) push(i,i+1); int tot=0; while (!q.empty()) { pair2 now=q.top(); // cout_pair(now); q.pop(); if (b[now.x]||b[now.y]) continue; b[now.x]=1; b[now.y]=1; ans[++tot]=now; int l=now.x-1,r=now.y+1; if (l<1||r>n) continue; while (l>1&&b[l]) l--; while (r<n&&b[r]) r++; if (b[l]||b[r]) continue; if (s[l-1]!=s[r-1]) push(l,r); } printf("%d\n",tot); for (int i=1;i<=tot;i++) cout_pair(ans[i]); // while (1); return 0; }
相关文章推荐
- 关于java重载(重载方法匹配优先级)
- 关于参数为可空值类型的多个重载的优先级 (彻底晕掉了)
- C++模板编程->分辨重载优先级
- C++模板编程->分辨重载优先级
- 关于Java重载方法匹配优先级
- [013]函数重载--int*和void*的匹配优先级
- Java中的重载方法之匹配优先级
- priority_queue优先级重载
- 重载,集成和多态;算符优先级;没事儿找事儿
- priority_queue 优先级队列的基本应用和重载问题
- C++普通函数与模板函数以及特化函数重载的优先级问题
- C++运算符能否重载以及优先级问题
- 关于Java重载方法匹配优先级
- C++运算符优先级以及可否重载
- java重载方法匹配的优先级
- 关于Java重载方法匹配优先级
- 【转】Java中重载和重写的区别
- 第八周-项目一-复数类中的运算符的重载
- 重载overload和重写overried的区别
- c++ 优先级队列输出最小值