题目链接:

https://cn.vjudge.net/problem/POJ-3476

题目大意:

一串长度为N的彩球,编号为1-N,每个球的颜色为R,G,B,给出它们的颜色,然后进行如下操作:

每次消除连续颜色最长的最左端的一串,剩下的球如果分成两串,就把左右两串连接起来,输出每次消除的球的颜色及编号。

解题思路:

将球的同一颜色的串压入优先队列中,每次取出最长的串,相同长度的串取最左端的串。

取出来之后,如果将小球分成了两串,如果两端颜色一样可以合并,那就网优先队列中压入新合成的串。每次取出串之后,将串的每一位进行标记,原因是由于没有将原来的两串删除就直接直接加入合并后的串,所以只需要标记一下已经取出,那么后加入的串由于长度长会先出优先队列。算法是正确的。

对于需要输出具体是那些小球,利用pre数组和next数组,pre[i]表示第i个小球前面连着的小球的下标。next则表示后面连着的小球,用这两个数组模拟双端链表,可以输出具体是那些小球。

用C++提交不会超时

 //#include<bits/stdc++.h>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 1e6 + ;
typedef long long ll;
struct node
{
char c;
int pos, len;
node(char c, int pos, int len):c(c), pos(pos), len(len){}
bool operator <(const node& a)const
{
return len < a.len || len == a.len && pos > a.pos;//优先队列
}
};
char s[maxn];
int pre[maxn], next[maxn];
bool vis[maxn];
priority_queue<node>q;
int main()
{
scanf("%s", s);
int n = strlen(s);
for(int i = ; i < n;)
{
int st = i, len = ;
while(s[++i] == s[st])len++;
//cout<<s[st]<<" "<<st<<" "<<len<<endl;
q.push(node(s[st], st, len));
}
for(int i = ; i < n; i++)
{
pre[i] = i - , next[i] = i + ;
}
memset(vis, , sizeof(vis));
while(!q.empty())
{
node now = q.top();
q.pop();
if(now.len <= )break;
if(vis[now.pos])continue;
printf("%c", now.c);
int head = pre[now.pos], tail = now.pos;
for(int i = ; i < now.len; i++, tail = next[tail])
{
vis[tail] = ;
printf(" %d", tail + );
}
puts("");
if(head >= )next[head] = tail;
if(tail < n)pre[tail] = head;
if(head < || tail >= n || s[head] != s[tail])continue; int len = ;
while(pre[head] >= && s[pre[head]] == s[head])
head = pre[head], len++;
while(next[tail] < n && s[next[tail]] == s[tail])
tail = next[tail], len++;
q.push(node(s[head], head, len));
}
return ;
}

POJ - 3476 A Game with Colored Balls---优先队列+链表(用数组模拟)的更多相关文章

  1. 1350: To Add Which? (优先队列+贪心 或者 数组模拟)

    1350: To Add Which? Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitt ...

  2. POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)

    题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...

  3. 【POJ 3476】A Game with Colored Balls

    POJ 3476 首先写了个treap,然后常数太大tle了... 然后想了个极为复杂的方法,是一共7个dsu,3个bit,还有一个set.然后写了一半就歇菜了... 然后看dxm的方法,是这样做的: ...

  4. Codeforces554 C Kyoya and Colored Balls

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

  5. 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 ...

  6. 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 ...

  7. Kyoya and Colored Balls(组合数)

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

  8. 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 ...

  9. 554C - Kyoya and Colored Balls

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

随机推荐

  1. Codeforces 985G. Team Players

    Description 有 \(n\) 个人 , \(m\) 对人有冲突 , 你要从这 \(n\) 个人中选出三个人成为一组 , 使得同一组的人不存在一对有冲突 题面 Solution 容斥 答案=总 ...

  2. Metronic 对话 chat

    http://keenthemes.com/preview/metronic/theme/admin_1/index.html: jquery让滚动条默认在最底部:$('#content').scro ...

  3. [转]C# 6.0 的新特性

    本文的内容包括引入C#6.0中的新的语言特性有哪些. 还有已经被引入的代码名称为 “Roslyn”新编译器. 编译器是开放源码的,并且可以从 codeplex 网站的这个地址下载到源代码: https ...

  4. 访问Django项目出现DisallowedHost at / Invalid HTTP_HOST header问题

    闲来无事,想玩玩django,源码安装碰到了一堆乱七八糟依赖性问题,耗费一下午的时间总算是在ubuntu14.04上搭建好了python3+django2开发环境, 心血来潮,创建了一个django项 ...

  5. 二、hdfs单节点安装

    一.准备环境 在配置hdfs之前,我们需要先安装好hadoop的配置,本文主要讲述hdfs单节点的安装配置. hadoop的单节点安装配置请参考:https://www.cnblogs.com/lay ...

  6. JAVA基础之——使用idea创建maven项目 以及使用tomcat本地调试springmvc

    前言:关于这个话题网上有很多,本文旨在引导实战纠偏,理论偏少,如果按照步骤还不能达到本文目的,请留言. 1 环境准备 1.1 软件准备 idea:官方下载社区版,下载后安装 maven:Apache- ...

  7. PHP-redis英文文档

    作为程序员,看英文文档是必备技能,所以尽量还是多看英文版的^^ PhpRedis The phpredis extension provides an API for communicating wi ...

  8. xshell ssh连接linux时提示ssh服务器拒绝了密码

    用Xshell root连接时显示ssh服务器拒绝了密码,应该是应该是sshd的设置不允许root用户用密码远程登录 修改 /etc/ssh/sshd_config文件,注意,安装了openssh才会 ...

  9. java工厂模式个人体会

    上一边文章主要对单例模式做了一个总结,这篇文章主要对工厂模式也写一写个人的体会. 工厂模式是设计模式的一种,它主要是把实现产品对象的过程封装起来,然后提供给客户端相应的接口.工厂模式也是有3种,分别为 ...

  10. jquery怎么取得有好几个并且name是相同的值

    jQuery("input[name='number']").each(function(){ alert(jQuery(this).val()); });