题意:
CodeForces 45C Dancing Lessons
博客搬新家,以后就是基本仅仅在自己的独立博客进行更新,欢迎訪问。
题目描写叙述:
题目链接:CodeForces 45C Dancing Lessons
题目大意:
有
- 若要组为舞伴则两人必须相邻。且两人舞蹈技能值差小的,优先组合,出队。
- 存在多个可选舞伴的话,那么就从队首起左边的优先出队。
- 抽走一对后,队伍合并,补齐空位。
问:最多能够出队多少队,而且按顺序输出跳舞成员编号(初始队列的左起位置)。
解题思路:
依据题目的描写叙述能够非常easy的将整个舞蹈队和出队要求构造成一个优先队列。
队列的每一个结点包括信息有。左側学员编号,右側学员编号,技能值差。而出队的小于符号应以两个结点的技能值差为第一比較小的优先出队,若技能值差同样。则比較左側学员的编号,编号小的优先出队。
另外使用数组
复杂度分析:
时间复杂度 :
空间复杂度 :
AC代码:
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxn = (100010 << 1);
int skill[maxn];
bool cp[maxn];
int r[maxn],l[maxn];
char que[maxn];
struct node{
int b,g,c;
bool operator < (const node& a) const{
if(c == a.c)return b > a.b;
else return c > a.c;
}
};
priority_queue<node>Q;
int main()
{
int n;
while(cin>>n)
{
cin >> que+1;
node tmp;
int cup = 0;
for(int i = 1; i <= n; i++){
l[i] = i - 1;
r[i] = i + 1;
if(que[i] == 'B')cup++;
cin >> skill[i];
}
cup = min(n - cup, cup);
while(!Q.empty()) Q.pop();
for(int i = 1; i < n; i++)
if(que[i] != que[i+1]){
tmp.b = i;
tmp.g = i+1;
tmp.c = abs(skill[i] - skill[i+1]);
Q.push(tmp);
}
memset(cp,false,sizeof(cp));
cout<< cup << endl;
while(cup--)
{
while(!Q.empty())
{
tmp=Q.top();
Q.pop();
if(!cp[tmp.b]&&!cp[tmp.g])
{
cout<< tmp.b << " " << tmp.g << endl;
cp[tmp.b] = cp[tmp.g] = true;
break;
}
}
int lc=l[tmp.b],rc=r[tmp.g];
r[lc] = rc;
l[rc] = lc;
tmp.b = lc;
tmp.g = rc;
tmp.c = abs(skill[lc]-skill[rc]);
if(lc > 0 && rc <= n && que[lc] != que[rc]) Q.push(tmp);
}
}
}
版权声明:转载请注明转自http://blog.csdn.net/a15129395718。独立博客:http://zihengoi.cn欢迎訪问
- 本文已收录于下面专栏:
内容举报
返回顶部
收藏助手
不良信息举报
posted on 2018-01-11 12:55 yjbjingcha 阅读(173) 评论(0) 编辑 收藏 举报